-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_cipher.py
More file actions
50 lines (35 loc) · 1.46 KB
/
list_cipher.py
File metadata and controls
50 lines (35 loc) · 1.46 KB
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
"""A module to encode secret messages into null ciphers using word lists."""
import random
def load_file(file_name: str) -> list:
"""
Load a text file and return a list of lowercase, stripped strings.
Args:
file_name (str): The path to the dictionary file.
Returns:
list: A list of words from the file.
"""
with open(file_name, "r", encoding="utf-8") as file:
content = file.read().split("\n")
content = [x.lower().strip() for x in content]
return content
def write_null_cipher(secret_message: str, dict_path: str, key: int) -> list:
"""Generate a list of words where the nth letter of each word spells out the secret message."""
content = load_file(dict_path)
secret_message = secret_message.lower().replace(" ", "")
usable_words = [w for w in content if len(w) >= key]
message_list = []
for char in secret_message:
choices = [w for w in usable_words if w[key - 1] == char]
if choices:
message_list.append(random.choice(choices))
else:
message_list.append(f"MISSING_WORD_FOR_{char.upper()}")
# print(f"len list: {len(message_list)}")
# print(f"len secret_message: {len(secret_message)}")
return message_list
def main():
"""Execute the null cipher encoding function."""
secret_message = "Panel at east end of chapel slides"
print(write_null_cipher(secret_message, "dict.txt", 3))
if __name__ == "__main__":
main()