fork download
  1. import math
  2.  
  3. # numbers from http://w...content-available-to-author-only...h.net/puzzles/cube2.htm
  4. s = "1 9 54 321 1,847 9,992 50,136 227,536 870,072 1,887,748 623,800 2,644"
  5. nums = map(int, s.replace(',', '').split())
  6.  
  7. freqs = [0] * 3
  8. d = 0
  9. for s in nums:
  10. freqs[d % 3] += s
  11. print(d, s)
  12. d += 1
  13. print(sum(freqs), freqs)
  14.  
  15. arithmetic = huffman = 0
  16. for i in range(3):
  17. prob = freqs[i] / sum(freqs)
  18. print('probability for mod-3-distance', i, 'is', prob)
  19. arithmetic += -math.log(prob, 2) * prob
  20. huffman += (1 + (freqs[i] < max(freqs))) * prob
  21. print('arithmetic:', arithmetic, 'bits per state')
  22. print('huffman: ', huffman, 'bits per state')
  23.  
Success #stdin #stdout 0.16s 10256KB
stdin
Standard input is empty
stdout
0 1
1 9
2 54
3 321
4 1847
5 9992
6 50136
7 227536
8 870072
9 1887748
10 623800
11 2644
3674160 [1938206, 853192, 882762]
probability for mod-3-distance 0 is 0.527523570013282
probability for mod-3-distance 1 is 0.23221416595902192
probability for mod-3-distance 2 is 0.24026226402769613
arithmetic: 1.4701906119602757 bits per state
huffman:    1.4724764299867181 bits per state