-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.py
More file actions
49 lines (37 loc) · 1.39 KB
/
Copy pathcipher.py
File metadata and controls
49 lines (37 loc) · 1.39 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
def encode(char, shift, operation):
if char.isdigit():
offset = ord(char) - ord('0')
if operation == "encode":
shifted = (offset + shift) % 10
elif operation == "decode":
shifted = (offset - shift) % 10
else:
raise ValueError("Invalid Operation!")
return chr(ord('0') + shifted)
elif char.isalpha():
base = ord('A') if char.isupper() else ord('a')
offset = ord(char) - base
if operation == "encode":
shifted = (offset + shift) % 26
elif operation == "decode":
shifted = (offset - shift) % 26
else:
raise ValueError("Invalid Operation!")
return chr(base + shifted)
else:
return char
def encodeOrDecodeString(stringToProcess, shift, operation):
newString = ""
for x in range(0, len(stringToProcess)):
processedChar = encode(stringToProcess[x], shift, operation)
newString += processedChar
return newString
def main():
operation = str(input("Encode or Decode: "))
assert operation.lower() == "encode" or operation.lower() == "decode", "Invalid operation!"
stringToProcess = str(input("Enter String: "))
shift = input("Enter shift number: ")
assert shift.isdigit(), NaN
print(encodeOrDecodeString(stringToProcess, shift, operation))
if __name__ == "__main__":
main()