Skip to content

The Python implementation of Huffman encoding fails on 1-character-long strings and empty strings #739

Closed
@thecnoNSMB

Description

@thecnoNSMB

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 an IndexError on line 32.

Secondly, if you call build_huffman_tree on a string 1 character long (Hypothesis suggested '0'), and then call build_codebook with the resulting tree, it throws ValueError: not enough values to unpack (expected 2, got 1) on line 41. The tree that build_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 bug can be reproduced
  • The bug can be fixed (if not, please explain why not in a comment below)
  • There is a timeline to fix the bug
  • The bug has been fixed (Please link the PR)

Metadata

Metadata

Assignees

Labels

ProblemThis is a problem in the archive or an implementation.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions