Question
Implement a reversible encoder for an integer sequence using run-length blocks for long runs and fixed-size literal blocks otherwise. ```python def encode(values: list) -> list: ... def decode(blocks: list) -> list: ... ``` ## Requirements and format - A run of at least eight equal values becomes `["RLE", value, count]` and consumes the full run. - Otherwise consume the next eight values as `["BP", values]`. - A final block may contain fewer than eight values. If every remaining value is equal, encode it as RLE; otherwise use a short BP block. - Walk strictly left to right, making output deterministic. `decode` must validate the entire block sequence before reconstructing it. Require a list of blocks: RLE has exactly three fields, an integer value and a positive integer count; BP has exactly two fields and a list of one through eight integer values. Booleans are not integers for this contract. Unknown block kinds and malformed shapes raise `ValueError`. Reject total decoded length above 1,000,000 with `ValueError` before allocating output. This is a practice resource limit, not a format limit. Empty block lists decode to empty output. Input to `encode` is an integer list of at most 1,000,000 elements. Do not mutate either input. ## Design discussion (not graded) 1. Pack BP values into a byte representation using a declared bit width. 2. Stream encoding and decoding without holding the entire sequence. 3. Add block headers, checksums, and corruption detection. 4. Choose block type based on encoded byte size rather than a fixed threshold.