example for for computing letter frequency

This commit is contained in:
Simon Pirkelmann 2020-01-28 18:42:57 +01:00
parent 8639088565
commit a93eb46871
2 changed files with 28 additions and 0 deletions

1
code/ciphertext.txt Normal file
View File

@ -0,0 +1 @@
Tg stgsf Djia tf Ejnsg, nm dseqs stg Ajeetq. Gtiaq tg stgsf csriaqsg, piafrqythsg Djia, vj sp gmia Fjnso otsiaq rgn Vrofytkcsd ujg nsg Vmsgnsg asomeamsghsg, rgn mria gtiaq tg stgso qojibsgsg, bmadsg Pmgnhores jags Qtpias rgn Pqrsads, vj fmg ptia yrf Sppsg atgpsqysg bjsggqs: gstg, nmp Djia vmo stgs Ajeetqajsads, rgn nmp astppq, sp vmo psao bjfcjoqmesd.

27
code/frequency.py Normal file
View File

@ -0,0 +1,27 @@
import matplotlib.pyplot as plt
f = open('ciphertext.txt')
ciphertext = f.readline().upper()
letter_count = {}
letters = [chr(i) for i in range(ord('A'), ord('Z')+1)]
for c in letters:
letter_count[c] = 0
for c in ciphertext:
if c in letter_count.keys():
letter_count[c] += 1
print(letter_count)
x = range(0,26)
y = [letter_count[chr(ord('A')+i)] for i in range(0,26)]
plt.bar(x, y)
plt.xticks(x, letters) # Set locations and labels
plt.show()