Single-byte checksum of a payload
Many lightweight protocols use a one-byte checksum: just the sum of all bytes modulo 256.
- Each element in the payload is a byte value (0–255).
- The checksum is the sum of all bytes modulo 256.
- An empty payload has a checksum of 0.
checksum_byte(payload: list<int>) → int
Where you start
def checksum_byte(payload: list[int]) -> int:
Worked examples
| Call | Result |
|---|---|
checksum_byte([1, 2, 3]) | 6 |
checksum_byte([255, 1]) | 0 |
checksum_byte([]) | 0 |
checksum_byte([128, 128]) | 0 |
Hint
Add every element, then take the remainder when divided by 256.
Reference solution in Python
def checksum_byte(payload: list[int]) -> int:
return sum(payload) % 256