PracticeStrings

Keep the Even-Position Label Characters

BeginnerStringstraversalindex arithmeticstring building Time not estimated Not started

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) <= 100000
  • label contains lowercase letters and spaces
  • Types: label is str; result is str

Function signature

def keep_even_label_positions(label: str) -> str
label str
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.

Approved · cc013-abs_680_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1