initial commit
This commit is contained in:
81
python/freq/main.py
Normal file
81
python/freq/main.py
Normal file
@@ -0,0 +1,81 @@
|
||||
txt = "NTCGPDOPANFLHJINTOOFITOVJHJCTMMHIHEMTCPFDWTSOFSHTOGFWTETTJJTBTOOFSZOVEOCHCVCHPJHOCGTOHNQMTOCNTCGPDCGFCSTQMFBTOFBGFSFBCTSHJCGTQMFHJCTYCXHCGFAHYTDDHAATSTJCBGFSFBCTSHJCGTBHQGTSCTYCCGHONTCGPDQSTOTSWTOCGTMTCCTSASTRVTJBZHJCGTQMFHJCTYCFJDOPPJTBFJOTFSBGAPSCGTQMFHJCTYCASPNFIHWTJBHQGTSCTYCEZBPNQFSHJICGTASTRVTJBZPATFBGMTCCTSFIFHJOCCGTLJPXJBPNNPJASTRVTJBZHJCGTVJDTSMZHJIMFJIVFIT"
|
||||
|
||||
freq_tab = ["E", "T", "A", "I", "N", "O", "S", "R", "L", "D", "H", "C", "U", "M", "F", "P", "Y", "G", "W", "V", "B",
|
||||
"K", "X", "J", "Q", "Z"]
|
||||
|
||||
|
||||
def freq_letter(txt):
|
||||
dic = {}
|
||||
for letter in txt:
|
||||
if letter in dic:
|
||||
dic[letter] += 1
|
||||
else:
|
||||
dic[letter] = 1
|
||||
return dict(sorted(dic.items(), key=lambda item: item[1]))
|
||||
|
||||
|
||||
def freq_m_letter(txt, nbr=2):
|
||||
dic = {}
|
||||
i = 0
|
||||
while i < len(txt) - 1:
|
||||
letter = txt[i:i + nbr]
|
||||
if letter in dic:
|
||||
dic[letter] += 1
|
||||
else:
|
||||
dic[letter] = 1
|
||||
i += 1
|
||||
return dict(sorted(filter(lambda x: x[1] > 2, dic.items()), key=lambda item: item[1]))
|
||||
|
||||
|
||||
def trad(txt_, replace_):
|
||||
sol = ""
|
||||
for letter in txt_:
|
||||
if letter in replace_:
|
||||
sol += replace_[letter]
|
||||
else:
|
||||
sol += letter
|
||||
return sol
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
replace = {"C": "t",
|
||||
"G": "h",
|
||||
"T": "e",
|
||||
"H": "i",
|
||||
"J": "n",
|
||||
"S": "r",
|
||||
"F": "a",
|
||||
"Y": "x",
|
||||
"B": "c",
|
||||
"I": "g",
|
||||
"M": "l",
|
||||
"Q": "p",
|
||||
"V": "u",
|
||||
"A": "f",
|
||||
"R": "q",
|
||||
"Z": "y",
|
||||
"D": "d",
|
||||
"P": "o",
|
||||
"O": "s",
|
||||
"N": "m",
|
||||
"L": "k",
|
||||
"E": "b",
|
||||
"W": "v",
|
||||
"X": "w"
|
||||
}
|
||||
|
||||
freq = freq_letter(txt)
|
||||
print(freq)
|
||||
for i in range(2, 10):
|
||||
print("===")
|
||||
freq_2 = freq_m_letter(txt, i)
|
||||
print(freq_2)
|
||||
print('{', end=" ")
|
||||
for a in freq_2:
|
||||
print(trad(a, replace), end=', ')
|
||||
print('}')
|
||||
|
||||
sol = trad(txt, replace)
|
||||
|
||||
print('====')
|
||||
print(sol)
|
||||
Reference in New Issue
Block a user