Skip to content

Instantly share code, notes, and snippets.

@westscz
Created February 6, 2020 11:48
Show Gist options
  • Save westscz/3f3034dcf7eb04d732b93a0f2229b24b to your computer and use it in GitHub Desktop.
Save westscz/3f3034dcf7eb04d732b93a0f2229b24b to your computer and use it in GitHub Desktop.
def str_to_ascii(s):
chars_to_ascii = [ord(e) for e in s]
reversed_ascii = chars_to_ascii[::-1] #or reversed(chars_to_ascii)
map_to_str = map(lambda x:str(x), reversed_ascii) #wymagane przez join aby kady element był stringiem
return "".join(map_to_str)
def ascii_to_str(s):
result = ""
while s: #póki nie przetworzymy wszystkiego
number_length = 3 if s[0]=='1' else 2 #sprawdzamy czy będzie cyfra 1XX czy 3X, 6-9X
ascii_number = s[:number_length]
char = chr(int(ascii_number))
result+=char
s = s[number_length:] #wycinamy przetworzona liczbe
reversed_str = "".join(reversed(result))
return reversed_str
assert ascii_to_str(str_to_ascii('hacker rank')) == 'hacker rank'
s = 'hacker rank'
to_ascii = str_to_ascii(s)
print(to_ascii)
to_str = ascii_to_str(to_ascii)
print(to_str)
@westscz
Copy link
Author

westscz commented Feb 9, 2020

test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment