-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment.py
More file actions
51 lines (48 loc) · 1.33 KB
/
Assignment.py
File metadata and controls
51 lines (48 loc) · 1.33 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
#creating a dictionary to mapping opposite character
h={'A':'Z','B':'Y','C':'X','D':'W','E':'V','F':'U','G':'T','H':'S','I':'R','J':'Q','K':'P','L':'O','M':'N'}
h1={'Z': 'A', 'Y': 'B', 'X': 'C', 'W': 'D', 'V': 'E', 'U': 'F', 'T': 'G', 'S': 'H', 'R': 'I', 'Q': 'J', 'P': 'K', 'O': 'L', 'N': 'M'}
# for encoding
def encoding(s):
res = '' # to store the answer
for i in s:
if ord(i) % 2 == 1: # ASCII value if odd
if i in h.keys(): #
res += h[i]
else:
res += h1[i]
else:
res += i
if i in h.keys():
res += h[i]
else:
res += h1[i]
print(res)
# for decoding
def decoding(s):
for i in range(len(s) - 1):
if (ord(s[i]) % 2) == 1:
s = s.replace(s[i - 1], '')
res = ''
for i in range(len(s)):
if ord(s[i]) % 2 == 0:
if s[i] in h.keys():
res += h[s[i]]
else:
res += h1[s[i]]
else:
if s[i] in h.keys():
res += h[s[i]]
else:
res += h1[s[i]]
print(res)
# main function
print("Enter String:")
s=input()
print("choose \n 1.encode 2.decode")
t=int(input())
if t==1:
encoding(s)
elif t==2:
decoding(s)
else:
print('wrong choice')