Example 1
Arguments: [
[
1,
2
]
]
Expected result: [
33
]Low nibble 1 plus high nibble 2×16 gives byte 33.
KV memory and quantization · Medium
Problem 38 of 50 · Python and NumPy
Pack signed four-bit integers into unsigned bytes. Store the first value in a byte’s low nibble and the second in its high nibble, using four-bit two’s-complement representation. If there is an odd last value, pad only the unused high nibble with zero. Return a list of byte integers; empty input returns [].
def pack_int4(values):Arguments: [
[
1,
2
]
]
Expected result: [
33
]Low nibble 1 plus high nibble 2×16 gives byte 33.
Arguments: [
[
-1,
-8
]
]
Expected result: [
143
]-1 is nibble 15 and -8 is nibble 8, giving 15+128=143.