An open API service indexing awesome lists of open source software.

https://github.com/bapjiws/huffman-code


https://github.com/bapjiws/huffman-code

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

        

Given a non-empty string s no longer than 104 symbols, encode.py builds an optimal prefix-free code.
The first output line shows how many different k letters are in the string and the encoded string's length.
The next k lines show letters' codes in "letter: code" format. The last line shows the encoded string.

Sample Input 1:

a

Sample Output 1:

1 1

a: 0

0

Sample Input 2:

abacabad

Sample Output 2:

4 14

a: 0

b: 10

c: 110

d: 111

01001100100111

decode.py, in turn, restores the string by its prefix-free code. The first input line provides k difrerent letters that are in the string and l, the encoded string's length. The next k lines provide letters' prefix-free codes in "letter: code" format (the letters can go in arbitrary order).
The output line shows string s.

Sample Input 1:

1 1

a: 0

0

Sample Output 1:

a

Sample Input 2:

4 14

a: 0

b: 10

c: 110

d: 111

01001100100111

Sample Output 2:

abacabad