Find a Consecutive Warehouse Handoff
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
handoff_sequence = [78, 103]warehouse_visits = [12, 45, 78, 103, 156]Truehandoff_sequence = [18, 40]warehouse_visits = [7, 18, 29, 40]Falsehandoff_sequence = []warehouse_visits = [91, 205, 317]TrueConstraints
0 <= len(warehouse_visits) <= 1001 <= warehouse_visits[i] <= 10000 for every i0 <= len(handoff_sequence) <= len(warehouse_visits)1 <= handoff_sequence[i] <= 10000 for every i- Types:
warehouse_visitsis int[],handoff_sequenceis int[]; result is bool
Function signature
def contains_handoff_sequence(warehouse_visits: list[int], handoff_sequence: list[int]) -> bool
warehouse_visitslist[int]- Warehouse identifiers visited by the shipment, in travel order.
handoff_sequencelist[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.