Is there a simpler way to write this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is there a simpler way to write this code?

I apologise for the spoiler if you've not tried the secret message code master problem. I wrote this to solve the secret message problem, it works perfectly but I can't help thinking there's an easier way to solve this, probably using regular expressions(import re). If you have a smarter way of solving the problem, please help me out. https://code.sololearn.com/cxamH2B926gD/?ref=app

5th Sep 2021, 6:02 PM
Akinlade Babatunde
Akinlade Babatunde - avatar
10 Answers
+ 4
Here's one import string str=input().lower() print(''.join([string.ascii_lowercase[25-ord(i)%97] if i != ' ' else ' ' for i in str]))
5th Sep 2021, 6:07 PM
Hima
Hima - avatar
+ 5
x = input().lower() abc = 'abcdefghijklmnopqrstuvwxyz' d = dict(zip(abc, abc[::-1])) print("".join(d.get(s, s) for s in x))
5th Sep 2021, 8:29 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 3
Here is another one: import string letters = list(string.ascii_lowercase) d = dict(zip(letters,letters[::-1])) m = input().lower() r = "" for l in m: if (l != " "): r += (d[l]) else: r += " " print(r)
5th Sep 2021, 7:28 PM
Coding Cat
Coding Cat - avatar
+ 3
#what about this one? alpha="abcdefghijklmnopqrstuvwxyz" table=str.maketrans(alpha,alpha[::-1]) text=input().lower() print(text.translate(table))
7th Sep 2021, 1:35 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 1
Sousou your code gives error for white space, it's missing a " if i != ' ' else ' ' " statement just before the for loop in the print argument
6th Sep 2021, 11:45 AM
Akinlade Babatunde
Akinlade Babatunde - avatar
+ 1
Akinlade Babatunde , Ty for info, I didn't notice that . Code updated :)
6th Sep 2021, 2:47 PM
Sousou
Sousou - avatar
+ 1
Ratnapal Shende very nice solution! Wish I could double upvote this 👍
7th Sep 2021, 2:51 PM
Coding Cat
Coding Cat - avatar
+ 1
Coding Cat [Mouse searching] Thank you so much! ☺️🎊
8th Sep 2021, 10:50 AM
Ratnapal Shende
Ratnapal Shende - avatar
- 4
Ndkeodjrrneodo
6th Sep 2021, 3:49 PM
mahdi rahimi