Repository navigation
Expand file tree
/
Copy pathsymbol_replacements.py
More file actions
90 lines (74 loc) · 2.09 KB
/
Copy pathsymbol_replacements.py
File metadata and controls
90 lines (74 loc) · 2.09 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
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
import sys
from bidict import bidict
# Implement character replacement from
# Crack the Code: the History of Spy Codes and Ciphers, pg. 17
symbol_dict = bidict(
{
"a": "!",
"b": "@",
"c": "#",
"d": "$",
"e": "%",
"f": "^",
"g": "&",
"h": "*",
"i": "(",
"j": ")",
"k": "_",
"l": "+",
"m": "{",
"n": "}",
"o": "|",
"p": "\\",
"q": ":",
"r": ";",
"s": "'",
"t": '"',
"u": "<",
"v": ">",
"w": "`",
"x": "/",
"y": "?",
"z": "~",
# Add spaces so they can be part of the message
# This is the lazy implementations
" ": " ",
}
)
def symbol_replace(text):
"""
Replace symbols with letters and vis-versa based on a defined list.
Right now the function assumes that the text passed will be decodable.
Args:
text (string): string of text or symbols to be encoded/decoded
Returns:
string: the encoded or decoded message
"""
result = ""
for char in text:
# Only shift characters in symbol_dict
char_lower = char.lower()
if char_lower in symbol_dict.keys():
# convert char to symbol
symbol_temp = symbol_dict[char_lower]
result += str(symbol_temp)
elif char_lower in symbol_dict.values():
# Keep non-alphabetic characters unchanged
result += symbol_dict.inverse[char_lower]
# Pass character back to output if its not in the dict
else:
result += char_lower
return result
def main():
# Check if the right number of arguments have been passed
# if len(sys.argv) != 2:
# print("The text to decode/encode must be passed")
# sys.exit(1)
text = input("enter text to be encoded/decoded: ")
# text = fr"{sys.argv[1]}"
# Process the text and display the result
result = symbol_replace(text)
print(f"Original: {text}")
print(f"Result: {result}")
if __name__ == "__main__":
main()