Match Shipment Assignment Patterns
Context
A carrier records the sequence of shipment codes loaded onto a truck and the sequence of warehouse codes receiving those shipments. The actual names are not important; the repetition pattern is. Determine whether each distinct shipment code consistently corresponds to one distinct warehouse code throughout the two aligned records.
Problem
Given two equal-length arrays, shipment_codes and warehouse_codes, compare their positional patterns. A shipment code must always correspond to the same warehouse code wherever it appears. Also, two different shipment codes must never correspond to the same warehouse code. Return true only when every aligned position satisfies one consistent one-to-one correspondence. Return false if the arrays have different lengths or if any consistency or distinctness rule fails.
Examples
shipment_codes = ["alpha", "beta", "alpha", "gamma"]warehouse_codes = ["north", "south", "north", "east"]Trueshipment_codes = ["alpha", "beta", "alpha"]warehouse_codes = ["north", "south", "east"]Falseshipment_codes = ["alpha", "beta", "gamma"]warehouse_codes = ["north", "north", "east"]FalseConstraints
1 <= len(shipment_codes) <= 1001 <= len(shipment_codes[i]) <= 20 for every ishipment_codes[i]: alphabet lowercaselen(shipment_codes) <= len(warehouse_codes) <= len(shipment_codes)1 <= len(warehouse_codes[i]) <= 20 for every iwarehouse_codes[i]: alphabet lowercaseshipment_codes contains only lowercase letters.warehouse_codes contains only lowercase letters.- Types:
shipment_codesis str[],warehouse_codesis str[]; result is bool
Function signature
def matches_assignment_pattern(shipment_codes: list[str], warehouse_codes: list[str]) -> bool
shipment_codeslist[str]- The shipment code at each loading position.
warehouse_codeslist[str]- The warehouse code assigned at each matching position.
- returns bool
- True when the two arrays have identical positional repetition structure under a one-to-one correspondence; otherwise false.
Adapted from MBPP problem task_74 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- The correspondence must be one-to-one in both directions.
- An expected linear-time scan with linear auxiliary bookkeeping is intended.