← Quiz: Rooted Trees and Huffman Coding: all 10 questions
Quiz: Rooted Trees and Huffman Coding · Question 7 of 10
What does this print? (code)
What does this print?
code = {"a": "0", "b": "10", "c": "11"}
reverse = {w: ch for ch, w in code.items()}
out, cur = "", ""
for bit in "10011010":
cur += bit
if cur in reverse:
out += reverse[cur]
cur = ""
print(out)
- A.baca
- B.bacab
- C.bcab
- D.abcab
Answer & explanation
Correct answer: bacab
Split as 10 | 0 | 11 | 0 | 10, which reads b, a, c, a, b. A prefix code never needs to look ahead.
Can you get the rest right without peeking?
Take the timed Quiz: Rooted Trees and Huffman Coding quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →