Build a symmetric quantizer
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
- Complete the starter function using the contract above.
- Use the examples and visible tests to check normal inputs, boundaries, and rejected inputs.
- 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
EXAMPLE 2
Inputvalues=[0,0], bits=4
Output([0,0],1.0,[0.0,0.0])
Implement the function, then run the tests.
Ctrl / ⌘ + Enter to test · Shift + Tab leaves editor