Workspace/Coding labs
Loading progress

Build a symmetric quantizer

Intermediate40 min

Implement quantize(values,bits), returning (codes, scale, restored). Bits must be an integer from 2 through 8. Use qmax=2**(bits-1)-1, scale=max(abs(values))/qmax, and Python round for codes. Use scale=1.0 for empty or all-zero input. Reject nonfinite or nonnumeric values and booleans. The symmetric scheme intentionally does not use the most negative signed code.

Your task

  1. Complete the starter function using the contract above.
  2. Use the examples and visible tests to check normal inputs, boundaries, and rejected inputs.
  3. Run tests to record your result, then compare with the explained reference solution.

Examples

EXAMPLE 1

Inputvalues=[-1,-0.2,0.4,1], bits=4

Outputcodes=[-7,-1,3,7], scale=1/7

Dequantization multiplies codes by scale.
EXAMPLE 2

Inputvalues=[0,0], bits=4

Output([0,0],1.0,[0.0,0.0])

The zero range avoids division by zero.
solution.pyPython 3.12