PracticeSearching

Find a Shipment Label

EasySearchingtraversalstate trackingcomparison Time not estimated Not started

Context

A warehouse records a space-separated line of lowercase labels for packages loaded onto a delivery route. Dispatchers need to check whether a particular label appears in that record before assigning handling instructions.

Problem

Write a function that checks whether the query token occurs in the shipment text. Split the text into tokens using spaces as separators; leading, trailing, and repeated spaces do not create tokens. Return true if at least one token is exactly equal to the query token, and return false otherwise. An empty query token is never considered a match. Scan the derived tokens once and stop as soon as a match is found.

Examples

Example 1
Input: query_token = "fragile"shipment_text = "blue box fragile"
Output: True
Explanation: The derived token list is ["blue", "box", "fragile"], and the target token is "fragile". The executed result is true because the final found_flag is true. (fact_ids:, )
Example 2
Input: query_token = "crate"shipment_text = "red crate priority"
Output: True
Explanation: The derived token list is ["red", "crate", "priority"], and the target token is "crate". The executed result is true because the final found_flag is true. (fact_ids:, )
Example 3
Input: query_token = "pack"shipment_text = "small package ready"
Output: False
Explanation: The derived token list is ["small", "package", "ready"], and the target token is "pack". The executed result is false because the final found_flag is false. (fact_ids:, )

Constraints

  • 0 <= len(shipment_text) <= 10000
  • 0 <= len(query_token) <= 30
  • shipment_text contains lowercase letters and spaces.
  • query_token contains only lowercase letters.
  • Types: shipment_text is str, query_token is str; result is bool

Function signature

def contains_shipment_label(shipment_text: str, query_token: str) -> bool
shipment_text str
The shipment label record, containing lowercase words separated by spaces.
query_token str
The lowercase label to search for.
returns bool
Return true exactly when the query token is one of the non-empty space-separated tokens in shipment_text; otherwise return false.

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

Notes

  • Spaces are the only separators.
  • Repeated separators are ignored and do not create empty tokens.
Approved · cb040-abs_297_v314 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1