From a93eb46871170a26228d0fadc36454b14d3caf37 Mon Sep 17 00:00:00 2001 From: spirkelmann Date: Tue, 28 Jan 2020 18:42:57 +0100 Subject: [PATCH] example for for computing letter frequency --- code/ciphertext.txt | 1 + code/frequency.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 code/ciphertext.txt create mode 100644 code/frequency.py diff --git a/code/ciphertext.txt b/code/ciphertext.txt new file mode 100644 index 0000000..4a4858a --- /dev/null +++ b/code/ciphertext.txt @@ -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. diff --git a/code/frequency.py b/code/frequency.py new file mode 100644 index 0000000..9d2e717 --- /dev/null +++ b/code/frequency.py @@ -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()