All problems
Huffman Coded Size in Bytes
Problem
Build a Huffman code from the given symbol frequencies and return the total compressed size in whole bytes. The total bit count is the sum of frequency times codeword length, which equals the sum of all merge weights for two or more symbols, while a lone symbol costs one bit per occurrence. Return 0 for an empty frequency list.
Hint
The total Huffman bit count equals the sum of every merge weight when there are at least two symbols.
Test cases
case 1
in: [[5,2,1]]
expected: 2
case 2
in: [[1,1]]
expected: 1
case 3
in: [[8]]
expected: 1
case 4
in: [[1,1,1,1]]
expected: 1
case 5
in: [[]]
expected: 0
Starter codePython
import heapq
def huffman_size_bytes(freqs):
# Your code here
pass5 test cases · run your code against them in the editor