PY-17
Encode Ordered IDs as Gaps
Task
Write two functions that convert an ordered list of IDs to gaps and back again:
encode_gaps(ids) and decode_gaps(gaps).
The input to encode_gaps is a list of non-negative integers in strictly
increasing order. The first gap is the first ID itself. Every later gap is the
difference between the current ID and the previous ID. For example, the IDs
[3, 8, 10] become [3, 5, 2].
The input to decode_gaps is a valid gap list produced by this rule: it is
empty, or its first value is a non-negative integer and each later value is a
positive integer. Reconstruct the original IDs by treating the first gap as
the first ID and adding each later gap to the previous ID.
An empty list encodes to an empty list and decodes to an empty list. Neither function may change its input list.
Example
The first gap has no previous ID to subtract from. Each later gap records only the increase since the preceding ID, so decoding restores every value and its order.
Your implementation
Edit solution.py and keep both function names and signatures:
Each function must return a new list of integers. The two functions must be
exact inverses for every valid input: decode_gaps(encode_gaps(ids)) must equal
ids. Do not print results, ask for input, or mutate either argument. The
contract supplies valid, strictly increasing IDs and valid gap lists, so error
handling for invalid inputs is not part of this problem.