PracticeSearching
Find a Shipment Label
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:
TrueExplanation: 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:
TrueExplanation: 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:
FalseExplanation: 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) <= 100000 <= len(query_token) <= 30shipment_text contains lowercase letters and spaces.query_token contains only lowercase letters.- Types:
shipment_textis str,query_tokenis str; result is bool
Function signature
def contains_shipment_label(shipment_text: str, query_token: str) -> bool
shipment_textstr- The shipment label record, containing lowercase words separated by spaces.
query_tokenstr- 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.
Code
Saved