+ 4

Can this be made simpler?

This code passed the code coach but was wondering if there was an easier/different way of doing it. https://sololearn.com/compiler-playground/c2D0h05YuYvB/?ref=app

2nd Apr 2024, 11:55 AM
lee harding
lee harding - avatar
11 Respuestas
+ 4
Your approach with maketrans is a good generalization of the mapping problem, that could even work with more advanced cryptogtaphic scenarios. but the way you manually construct the dictionary, is pretty ugly. In this particular task, the following code works too: print(''.join(map(lambda c: chr(219-ord(c)), message))) What does it do? - ord gives the position of each character in the Ascii table - 219 comes from the position of z (122) and a (97) added - subtracting the ord of the actual character I achieve the reverse of the alphabet with a simple math operation - chr converts the number back to character - combine all letters back to string with join
2nd Apr 2024, 4:24 PM
Tibor Santa
Tibor Santa - avatar
+ 4
Tibor Santa even simpler, no map or lambda. Just use ''.join(generator expression) #encode coded = ''.join(chr(219-ord(c)) for c in input().lower()) print(coded) #decode decoded = ''.join(chr(219-ord(c)) for c in coded) print(decoded)
3rd Apr 2024, 3:35 AM
Bob_Li
Bob_Li - avatar
+ 4
Just another idea: You could check if ord(c) is in the valid range and if not, leave it "untranslated". It's just a little add-on, but makes the code more robust.
3rd Apr 2024, 9:42 AM
Lisa
Lisa - avatar
+ 3
Interesting. How about replacing line 3 with the code below? mapping_table = dict() # lowercase for i in range(26): mapping_table[chr(97 + i)] = chr(97 + 25 - i) # uppercase for i in range(26): mapping_table[chr(65 + i)] = chr(65 + 25 - i) Or with these lines of code? import string mapping_table= dict() # lowercase for i in range(26): mapping_table[string.ascii_lowercase[i]] = string.ascii_lowercase[::-1][i] # uppercase for i in range(26): mapping_table[string.ascii_uppercase[i]] = string.ascii_uppercase[::-1][i]
2nd Apr 2024, 2:10 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 3
I just learned str.maketrans is intended to work with str.translate. It is a little code. Just for fun. https://sololearn.com/compiler-playground/cTvvnXm7Xl6U/?ref=app
2nd Apr 2024, 5:45 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 3
Bob_Li nice :) I am getting used to other forms of looping too much and "for" is getting erased from my vocabulary :)
3rd Apr 2024, 3:49 AM
Tibor Santa
Tibor Santa - avatar
+ 2
lee harding something like this: challenge: can coded snd decoded be written as one liners? https://sololearn.com/compiler-playground/cRcaJH6qgIhO/?ref=app
4th Apr 2024, 2:07 AM
Bob_Li
Bob_Li - avatar
+ 1
Thank you for taking the time to reply, being a beginner, any advice is much appreciated. You're right in saying my code is.... on the noob side, it was a pain typing it all out. I'll have to do some research into both of your answers as I'm unfamiliar with either at the moment.
2nd Apr 2024, 9:44 PM
lee harding
lee harding - avatar
+ 1
Thank you for the responses everyone. After researching what all of you've said I can safely say I know a bit more than yesterday. I've got 1 question though, how would you stop it translating any white space in the input string if using ord() chr() method?
4th Apr 2024, 1:25 AM
lee harding
lee harding - avatar
+ 1
lee harding you can add an if statement.
4th Apr 2024, 1:26 AM
Bob_Li
Bob_Li - avatar
+ 1
I see, I tried that but couldn't get it to work. I'll keep playing but I may end up coming back to you!
4th Apr 2024, 1:38 AM
lee harding
lee harding - avatar