PracticeArrays

Find a Consecutive Warehouse Handoff

EasyArraystraversalcomparisonnested iteration Time not estimated Not started

Context

A logistics planner records the warehouse identifiers visited by a shipment in order. A delivery request also specifies a short required handoff sequence. Determine whether that required sequence appears as consecutive entries somewhere in the recorded visit list.

Problem

Given a source list of warehouse identifiers and a target list describing a required handoff sequence, return whether the target occurs as one contiguous subsequence of the source. The identifiers must match in order with no gaps. An empty target is considered present in every source, while a nonempty target longer than the source cannot occur. Check possible starting positions and stop as soon as one complete match is confirmed.

Examples

Example 1
Input: handoff_sequence = [78, 103]warehouse_visits = [12, 45, 78, 103, 156]
Output: True
Explanation: The first call returns true because its final traced found_flag is true after matching the target at start_pos 2 with match_len 2.
Example 2
Input: handoff_sequence = [18, 40]warehouse_visits = [7, 18, 29, 40]
Output: False
Explanation: The second call returns false because its final traced found_flag is false after the candidate checks finish.
Example 3
Input: handoff_sequence = []warehouse_visits = [91, 205, 317]
Output: True
Explanation: The third call returns true because the empty handoff_sequence is considered present, and the executed call returns true. (,, )

Constraints

  • 0 <= len(warehouse_visits) <= 100
  • 1 <= warehouse_visits[i] <= 10000 for every i
  • 0 <= len(handoff_sequence) <= len(warehouse_visits)
  • 1 <= handoff_sequence[i] <= 10000 for every i
  • Types: warehouse_visits is int[], handoff_sequence is int[]; result is bool

Function signature

def contains_handoff_sequence(warehouse_visits: list[int], handoff_sequence: list[int]) -> bool
warehouse_visits list[int]
Warehouse identifiers visited by the shipment, in travel order.
handoff_sequence list[int]
Warehouse identifiers that must appear consecutively.
returns bool
Return true exactly when the complete handoff_sequence matches a contiguous portion of warehouse_visits; otherwise return false.

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

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