Guest User

Untitled

a guest
Jan 21st, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1.  
  2. '''Frequency Analyzer
  3. By Josh Famestad'''
  4.  
  5. def get_count(char, message):
  6. '''(str, str) -> int
  7. Returns the number of ocurrances of char in message
  8.  
  9. >>>get_count('a', 'the cat in the hat')
  10. 2
  11. >>>get_count('b', 'slug')
  12. 0
  13. '''
  14. count = 0
  15. for mchar in message:
  16. if char == mchar:
  17. count = count + 1
  18. return count
  19.  
  20. def analyze(message):
  21. '''(str) -> str
  22. Returns the frequency of each character in the message
  23. >>>analyze('dog')
  24. d 1
  25. o 1
  26. g 1
  27. >>>analyze('ferret')
  28. f 1
  29. e 2
  30. r 2
  31. t 1
  32. '''
  33. result = {}
  34. alphabet = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*'
  35. for char in alphabet:
  36. if char in message:
  37. result[char] = get_count(char, message)
  38. return result
  39.  
  40. '''
  41. candidate for initial assumed order of declining frequency
  42. etaoinshrdlucmfwypvbgkjqxz
  43.  
  44. most common digrams
  45. TH HE AN RE ER IN ON AT ND ST ES EN OF TE ED OR TI HI AS TO
  46.  
  47. most common repeated letters
  48. LL EE SS OO TT FF RR NN PP CC
  49.  
  50. The 'top twelve' letters comprise about 80% of the
  51. total usage. The 'top eight" letters comprise about 65% of the total usage.
  52.  
  53. a 8.167%
  54. b 1.492%
  55. c 2.782%
  56. d 4.253%
  57. e 12.702%
  58. f 2.228%
  59. g 2.015%
  60. h 6.094%
  61. i 6.966%
  62. j 0.153%
  63. k 0.747%
  64. l 4.025%
  65. m 2.406%
  66. n 6.749%
  67. o 7.507%
  68. p 1.929%
  69. q 0.095%
  70. r 5.987%
  71. s 6.327%
  72. t 9.056%
  73. u 2.758%
  74. v 1.037%
  75. w 2.365%
  76. x 0.150%
  77. y 1.974%
  78. z 0.074%
  79. '''
  80.  
  81. '''generate sample values'''
  82.  
  83.  
  84. '''cipher = define_cipher('qwertyuioplkjhgfdsazxcvbnm MNBVCXZQWERTYUIOPLKJHGFDSA')
  85. message = 'hidy ho'
  86. ciphertext = encrypt(message, cipher)
  87. data = analyze(ciphertext)'''
  88.  
  89. def guess(cipher_char, plain_char):
  90. cipher[cipher_char] = plain_char
  91.  
  92. def attempt_get_cipher(ciphertext):
  93. data = analyze(ciphertext)
  94. cipher = sorted(data, key=data.get, reverse=True)
  95. assumed_order = ' etaoinshrdlucmfwypvbgkjqxz'
  96. return dict(zip(cipher,' etaoinshrdlucmfwypvbgkjqxz'))
Add Comment
Please, Sign In to add comment