finalizando trab 1

This commit is contained in:
José Henrique 2023-08-26 12:34:05 -03:00
parent 218aca1a6c
commit 659bd90c72
3 changed files with 46 additions and 43 deletions

View File

@ -1,4 +1,5 @@
import sys
import cesar
if len(sys.argv) < 2:
print("Usage: python3 afreq.py [arquivo]")
@ -38,10 +39,6 @@ frequencias.reverse()
caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
def afreq(string):
# determinar a frequencia de cada caractere na string
# comparar com a frequencia de cada caractere na lingua portuguesa
# calcular a diferenca entre as frequencias
str_freq = {}
for c in string:
if not c.isalnum():
@ -55,9 +52,7 @@ def afreq(string):
freq_sorted = sorted(str_freq.items(), key=lambda item: item[1])
freq_sorted = [x[0] for x in freq_sorted]
freq_sorted.reverse()
print(freq_sorted[0], frequencias[0])
print(freq_sorted[1], frequencias[1])
most_freq = freq_sorted[0]
possiveis_chaves = []
@ -66,15 +61,18 @@ def afreq(string):
continue
index_c = caracteres.index(c)
index_freq = caracteres.index(freq_sorted[i])
print(c, index_c, index_freq)
index_freq = caracteres.index(most_freq)
possiveis_chaves.append((index_freq - index_c) % 26) # (indice da freq da letra na str - indice da letra em PT) % ignorar letras maiusculas
print(freq_sorted[0], c + ' : ' + str(possiveis_chaves[-1]))
return possiveis_chaves
file_str = open(sys.argv[1], 'r').read()
possiveis_chaves = afreq(file_str)
print(possiveis_chaves)
for key in possiveis_chaves:
print(str(key) + ' : ' + cesar.cesar(file_str, -key))

View File

@ -1,19 +1,5 @@
import sys
if len(sys.argv) != 4:
print("Usage: python3 cesar.py -c/-d [key] [string]")
sys.exit(1)
encrypt = True
if sys.argv[1] == "-d":
encrypt = False
key = int(sys.argv[2])
string = sys.argv[3]
if not encrypt:
key *= -1
special_letters = {
"ã": "a",
"á": "a",
@ -52,10 +38,34 @@ def cesar(string, key):
if letter in special_letters:
letter = special_letters[letter]
if letter not in caracteres:
continue
index = caracteres.index(letter)
new_index = (index + key) % len(caracteres)
new_letter = caracteres[new_index]
new_string += new_letter
return new_string
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: python3 cesar.py -c/-d [key] [string]")
sys.exit(1)
encrypt = True
if sys.argv[1] == "-d":
encrypt = False
key = int(sys.argv[2])
string = sys.argv[3]
if not encrypt:
key *= -1
print(cesar(string, key))
# # [A-Z, a-z, 0-9]
# if ord('0') <= ord(letter) <= ord('9'):
# if new_letter > ord('9'):
@ -72,9 +82,3 @@ def cesar(string, key):
# new_letter -= 75 # z + 1 = 0
# elif new_letter < ord('a'):
# new_letter -= 6 # a - 1 = Z
new_string += new_letter
return new_string
print(cesar(string, key))

1
trabalho1/texto1.txt Normal file
View File

@ -0,0 +1 @@
0EFP FP 7 P7JMIB QBUQ CLO 9E7O79QBO COBNRBK9V 7K7IVPFP.