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]))- A. 224
- B. 237
- C. 213
- 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
- What is printed? python def zeros(n): c = 0 while n: n //= 5 c += n return c print(zeros(100), zeros(25))
- What is printed? python def mincut(s): n = len(s) pal = [[False] n for in range(n)] for i in range(n - 1, -1, -1): for j in range(i, n)…
- What is printed? python def triecount(words, pref): root = {} for w in words: node = root for ch in w: node = node.setdefault(ch, {})…
- What is printed? python M = 109 + 7 N = 10 fact = [1] (N + 1) for i in range(1, N + 1): fact[i] = fact[i - 1] i % M def nCr(n, r): return…
- What is printed? python def summin(a): MOD = 109 + 7 st = [] total = 0 cur = 0 for x in a: cnt = 1 while st and st[-1][0] >= x: v, c =…
- What is printed? python print(6 & 3 == 2, 5 | 2 == 7)