PracticeStrings
First Symmetric Shipment Label
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:
0Explanation: The executed call returned 0, and its final `answer` and `position` were both 0.
Example 2
Input:
labels = ["parcel", "tablet", "radar", "box"]Output:
2Explanation: 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:
-1Explanation: The executed call returned -1 after the final position was 2.
Constraints
0 <= len(labels) <= 1001 <= len(labels[i]) <= 20 for every ilabels[i]: alphabet lowercaselabels contains only lowercase letters.- Types:
labelsis str[]; result is int
Function signature
def find_first_symmetric_label(labels: list[str]) -> int
labelslist[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.
Code
Saved