seg/trabalho1/afreq.py

78 lines
1.7 KiB
Python
Raw Normal View History

2023-08-23 12:38:41 +00:00
import sys
2023-08-26 15:35:20 +00:00
import cesar
2023-08-23 12:38:41 +00:00
if len(sys.argv) < 2:
print("Usage: python3 afreq.py [arquivo]")
sys.exit(1)
2023-08-22 16:29:32 +00:00
frequencias = {
'A': 14.63,
'B': 1.04,
'C': 3.88,
'D': 4.99,
'E': 12.57,
'F': 1.02,
'G': 1.30,
'H': 1.28,
'I': 6.18,
'J': 0.40,
'K': 0.02,
'L': 2.78,
'M': 4.74,
'N': 5.05,
'O': 10.73,
'P': 2.52,
'Q': 1.20,
'R': 6.53,
'S': 7.81,
'T': 4.34,
'U': 4.63,
'V': 1.67,
'W': 0.01,
'X': 0.21,
'Y': 0.01,
'Z': 0.47
}
2023-08-23 12:38:41 +00:00
frequencias = sorted(frequencias.keys(), key=lambda x: frequencias[x])
frequencias.reverse()
caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
2023-08-22 16:29:32 +00:00
def afreq(string):
str_freq = {}
for c in string:
if not c.isalnum():
continue
if c in str_freq:
str_freq[c] += 1
else:
str_freq[c] = 1
2023-08-23 12:38:41 +00:00
freq_sorted = sorted(str_freq.items(), key=lambda item: item[1])
freq_sorted = [x[0] for x in freq_sorted]
freq_sorted.reverse()
2023-08-26 15:35:20 +00:00
most_freq = freq_sorted[0]
2023-08-23 12:38:41 +00:00
possiveis_chaves = []
for i, c in enumerate(frequencias):
if c not in freq_sorted:
2023-08-22 16:29:32 +00:00
continue
2023-08-23 12:38:41 +00:00
index_c = caracteres.index(c)
2023-08-26 15:35:20 +00:00
index_freq = caracteres.index(most_freq)
2023-08-22 16:29:32 +00:00
2023-08-23 12:38:41 +00:00
possiveis_chaves.append((index_freq - index_c) % 26) # (indice da freq da letra na str - indice da letra em PT) % ignorar letras maiusculas
2023-08-22 16:29:32 +00:00
2023-08-26 15:35:20 +00:00
print(freq_sorted[0], c + ' : ' + str(possiveis_chaves[-1]))
2023-08-22 16:29:32 +00:00
return possiveis_chaves
2023-08-23 12:38:41 +00:00
file_str = open(sys.argv[1], 'r').read()
possiveis_chaves = afreq(file_str)
2023-08-26 15:35:20 +00:00
print(possiveis_chaves)
for key in possiveis_chaves:
print(str(key) + ' : ' + cesar.cesar(file_str, -key))