Read and Write a Binary File Format

medium
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 20. Everything before it counts as cleared.

Design-and-parse binary formats with the struct module. The format (all little-endian):

  • header: the 4 bytes b"FRG1", then the record count as uint32
  • each record: id as uint32, score as float64, then the name's UTF-8 byte length as uint16, then the name bytes

Implement:

  • pack_records(records): records is a list of (id, score, name) tuples. Return the bytes.
  • unpack_records(data): the inverse. Raise ValueError for a bad magic number, truncated data, or extra bytes after the last record.
  • record_size(name): the encoded size of one record in bytes
#binary#struct#serialization2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Read and Write a Binary File Format · TechGradify