Skip to content

Instantly share code, notes, and snippets.

@RaminMammadzada
Created April 12, 2020 07:42
Show Gist options
  • Save RaminMammadzada/de98c42a0f1ad35f0391efc6f6f164e0 to your computer and use it in GitHub Desktop.
Save RaminMammadzada/de98c42a0f1ad35f0391efc6f6f164e0 to your computer and use it in GitHub Desktop.
The frequencies of the letter in the text
"""
The following English text was encrypted using a substitution cipher that shuffles the alphabet rather than just shifting it.
Can you find what the following letters l and h stand for in the following text ?
hm al, mo tmh hm al, huvh gn hul jzlnhgmt:
qulhulo 'hgn tmaxlo gt hul cgty hm nzrrlo
hul nxgtsn vty voomqn mr mzhovslmzn rmohztl,
mo hm hvel vocn vsvgtnh v nlv mr homzaxln
vty ak mppmngts lty hulc?
hm ygl: hm nxllp;
tm cmol; vty, ak v nxllp hm nvk ql lty
hul ulvoh-vwul vty hul humznvty tvhzovx numwen
huvh rxlnu gn ulgo hm, 'hgn v wmtnzccvhgmt
yldmzhxk hm al qgnu'y. hm ygl, hm nxllp;
hm nxllp: plowuvtwl hm yolvc: vk, hulol'n hul oza;
rmo gt huvh nxllp mr ylvhu quvh yolvcn cvk wmcl
qult ql uvdl nuzrrxly mrr hugn cmohvx wmgx,
cznh sgdl zn pvznl
"""
from collections import OrderedDict
def findLetterFrequency(ciphertext):
frequencies = {}
for letter in ciphertext:
if( frequencies.get(letter) == None ):
frequencies[letter] = 1
else:
frequencies[letter] += 1
frequencies = OrderedDict(sorted(frequencies.items(), key=lambda x: x[1]))
# print all letter frequencies
for letter, frequency in frequencies.items():
print(letter + ": " + str(frequency))
ciphertext = "hm al, mo tmh hm al, huvh gn hul jzlnhgmt:" \
"qulhulo 'hgn tmaxlo gt hul cgty hm nzrrlo" \
"hul nxgtsn vty voomqn mr mzhovslmzn rmohztl," \
"mo hm hvel vocn vsvgtnh v nlv mr homzaxln" \
"vty ak mppmngts lty hulc?" \
"hm ygl: hm nxllp;" \
"tm cmol; vty, ak v nxllp hm nvk ql lty" \
"hul ulvoh-vwul vty hul humznvty tvhzovx numwen" \
"huvh rxlnu gn ulgo hm, 'hgn v wmtnzccvhgmt" \
"yldmzhxk hm al qgnu'y. hm ygl, hm nxllp;" \
"hm nxllp: plowuvtwl hm yolvc: vk, hulol'n hul oza;" \
"rmo gt huvh nxllp mr ylvhu quvh yolvcn cvk wmcl" \
"qult ql uvdl nuzrrxly mrr hugn cmohvx wmgx," \
"cznh sgdl zn pvznl"
findLetterFrequency(ciphertext)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment