I was having fun solving the riddles from the Pythonchallenge website
when I stumbled upon a weird behaviour:
With this input: *g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.
We should be able to get the following output: *"i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url." *
Instead what we get when we decipher it with a simple ROT2 script is: i hope you didnt tr{nsl{te it |y h{nd0 th{ts wh{t computers {re for0 doing it in |y h{nd is inefficient {nd th{t)s why this text is so long0 using string0m{ketr{ns+ is recommended0 now {pply on the url0*
My ROT2 script I refer to is as follows:
user_input = input().split(' ')
newletter_int = 0
new_output = []
for word in user_input:
newletter_int = 0
newstr = ''
for letter in word:
newletter = ord(letter) + 2
newstr += chr(newletter)
new_output.append(newstr)
print(" ".join(new_output))
This naturally happens of course, because the letter "y" has the order number of 121 and when we add 2 to 121 we get the character with order number 123 which is "{". But why would then the Python maketrans result in the correct character?
Please, note that I have solved the task with maketrans and what I am looking for is not a solution to the riddle, as I have been able to find it out myself. I am looking for a simple explanation what is the difference between the two methods. Also, please, do not refer to the pages where the solution is linked, as I am not looking for them, but for explanation of the difference of the functionalities between my script above and the string.maketrans() method and also an answer to the question why is this the recommended way to solve the riddle.