OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

ROT2 cipher resulting in different than expected characters when deciphered with Python

  • Thread starter Thread starter apingaway
  • Start date Start date
A

apingaway

Guest
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:

Code:
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.
<p>I was having fun solving the riddles from the <a href="http://www.pythonchallenge.com/pc/def/map.html" rel="nofollow noreferrer">Pythonchallenge</a> website</p>
<p>when I stumbled upon a weird behaviour:</p>
<p>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.</p>
<p>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."
*</p>
<p>Instead what we get when we decipher it with a simple ROT2 script is:
<em>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</em>+ is recommended0 now {pply on the url0*</p>
<p>My ROT2 script I refer to is as <a href="https://pythontutor.com/visualize.h... lmu ynnjw ml rfc spj."]&textReferences=false" rel="nofollow noreferrer">follows</a>:</p>
<pre><code>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))
</code></pre>
<p>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?</p>
<p>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.</p>
 

Latest posts

D
Replies
0
Views
1
Domantas Giedraitis
D
Top