The Python implementation of Huffman encoding fails on 1-character-long strings and empty strings #739
Closed
3 of 4 tasks
Labels
Problem
This is a problem in the archive or an implementation.
Bug Report
Description
There are two bugs here, both in huffman.py. First, if you call
build_huffman_tree
with an empty string, it throws anIndexError
on line 32.Secondly, if you call
build_huffman_tree
on a string 1 character long (Hypothesis suggested'0'
), and then callbuild_codebook
with the resulting tree, it throwsValueError: not enough values to unpack (expected 2, got 1)
on line 41. The tree thatbuild_huffman_tree
builds in this case is actually just a string that's one character long, both in this case and for #659, which I'm pretty sure is unintended behavior.Environment
This was tested in Python 3.8, but is likely reproducible on every Python version the script supports.
Additional context
These bugs were found using the Python property-based testing library Hypothesis. This issue may be a duplicate of, or related to, #659. Apologies if I trimmed too many sections from the template.
For Algorithm Archive Developers
The text was updated successfully, but these errors were encountered: