Unpack signed converter samples

Write codeMedium22 min

Your task

A vibration logger packs signed converter samples end to end into 16 bit halfwords. Sample 0 starts at bit 0 of halfwords[0], counting the least significant bit of a halfword as bit 0. Each later sample starts where the one before it ended, and a sample that runs past bit 15 carries on in the low bits of the next halfword. Every sample is width bits wide and is held in two's complement, so a sample whose top bit is set reads as a negative value. width is between 2 and 12, and each halfword holds a value from 0 to 65535. Return the first count samples as signed values, in the order they were logged. Return an empty list when count is 0, and ignore any halfwords left over past the last sample asked for.

Examples

decode_sample_stream(halfwords: list[int], width: int, count: int) -> list[int]
Example 1
Inputdecode_sample_stream([34801], 4, 4)
Output[1, -1, 7, -8]
Example 2
Inputdecode_sample_stream([16259, 24], 5, 5)
Output[3, -4, 15, -16, 1]
Example 3
Inputdecode_sample_stream([2047, 128, 0], 12, 3)
Output[2047, -2048, 0]