Huffman Encode
Huffman code is a specific type of optimal prefix code that is commonly used for lossless data compression. The algorithm was developed by David A. Huffman while he was a Sc.D. student at MIT and was published in 1952.
The output from Huffman's algorithm can be viewed as a variable-length code table for encoding a source symbol. The algorithm derives this table from the estimated probability or frequency of occurrence (weight) for each possible value of the source symbol. As in other entropy encoding methods, more common symbols are generally represented using fewer bits than less common symbols.
The simplest construction algorithm uses a priority queue, where the node with the lowest frequency is given the highest priority.
- Create a leaf node for each symbol and add it to the priority queue.
- While there is more than one node in the queue:
- Remove the two nodes of highest priority (lowest frequency) from the queue.
- Create a new internal node with these two nodes as children and with a frequency equal to the sum of the two nodes' frequencies.
- Add the new node to the queue.
- The remaining node is the root node, and the tree is complete.
It's important to note that for our task, nodes with the same frequency have different priorities. Symbols with lower ASCII code have a higher priority, for example, 'A' has a higher priority than 'B', and 'DZ' has a higher priority than 'E'.
- Mark the connections between nodes with 0 and 1 (the connection with the higher priority node with 0, and the other with 1).
- The digits along the way from the root node to the leaf form the code for the leaf's symbol.
- The result for our task is a source string in which all the symbols have been replaced by their codes.
Input: String (str).
Output: String (str).
Examples:
assert huffman_encode("BADABUM") == "1001110011000111" assert ( huffman_encode("A DEAD DAD CEDED A BAD BABE A BEADED ABACA BED") == "1000011101001000110010011101100111001001000111110010011111011111100010001111110100111001001011111011101000111111001" ) assert ( huffman_encode("no devil lived on") == "100101111000001110010011111011010110001000111101100" ) assert huffman_encode("an assassin sins") == "110111100110001100010111110001011110"
How it’s used: Commonly used for lossless data compression.
Precondition: Given string maximum length is 32000. String contains letters and spaces (a-z, A-Z, " ").
Idea for the mission was taken from local school challenge for kids.
Screen by Cmglee for wiki.
CheckiO Extensions allow you to use local files to solve missions. More info in a blog post.
In order to install CheckiO client you'll need installed Python (version at least 3.8)
Install CheckiO Client first:
pip3 install checkio_client
Configure your tool
checkio --domain=py config --key=
Sync solutions into your local folder
checkio sync
(in beta testing) Launch local server so your browser can use it and sync solution between local file end extension on the fly. (doesn't work for safari)
checkio serv -d
Alternatevly, you can install Chrome extension or FF addon
checkio install-plugin
checkio install-plugin --ff
checkio install-plugin --chromium
Read more here about other functionality that the checkio client provides. Feel free to submit an issue in case of any difficulties.
Welcome to Pair Programming! Engage in real-time collaboration on coding projects by starting a session and sharing the provided unique URL with friends or colleagues. This feature is perfect for joint project development, debugging, or learning new skills together. Simply click 'Start Session' to begin your collaborative coding journey!
You are trying to join a pair programming session that has not started yet.
Please wait for the session creator to join.
It looks like the creator of the pair programming session closed the editor window.
It might happen accidentally, so that you can wait for reconnection.