1. 程式人生 > >python練題--遊戲式闖關挑戰

python練題--遊戲式闖關挑戰

>print pow(2,38)
>274877906944
def tranChar(s):
    if s==" " or s=="\'" or s==".":
        t = s
    elif s=="y":
        t = "a"
    elif s=='z':
        t = 'b'
    else:
        t = chr(ord(s)+2)
    return t

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() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
s = "" for i in str.strip(" "): s = s+tranChar(i) print s

經典解法:

print "".join(map(lambda x: x.isalpha() and chr((ord(x)+2-ord("a"))%26 + ord("a")) or x, input))

tips:在該網頁的原始碼找題目!

find rare characters in the mess below:

print "".join(map(lambda x: x.isalpha() and x or "",mess))
reg = r'[^A-Z]+[A-Z]{3}([a-z])[A-Z]{3}[^A-Z]'
re_mess_pattern = re.compile(reg)
r = re_mess_pattern.findall(mess)
print "".join(r)
import urllib,re,time

uri = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%s"

reg = r"and the next nothing is (\d+)"
num = "8022"

while True
: try: source = urllib.urlopen(uri % num).read() # print source num = re.search(reg,source).group(1) print num except: break print "Result is:",num

中間還要折騰兩次~

import urllib,pickle

uri = 'http://www.pythonchallenge.com/pc/def/banner.p'
data = urllib.urlopen(uri)
tdata = pickle.load(data)

for i in tdata:
    print "".join([j[1]*j[0] for j in i])  #j[1]表示生成#的個數,j[0]表示空格或者#

              #####                                                                      ##### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
               ####                                                                       #### 
      ###      ####   ###         ###       #####   ###    #####   ###          ###       #### 
   ###   ##    #### #######     ##  ###      #### #######   #### #######     ###  ###     #### 
  ###     ###  #####    ####   ###   ####    #####    ####  #####    ####   ###     ###   #### 
 ###           ####     ####   ###    ###    ####     ####  ####     ####  ###      ####  #### 
 ###           ####     ####          ###    ####     ####  ####     ####  ###       ###  #### 
####           ####     ####     ##   ###    ####     ####  ####     #### ####       ###  #### 
####           ####     ####   ##########    ####     ####  ####     #### ##############  #### 
####           ####     ####  ###    ####    ####     ####  ####     #### ####            #### 
####           ####     #### ####     ###    ####     ####  ####     #### ####            #### 
 ###           ####     #### ####     ###    ####     ####  ####     ####  ###            #### 
  ###      ##  ####     ####  ###    ####    ####     ####  ####     ####   ###      ##   #### 
   ###    ##   ####     ####   ###########   ####     ####  ####     ####    ###    ##    #### 
      ###     ######    #####    ##    #### ######    ###########    #####      ###      ######

讀zip包中的readme檔案,可以知道題目意思和上面第5道題類似:


import urllib, zipfile, re, collections

o, n, f = [], "90052", "%s.txt"
nnr = "Next nothing is (\d+)"

# Download the ZIP file from http://www.pythonchallenge.com/pc/def/channel.zip

file = zipfile.ZipFile("channel.zip")

while True:
    try:
        n = re.search(nnr, file.read(f % n)).group(1)
    except:
        print file.read(f % n)
        break

    o.append(file.getinfo(f % n).comment)

print "".join(o)
這道題,貌似有些小問題,我是沒有收集到comment資訊.