ProgrammingVery hardGATE CS & IT · Infosys

What is printed?

What is printed?
import heapq
def huff(freqs):
    h = list(freqs)
    heapq.heapify(h)
    cost = 0
    while len(h) > 1:
        a = heapq.heappop(h)
        b = heapq.heappop(h)
        cost += a + b
        heapq.heappush(h, a + b)
    return cost
print(huff([5, 9, 12, 13, 16, 45]))
  1. A. 224
  2. B. 237
  3. C. 213
  4. D. 250

Correct answer: A. 224

Huffman coding repeatedly merges the two smallest frequencies, and the total cost is the sum of all merged weights. For these frequencies the merges sum to 224.

More Python Programming questions

Exams that ask this topic