-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crypto.py
109 lines (76 loc) · 3.14 KB
/
Crypto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import re
from collections import Counter
def clean_up_string(input_str):
"""Remove all non-alphanumeric characters from a string."""
return re.sub('[^a-zA-Z]+', '', input_str.upper())
def sort_key(row):
"""
Custom sort key function for the sorted() function.
Args:
row: A list representing a row in the array.
Returns:
A tuple containing the first element of the row and the second element.
"""
return (row[0], row[1].upper())
def count_words(input_str):
"""Count the number of words in a string."""
results = []
char_counts = Counter(input_str)
for char, count in char_counts.items():
results.append([count, char])
# results = sorted(results, key=lambda x: x[0], reverse=True)
results = sorted(results, key=sort_key, reverse=True)
return results
# for result in results:
# print(result[1], result[0])
def read_file_to_string(filename):
"""Reads the contents of a file into a string.
Args:
filename (str): The name of the file to read.
Returns:
str: The entire contents of the file as a string.
"""
with open(filename, 'r') as file:
text = file.read()
return text
def find_char_position_in_first_column(array, char_to_find):
"""Finds the position (row index) of the first occurrence of the given character in the first column of the 2D array.
Args:
array: The 2D array to search.
char_to_find: The character to find.
Returns:
The row index of the first occurrence of the character, or -1 if not found.
"""
for i, row in enumerate(array):
if row[1] == char_to_find:
return i
return -1
if __name__ == "__main__":
# input_str = "Helloabcdefghijklmnopqrstuvwxyz99832749287World!"
input_str = clean_up_string(read_file_to_string("646053.txt"))
ciphertext = "PBFPVYFBQXZTYFPBFEQJHDXXQVAPTPQJKTOYQWIPBVWLXTOXBTFXQWAXBVCXQWAXFQJVWLEQNTOZQGGQLFXQWAKVWLXQWAEBIPBFXFQVXGTVJVWLBTPQWAEBFPBFHCVLXBQUFEVWLXGDPEQVPQGVPPBFTIXPFHXZHVFAGFOTHFEFBQUFTDHZBQPOTHXTYFTODXQHFTDPTOGHFQPBQWAQJJTODXQHFOQPWTBDHHIXQVAPBFZQHCFWPFHPBFIPBQWKFABVYYDZBOTHPBQPQJTQOTOGHFQAPBFEQJHDXXQVAVXEBQPEFZBVFOJIWFFACFCCFHQWAUVWFLQHGFXVAFXQHFUFHILTTAVWAFFAWTEVOITDHFHFQAITIXPFHXAFQHEFZQWGFLVWPTOFFA"
ciphertext = clean_up_string(ciphertext)
# print(count_words(clean_up_string(input_str)))
# print(count_words(clean_up_string(ciphertext)))
key_frq = count_words(input_str)
ciphertext_frq = count_words(ciphertext)
# for text in key_frq:
# if text != None:
# print(text[1])
# print("=============================================")
# for text in ciphertext_frq:
# if text != None:
# print(text[1])
# i = 0
# print(i)
# print(len(key_frq))
# print(len(ciphertext_frq))
# for text1, text2 in zip(key_frq, ciphertext_frq):
# print(text1[1]+" = " + text2[1])
# i += 1
print("=============================================")
new_string = ""
for c in ciphertext:
position = find_char_position_in_first_column(ciphertext_frq, c)
new_string += key_frq[position][1]
print(new_string)