Python Challenge1What about Making Trans?


Title address www.pythonchallenge.com/pc/def/map….


The subject content everybody thinks twice before solving this. 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.

General tips:

  • Use the hints. They are helpful, most of the times.
  • Investigate the data given to you.
  • Avoid looking for spoilers.

Forums: Python Challenge Forums

Note: the forum link given by the official website is 404.


The title of the page says: How about some translation?

The URL says MAP, or mapping.

Contact the contents of the picture and know that it is a letter mapping. K to M, O to Q, E to G.

Experiments in Python found:

>>> ord('M') - ord('K')
2
>>> ord('Q') - ord('O')
2
>>> ord('G') - ord('E')
2
Copy the code

The ASCII code of the mapped letter is obtained by adding 2 to the ASCII code of the mapped letter.

Use this rule to transcode a string that looks like gibberish, skipping punctuation and not going beyond the letter boundary.

msg = "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."

def trans(x) :
    if x.isalpha():
        x = chr(ord(x) + 2)
        if x.isalpha():
            return x
        else:
            return chr(ord(x) - 26)
    else:
        return x

new_msg = []

for i in msg:
    new_msg.append(trans(i))

new_msg = ' '.join(new_msg)
print(new_msg)
Copy the code

The result is:

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.

The str.maketrans() method is recommended for finding the answer, however this method was replaced by str.maketrans() in Python 3. Syntax: str.maketrans(intab, outtab) Intab is the input table, outtab is the output table, returns a mapping table consisting of ASCII codes, stored as a dictionary. Rewrite the program with str.maketrans() :

intab = 'abcdefghijklmnopqrstuvwxyz'
outtab = 'cdefghijklmnopqrstuvwxyzab'
trantab = str.maketrans(intab, outtab)

msg = "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."

msg = msg.translate(trantab)

print(msg)
Copy the code

You get the same result, but it’s a lot cleaner.

Use the translate function for the URL as prompted:

intab = 'abcdefghijklmnopqrstuvwxyz'
outtab = 'cdefghijklmnopqrstuvwxyzab'
trantab = str.maketrans(intab, outtab)

url = 'map'

url = url.translate(trantab)

print(url)

Copy the code

Get the OCR, replace the map to the browser, press enter to enter the next level: www.pythonchallenge.com/pc/def/ocr….