PracticeStrings
Keep the Even-Position Label Characters
Context
A warehouse prints a compact shipment label for each delivery. The dispatch system needs a shortened label containing the characters found at positions 0, 2, 4, and so on, using zero-based positions. Character order must remain unchanged, and spaces are valid label characters.
Problem
Write a function that receives one shipment label and returns the subsequence formed by keeping every character whose zero-based position is even. Scan from left to right, preserve the order of retained characters, and treat spaces like any other character. If the label is empty, return an empty string.
Examples
Example 1
Input:
label = "parcel"Output:
"pre"Explanation: For the label "parcel", the executed output is "pre". [fact_ids:, ]
Example 2
Input:
label = "a b c"Output:
"abc"Explanation: For the label "a b c", the executed output is "abc" because the spaces are ordinary characters and the retained positions produce those three letters. [fact_ids:,, ]
Example 3
Input:
label = ""Output:
""Explanation: For the empty label, the executed output is an empty string. [fact_ids:,, ]
Constraints
0 <= len(label) <= 100000label contains lowercase letters and spaces- Types:
labelis str; result is str
Function signature
def keep_even_label_positions(label: str) -> str
labelstr- The shipment label whose characters are inspected by zero-based position.
- returns str
- The label containing only characters from even zero-based positions, in their original order.
Adapted from MBPP problem task_226 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Code
Saved