+ 4
Handling non alphabetic characters
How could this code be enhanced to handle non-alphabetic characters assigning a special value https://code.sololearn.com/cC2SBRhGO7iD/?ref=app
6 ответов
+ 4
Izaiah Kay if you enter the help(string) command, python will kindly provide you with all the information about string, including the ASCII – string.digits module of interest, or all characters in general – string.printable...😎
+ 3
Bob_Li I’ve been cracking my head around this for sometime . I guess i just have to rewrite it in a different way
+ 3
def encode(s):
return ''.join([f'{ord(x):04}' for x in s])
def decode(s):
res = ''
for n in range(0,len(s),4):
res += chr(int(s[n:n+4]))
return res
txt = input()
print(f'your input: {txt}')
secret = encode(txt)
print(secret)
reveal = decode(secret)
print(reveal)
+ 3
Bob_Li this is great
+ 2
The way you encoded it makes it impossible to decode...
+ 2
maybe zero pad single digits. Then you could decode it by taking two-digit numbers.
As for non-alphabetic characters, you could perhaps just encode the string by converting each letter to their ascii numbers instead? you have to use 3 or 4 digit groups, though. The encoded form would be very long.