finalizando trab 1

This commit is contained in:
José Henrique 2023-08-26 12:39:45 -03:00
parent 218aca1a6c
commit af82d8cd38
3 changed files with 47 additions and 46 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,26 +52,25 @@ 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 = []
for i, c in enumerate(frequencias):
if c not in freq_sorted:
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)
possiveis_chaves = afreq(file_str)[:3]
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,29 +38,47 @@ 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]
# # [A-Z, a-z, 0-9]
# if ord('0') <= ord(letter) <= ord('9'):
# if new_letter > ord('9'):
# new_letter += 7 # 9 + 1 = A
# elif new_letter < ord('0'):
# new_letter += 75 # 0 - 1 = z
# elif letter.isupper():
# if new_letter > ord('Z'):
# new_letter += 6 # Z + 1 = a
# elif new_letter < ord('A'):
# new_letter -= 7 # A - 1 = 9
# else:
# if new_letter > ord('z'):
# 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))
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'):
# new_letter += 7 # 9 + 1 = A
# elif new_letter < ord('0'):
# new_letter += 75 # 0 - 1 = z
# elif letter.isupper():
# if new_letter > ord('Z'):
# new_letter += 6 # Z + 1 = a
# elif new_letter < ord('A'):
# new_letter -= 7 # A - 1 = 9
# else:
# if new_letter > ord('z'):
# new_letter -= 75 # z + 1 = 0
# elif new_letter < ord('a'):
# new_letter -= 6 # a - 1 = Z

1
trabalho1/texto1.txt Normal file
View File

@ -0,0 +1 @@
K22sw wo2wy, y sxsmsy nk k3s5snkno qo1kv no py1wkmky no k3s34no2 ksxnk xky nowyx231y4 myx5sxmox3owox3o 04o 5ks zk13smszk1 xk w4nkxmk ny z1ymo22y no myw4xsmkmky mywy 4w 3yny. Xy ox3kx3y, xky zynowy2 o204omo1 04o y xy5y wynovy o2314341kv k04s z1omyxs9kny mk42k swzkm3y sxns1o3y xk 1ok5kvskmky ny 2s23owk no zk13smszkmky qo1kv. Ksxnk k22sw, o7s23ow n45snk2 k 1o2zos3y no mywy k roqowyxsk ny kwlsox3o zyvs3smy 3kv5o9 5oxrk k 1o22kv3k1 k 1ovk3s5snkno no 3yny2 y2 1om412y2 p4xmsyxks2 ox5yv5sny2.