Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Huffman code size
# Calculate the size of a Huffman-coded message # Input: list of alphabet letters' probabilities # input message size set probs ".5 .25 .15 .1" set size 1000 set sum 0; lmap x $probs {set sum [expr $sum+$x]} if {abs($sum-1)>1e-5} {puts "Error: sum of probabilities is not 1"; exit} for {set a $probs; set bits 0} {[llength $a]>1} {} { set a [lsort -r $a] set a [lreplace $a 0 1 [expr [lindex $a 0]+[lindex $a 1]]] set bits [expr $bits+[lindex $a 0]] } puts "Encoding [llength $probs] letters with probabilities [join $probs ,\ ] using Huffman code would take $bits bits per letter." puts "Encoding message of length $size would take [expr $size*$bits/8] bytes."
run
|
edit
|
history
|
help
0
1
wewew
Dec to binary addition
r
Longest common subsequence
NISP - Planning ID/Test ID
NISP - Planning ID Unit Test
NISP - Wheel Movements 0.1
3
dssd