python challenge – 1 – letter mapping Solution Answer P.S.

1

Python challenge 1

From the picture, we can see that
‘K’ -> ‘M’
‘O’ -> ‘Q’
‘E’ -> ‘G’

as a CSer, I assume most of us will come up the solution using ASCii Table. Based on the ASCii Table ‘a’~’z’ => ‘97’~’122’.
107 -> 109
111 -> 113
101 -> 103

Which means, convert the letter to the ASCii code and add 2, and then convert the code back to letter.

  1. The hint is: try to change the URL address.
  2. The current url is: http://www.pythonchallenge.com/pc/def/0.html.

So, we should try to replace the 0 with the answer of the equation.

Solution

str = "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() gqpcamkkclbcb. lmu ynnjw ml rfc spj."
result = []

for l in str:
    if ord(l) in range(97, 123):
        if ord(l) + 2 > 122:
            result.append(chr(97 - 1 + (ord(l)+2) % 122))
        else:
            result.append(chr(ord(l)+2))
    else:    
        result.append(l)

print "".join(result)
# i hope you didnt translate it zy hand. thats what computers are for. doing it in zy hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url.

Using the above code, we can convert the ‘map’ from url to ‘ocr’.

Answer

http://www.pythonchallenge.com/pc/def/ocr.html

P.S.

After we convert the tip, we get ‘using string.maketrans() is recommended’.

import string

a = "abcdefghijklmnopqrstuvwxyz"
b = "cdefghijklmnopqrstuvwxyzab"

table = string.maketrans(a, b)

str.translate(table) will also give the same answer. maketrans need you to provide two string in the same length. Also, you can do str.translate(table, 'a') to remove every ‘a’ from the original string, which means every ‘c’ from the answer will be removed.

print str.translate(table, 'a')

# i hope you didnt translate it by hand. thats what omputers are for. doing it in by hand is ineffiient and that's why this text is so long. using string.maketrans() is reommended. now apply on the url.