Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding and decoding done #518

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions encode_and_decode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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))