-
Notifications
You must be signed in to change notification settings - Fork 423
/
Copy pathencode_and_decode.py
37 lines (30 loc) · 958 Bytes
/
encode_and_decode.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
def encode(strs):
res = ""
for s in strs:
res += str(len(s)) + "#" + s
return res
def decode(s):
res = []
i = 0
while i < len(s):
j = i
while s[j] != '#':
j += 1
length = int(s[i:j])
i = j + 1
j = i + length
res.append(s[i:j])
i = j
return ''.join(res)
action = input("Do you want to encode or decode, Press 'e' to encode and 'd' to decode: ")
while action != 'e' and action != 'd':
action = input("Do you want to encode or decode, Press 'e' to encode and 'd' to decode: ")
print('Your action is', action)
if action == 'e':
string = input("Enter the string you wish to encode: ")
print('string is', string)
print("Encoded string is",encode(string))
elif action == 'd':
string = input("Enter the string you wish to decode: ")
print('string is', string)
print("Decoded string is", decode(string))