PythonChlenge Level 0~4

14845 ワード

PythonChlenge Level 0:   http://www.pythonchallenge.com/pc/def/0.html
     PythonChallenge Level 0~4 
    計算2 の38乗を返します。結果をURLの数字0に置き換えます。
print 2**38
 
 
 
 
 
 
PythonChlenge Level 1:   http://www.pythonchallenge.com/pc/def/map.html
PythonChallenge Level 0~4
 
ノートに与えられた規則に従って、画像の下に与えられた文字列を解読します。
g fmnc bgblr rpyl jyrccyl gr zw fyl.rfyr q ufyr amknsrpq dmp.bmggggggl zw fyclb gllcdyr'q rfgq.cccyr cffffq.cgq.cgq.cgq.jgq.cggggq.cggggggq.cggggggggggq.cgggq.cggggq.cgggggggggggq.cyl.cyl.cyl.cyl.cyl.cyl.cyl.     二つの関数を使う
string.
maketrans
(
from、 

)
Return a transplation table suitable for passing to  translate()、that will map each character in from into the character at the same position in to; from and ト must have the same length.
葃菗は変換テーブルテーブルテーブルテーブルテーブルテーブルテーブルテーブルテーブルブル(eg.'A'''a','B''''''''''b')を返します。fromとtoの長さは一致していなければなりません。fromの中にある文字はtoの中の対応する位置の文字に変換されます。 
string.
トレイ
(
s, 
テーブル[ 
deletechars]
)
Delete all characters from s that arin deletechars (if present)、and then tranlate the characters using テーブル、which must be a 256-character ストリングス giving the transplation for each character value,indexed by its ordinal.If テーブル is None、then only the character deletion step is performed.
xiang xiは変換テーブルテーブルテーブルテーブルに従って文字列sを変換します。
 
import string

#load the text 
text = '''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.
'''

#creat a translate table
table = string.maketrans(string.ascii_lowercase, string.ascii_lowercase[2:] + string.ascii_lowercase[:2])

#apply the translation table on string
print string.translate(text, table)

# Or you can "print text.translate(table)"
 if you don't know 「string.asciifuwerscase[2]」、「string.asciifuwerscase[:2]」、「string.asciifuwerscase」、 try to print it. 
 
実行結果:i hope you didnt translate it by hand.that what computters are for.
dong it in by hand is inefficient and that's why this text is so long. 
using stings.maketrans()is recommanded.now apply on the url.(Level 1 URLのmapを同様に置き換えればLevel 2に転送できます。)
 
 
 
 
 
 
PythonChlenge Level 2:   http://www.pythonchallenge.com/pc/def/ocr.html
 
     PythonChallenge Level 0~4
 
ホームページの左下にはいくつかのヒントがあります。
General tips:
Use the hints.They are helpful,most of the times.Investigate the data given to you.Avoid look ing for spoice. We saw the hint「but MABYE they are in the page source」 
実行:右クリック--ページのソースコードを表示します。 
このレベルのパズルが見つかりました。 find rare characters in the mess below:
 
str.isalpha()関数を使って、アルファベットかどうかを判断します。
str.
isalpha
(
)
Return true if all characters in the string are alphabeetic and there is at least one character,false otherswise.
For 8-bit strigs,this method is locale-dependent.
 
import string

mess = ''' #copy the mess from Level.2's page source '''
ans = ''

for x in mess:
    if x.isaplha():
        ans += x

print ans
 
結果: equality
これをレベル2のURLの「ocr」に置き換えると、level 3に転送されます。
 
 
 
 
 
PythonChlenge Level 3:   http://www.pythonchallenge.com/pc/def/equality.html
PythonChallenge Level 0~4ページの左下の内容
To see the solutions to the previous level,replace pc with pcc,i.e.go to:http://www.pythonchallenge.com/pcc/def/equality.html Join us on IRLC:irc.freenode.net菗pythonchalling
ページの左下の内容は本題と関係がないですが、「see the solutions to the previious level」からヒントを得て、第二関と同じようにウェブページのソースコードを調べました。
画像の下のヒント
One small letter、surrounded by EXACT LY three big bodyggards on each of its sides.
つの小文字の両方には三つの大文字があります。XXXXXXのような形をしています。その後、試してみましたが、結果は得られません。検索したところ、他のクリアランスの人が一致する形式はXXXXXXXXXXXXxです。なぜそうなったのかは分かりませんでしたが、確かに次の関までの正しい方向です。
与えられたmessからxxxxxxxxxxxxxxxxxxxxx形式の文字を探し出し、ウェブソースコード取得モジュールUrllib 2及び正規表現reモジュールを使用する。
urllib 2.
urlopen
(
url[,data[,timeout[,cafile[,capath[,cadefault[,context]]]]]
)
Open the URL url,which can be either a string or a  Request object
ダタ may be a string specifying additional data to send to the server,or ノン?ネ if no such data is needd.Currenntly HTTP requests are the only ones that use data;the HTTP request will be a POST instead of a GET when the ダタ parameter is provided. ダタ Shuld be a buffer in the standrd アプリ/x-www-form-urlencoded フォーマット.The  urllib.urlencode() function taes a mapping or sequence of 2-tuples and returns a string in this format.urllib 2 module sends HTTP/1.1 requests with Connection:close header includ.
The optional タイムアウト parameter specifies a timeout in seconds for blocking operation s like the connection atempt(if not specifield,the global default timeout setting will be used).This actually only works for HTTP,HTTPS conness.
If。 context is specified,it must be a  ssl.SSLContext instance describing the various SSL options.See  HTTPSConnection for more details.
The optional cafile and capath parameters specify a set of trusted CA certificast for HTTPS requests. cafile shuld point to a single file containing a bundle of CA certificat,wheres capath Shuld point to a directory of hashed certifiles.More information can be found in  ssl.SSLContext.load_verify_locations().
The cadefault parameter is ignored.
This function returns a file-like object with three additional methods:
geturl() — return the URL of the resource retrieved,commonly used to determine if a redirect was info() — return the meta-information of the page,such as headers,in the form of an  mimetools.Message instance  Quick Reference to HTTP Headers)getcode() — return the HTTP status code of the reponse.Raises.  URLER or on errors.
Note that ノン?ネ may be returned if no handler handles the request(though the default installed global)  Opener Director アメリカ  Unknown Handler to ensure this never happens)
In addition、if proxy settings are detected(for example、when a *_proxy environment variable like http-puproxy is set)  ProxyHandler is default installed and makes sure the requests are handed through the proxy.
Changed in version 2.6: タイムアウト was added.
Changed in version 2.7.9: cafile capath、 cadefault,and context were added.
 
re.
findll
(
pattern 
ストリングス 
flags=0
)
Return all non-overlapping matches of pattern in string、as a list of strigs.The ストリングス is scanned left-to-right,and matches are returned in the order found.If one or more groups are present in the pattern,return a list of groups;this will be a list of tuples the pattern the pattern the the pattern
New in version 1.5.2.
Chend in version 2.4: Added the optional flags argment.
 
import urllib2
import re
import string

text = urllib2.urlopen('http://www.pythonchallenge.com/pc/def/equality.html').read()
matstr = re.findall(r'[a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]', text)
ans = ''
i = 0
while i < len(matstr):
    ans += matstr[i][4]
    i += 1
print ans








 
探し出したマッチング文字に基づいて意味のある角度が必要で、最後のansはxxxxxxxxxxxXx形式の中間の小文字だけを抽出しました。
コード実行結果は linkedlistは前の問題の経験によって、Level 3のequalityを交替して車に返すと「linkedlist.php」と提示されました。ヒントによって、もう一度交換します。これはphpでLevel 4に転送されます。
 
 
 
 
 
PythonChlenge Level 4:   http://www.pythonchallenge.com/pc/def/linkedlist.php
PythonChallenge Level 0~4はよく分からない図を与えました。隠しメッセージがあるかどうか確認してください。ウェブページのソースコードの中で右クリックしてください。ウェブページのソースコードを確認してください。 次のとおりです
<html>
<head>
  <title>follow the chain</title>
  <link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<!-- urllib may help. DON'T TRY ALL NOTHINGS, since it will never 
end. 400 times is more than enough. -->
<center>
<a href="linkedlist.php?nothing=12345"><img src="chainsaw.jpg" border="0"/></a>
<br><br><font color="gold"></center>
Solutions to previous levels: <a href="http://wiki.pythonchallenge.com/"/>Python Challenge wiki</a>.
<br><br>
IRC: irc.freenode.net #pythonchallenge
</body>
</html>
 テーマ「フォロワーthe chain」、コメント「urllib may help.DON'T TRY ALL NOTHINGS、since it will never」 end.400 times is more than enough.」の別の分析ソースコードは、ウェブページの画像にハイパーリンクheref=「linkedlist.php?nothing=12345」が追加されていることが分かります。クリックした画像を返してテキストヒント「and the next nothing is 44827」を得ます。
以上の分析から、Level 4の問題面でやるべきことは、ウェブ画像のハイパーリンクURLのnothing値を再帰的に交換することであると分かりました。
import urllib2
import string

urLstr = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='

def get_Nothings(uid):
    if uid.isdigit():
        text = urllib2.urlopen(urLstr+uid).read()
        # print text
        nextuid = ''
        for x in text:
            if x.isdigit():
                nextuid += x 
        # print urLstr + uid             
        print urllib2.urlopen(urLstr+uid).read(),'
' return get_Nothings(nextuid) else: pass get_Nothings("12345")
 コードの実行時間が短いと、数字ではなくnothing値を与える場合に、「Yes,Divide by two and keep going」を提示します。
その前のnothing値16044を、コードの関数getNothings()の初期値として2である8022で割って実行し続ける。PythonChallenge Level 0~4 PythonChallenge Level 0~4
最後にLevel 4を得た結果peak.はURLに置き換えてLevel 5に転送します。
レベル5:http://www.pythonchallenge.com/pc/def/peak.html
Level 5~10の問題解決過程は次のブログに記録されます。ありがとうございます。