Reassemble a Message
Intermediate · Write the code · from How the Internet Works: Networks, Packets and Protocols
From the lesson: How does data travel in packets?
Information moves through the Internet as a data stream. The stream is cut into chunks, and each chunk is wrapped, or encapsulated, in a packet.
Read this part of the lessonWrite reassemble(packets, total). packets is a list of dictionaries with the keys "seq" (1 to total) and "data", in the order they arrived. Return the full message with the chunks in sequence order. If any packet from 1 to total is missing, return None. The same packet can arrive twice; use it once.
reassemble([{'seq': 2, 'data': 'lo!'}, {'seq': 1, 'data': 'Hel'}], 2) -> 'Hello!'def reassemble(packets, total):
# Use each packet's sequence number to put the data in order
pass
What Check runs
Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.
| Runs | Expected output |
|---|---|
| print(reassemble([{"seq": 2, "data": "lo!"}, {"seq": 1, "data": "Hel"}], 2)) | Hello! |
| print(reassemble([{"seq": 3, "data": "am"}, {"seq": 1, "data": "Go"}, {"seq": 2, "data": " te"}], 3)) | Go team |
| print(reassemble([{"seq": 1, "data": "Hi"}], 1)) | Hi |
More Networking exercises
- Is It a Path?Intermediate
- How Long Will It Take?Beginner