Skip to content
Tactiverge

△ Communications · modular arithmetic

Check a sensor packet

Compute an 8-bit checksum and reject corrupted radio packets.

  • Level 2 · Builder
  • 7 tests
  • technically checked

Your task

A radio link sends sensor packets as arrays of bytes. The LAST byte is a checksum: the sum of all the other bytes, modulo 256 (so it fits in one byte).

Write:

  • checksum(bytes) → (sum of bytes) % 256. An empty array has checksum 0.
  • isValid(packet) → true if the last byte equals the checksum of all the bytes before it.

Hints (0 of 2 shown)

    What gets checked

    • checksum([1, 2, 3]) → 6
    • checksum([200, 100]) → 44
    • isValid([10, 20, 30]) → true

    Plus 4 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.

    Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments

    Starting the sandbox…

    Press Run to see what your program prints.

    Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.