Question
A nested value contains integer leaves and internal `list`, `tuple`, or `dict` nodes. Dictionaries are traversed in insertion order. Implement: ```python def flatten(structure) -> list[int]: ... def unflatten(values: list[int], structure): ... ``` `flatten` returns leaves in traversal order. `unflatten` uses `structure` as a shape template and rebuilds the same container types while replacing leaves from left to right. Raise `ValueError` when there are too few or too many replacement values, and `TypeError` for unsupported nodes, including booleans. Empty containers must round-trip. Replacement values must be a list of non-boolean integers; reject other values with `TypeError`. Do not modify either input. Inputs are finite acyclic container trees; cycle detection and shared-reference preservation are separate extensions. The supplied `run_case` decodes version-one tagged containers into normal Python values and encodes the result with its types/order preserved. Implement the functions, not the fixture codec. Legacy plain JSON examples remain compatible. ## Design discussion (not graded) 1. Accept a predicate that defines which nodes are leaves. 2. Replace recursion with an explicit stack for deeply nested inputs. 3. Preserve shared references or detect cycles. 4. Make flattening lazy and rebuild from an iterator.