PracticeStrings
Parity Combo Collector
Context
A player enters a lowercase move string during a gaming challenge. The level's combo rule selects either even-positioned or odd-positioned moves, using zero-based positions. Build the move sequence that survives this rule so the score engine can evaluate that combo.
Problem
Write a function that receives a move string and a parity choice. Positions are zero-based: position 0 is even, position 1 is odd, and so on. Keep every character whose position has the chosen parity, preserving the order in which moves appeared. Omit all other characters. If the move string is empty, return an empty string.
Examples
Example 1
Input:
moves = "wasd"parity = 0Output:
"ws"Explanation: For moves "wasd" and parity 0, the executed result is "ws".
Example 2
Input:
moves = "jump"parity = 1Output:
"up"Explanation: For moves "jump" and parity 1, the executed result is "up".
Example 3
Input:
moves = ""parity = 0Output:
""Explanation: For an empty move string, the executed result is an empty string.
Constraints
0 <= len(moves) <= 200moves: alphabet lowercase0 <= parity <= 1moves contains only lowercase letters.- Types:
movesis str,parityis int; result is str
Function signature
def collect_parity_moves(moves: str, parity: int) -> str
movesstr- The sequence of lowercase move characters entered by the player.
parityint- The position parity to keep: 0 for even positions or 1 for odd positions.
- returns str
- The move string containing only characters at positions whose zero-based parity equals parity, in original order.
Adapted from MBPP problem task_202 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- Use zero-based positions, so the first character is at position 0.
- An empty moves string is valid and produces an empty result.
Code
Saved