Exercise 1 solution import struct def make_wav(pcm, rate=8000, channels=1, extra=b""): fmt = struct.pack(" len(raw): raise ValueError("truncated chunk header") cid, size = struct.unpack_from("<4sI", raw, pos) if pos + 8 + size > len(raw): raise ValueError(f"chunk {cid!r} runs past end of file") found.append((cid, size)) pos += 8 + size + (size & 1) # odd sizes are followed by one pad byte return found odd = b"junk" + struct.pack("", e) # [(b'fmt ', 16), (b'junk', 3), (b'data', 6)] # signature -> not a WAV file # past end -> chunk b'data' runs past end of file # short header -> truncated chunk header (Run as written; the output matched the comments.) Explanation: The walker starts at byte 12, after "RIFF", the size and "WAVE". Each chunk is a 4-letter id, a 4-byte little-endian size and the data, and a chunk with an odd size is followed by one pad byte that is not counted in the size. The test chunk has 3 data bytes, so skipping its pad byte is what lets the walker find "data" next. Every size is checked against the bytes really present before it is used.