Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
13
Task/Huffman-coding/Mathematica/huffman-coding.math
Normal file
13
Task/Huffman-coding/Mathematica/huffman-coding.math
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
huffman[s_String] := huffman[Characters[s]];
|
||||
huffman[l_List] := Module[{merge, structure, rules},
|
||||
|
||||
(*merge front two branches. list is assumed to be sorted*)
|
||||
merge[k_] := Replace[k, {{a_, aC_}, {b_, bC_}, rest___} :> {{{a, b}, aC + bC}, rest}];
|
||||
|
||||
structure = FixedPoint[
|
||||
Composition[merge, SortBy[#, Last] &],
|
||||
Tally[l]][[1, 1]];
|
||||
|
||||
rules = (# -> Flatten[Position[structure, #] - 1]) & /@ DeleteDuplicates[l];
|
||||
|
||||
{Flatten[l /. rules], rules}];
|
||||
Loading…
Add table
Add a link
Reference in a new issue