Wednesday, October 26, 2011

[Py] The Python Challenge -- Level 01

Here is a note of my solution to solve Level 01 of The Python Challenge.

Used functions: ord() and chr().



The ord() and chr() are handy to convert between the ASCII characters and the corresponding digits.

The original version of the my code (in Python 3) is as follows.

#str_in = "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." 
str_in = "map"

ch = ''
for tmp in str_in:
    if ord(tmp) >= ord('a'):
        if ord(tmp) <= ord('x'):
            ch = ch + ( chr( ord(tmp) + 2) )
        if ord(tmp) >= ord('y'):
            ch = ch + ( chr( ord(tmp) + 2 - 26) )
    else:
        ch = ch + tmp 

print(ch)

No comments:

Post a Comment