PracticeStrings

First Symmetric Shipment Label

EasyStringstraversalcomparisonstate tracking Time not estimated Not started

Context

A warehouse scanner records shipment labels in loading order. A label is symmetric when it reads identically from left to right and right to left. Find the position of the first symmetric label so the dock team can inspect it. If no label qualifies, return -1.

Problem

Write a function that examines shipment labels from the beginning of the list. Return the zero-based position of the first label that is symmetric: its characters must match in mirrored positions. Stop scanning as soon as the first qualifying label is found. Return -1 when the list is empty or no label is symmetric.

Examples

Example 1
Input: labels = ["level", "cargo", "crate"]
Output: 0
Explanation: The executed call returned 0, and its final `answer` and `position` were both 0.
Example 2
Input: labels = ["parcel", "tablet", "radar", "box"]
Output: 2
Explanation: The executed call returned 2, and its final `answer`, `position`, and `current_item` were 2, 2, and `"radar"`.
Example 3
Input: labels = ["apple", "banana", "cloud"]
Output: -1
Explanation: The executed call returned -1 after the final position was 2.

Constraints

  • 0 <= len(labels) <= 100
  • 1 <= len(labels[i]) <= 20 for every i
  • labels[i]: alphabet lowercase
  • labels contains only lowercase letters.
  • Types: labels is str[]; result is int

Function signature

def find_first_symmetric_label(labels: list[str]) -> int
labels list[str]
Shipment labels listed in loading order.
returns int
The zero-based position of the first symmetric label, or -1 if no label satisfies the predicate.

Adapted from MBPP problem task_278 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Notes

  • The result counts labels before the first match.
  • A symmetric label may appear at the first or final position.
Approved · c018r1-abs_732_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1