finalizando traablho 1

This commit is contained in:
José Ivanchechen 2023-08-25 22:13:45 +01:00
parent 218aca1a6c
commit 683d4356c3
2 changed files with 23 additions and 25 deletions

View File

@ -38,9 +38,9 @@ frequencias.reverse()
caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
def afreq(string): def afreq(string):
# determinar a frequencia de cada caractere na string # determina a frequencia de cada caractere na string
# comparar com a frequencia de cada caractere na lingua portuguesa # compara com a frequencia de cada caractere na lingua portuguesa
# calcular a diferenca entre as frequencias # calcula a diferenca entre as frequencias
str_freq = {} str_freq = {}
for c in string: for c in string:
@ -56,9 +56,6 @@ def afreq(string):
freq_sorted = [x[0] for x in freq_sorted] freq_sorted = [x[0] for x in freq_sorted]
freq_sorted.reverse() freq_sorted.reverse()
print(freq_sorted[0], frequencias[0])
print(freq_sorted[1], frequencias[1])
possiveis_chaves = [] possiveis_chaves = []
for i, c in enumerate(frequencias): for i, c in enumerate(frequencias):
@ -68,7 +65,7 @@ def afreq(string):
index_c = caracteres.index(c) index_c = caracteres.index(c)
index_freq = caracteres.index(freq_sorted[i]) index_freq = caracteres.index(freq_sorted[i])
print(c, index_c, index_freq) # print(c, index_c, index_freq)
possiveis_chaves.append((index_freq - index_c) % 26) # (indice da freq da letra na str - indice da letra em PT) % ignorar letras maiusculas possiveis_chaves.append((index_freq - index_c) % 26) # (indice da freq da letra na str - indice da letra em PT) % ignorar letras maiusculas

View File

@ -56,25 +56,26 @@ def cesar(string, key):
new_index = (index + key) % len(caracteres) new_index = (index + key) % len(caracteres)
new_letter = caracteres[new_index] 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 new_string += new_letter
return new_string return new_string
print(cesar(string, key)) 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