+ 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

14th Jan 2024, 4:53 AM
Izaiah Kay
Izaiah Kay - avatar
6 Antworten
+ 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...😎
14th Jan 2024, 10:02 AM
Solo
Solo - avatar
+ 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
14th Jan 2024, 6:29 AM
Izaiah Kay
Izaiah Kay - avatar
+ 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)
14th Jan 2024, 8:00 AM
Bob_Li
Bob_Li - avatar
+ 3
Bob_Li this is great
14th Jan 2024, 8:02 AM
Izaiah Kay
Izaiah Kay - avatar
+ 2
The way you encoded it makes it impossible to decode...
14th Jan 2024, 6:26 AM
Bob_Li
Bob_Li - avatar
+ 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.
14th Jan 2024, 6:54 AM
Bob_Li
Bob_Li - avatar