finalizando trab 1

This commit is contained in:
José Henrique 2023-08-26 12:35:20 -03:00
parent 218aca1a6c
commit d05fe72d17
2 changed files with 45 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)
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