Add option to Huffman Code a string not just a file; add doctests to Huffman file #11516
+121
−29
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Describe your change:
Previously the
compression/huffman.py file
provided thehuffman
function to Huffman Code the contents of a file and print the result.I have added the
huffman_string
function (along withparse_string
, which is analogous toparse_file
) to Huffman Code a given string and return the result.I have also edited the
huffman
function to allow the user to specify more options, such as how letter bitstrings should be separated and which file to print the result to. Calls to thehuffman
function with only one argument still do exactly the same thing as before.I have also added doctests to the existing
build_tree
function.EDIT: no longer doctests for existing
huffman
andparse_file
functions(I don't know if
huffman_string
counts as a new algorithm, I suppose it does)(I see it says avoid changing both code and tests in a single pull request, but since I was adding my own new functions with doctests, I thought it made sense to also add doctests for the existing functions)
Checklist: