1313def catch (text , pattern = r'<!--(.*?)-->' , cnt = 0 ):
1414 return re .findall (pattern , text , re .DOTALL )[cnt ]
1515
16+ def analysis (text ):
17+ NOTHING = catch (text , r'nothing=([0-9]+)">' )
18+ MAX_RANGE = catch (text , r'([0-9]+) times is more than enough' )
19+ return NOTHING , MAX_RANGE
1620
17- def solve (url , NOTHING , MAXRANGE ):
21+ def solve (url , nothing , maxrange ):
1822 reo = re .compile (r'and the next nothing is ([0-9]+)' )
19- nothing = NOTHING
23+ next = nothing
24+ text = ''
2025 print ("Following the nothing chain!" )
21- for i in range (int (MAXRANGE )):
22- response = requests .get (url , params = {'nothing' : nothing })
26+ for i in range (int (maxrange )):
27+ response = requests .get (url , params = {'nothing' : next })
2328 text = response .text
2429 m = reo .search (text )
2530 print ('{}: {}' .format (i , text ))
2631 if m :
27- nothing = m .group (1 )
32+ next = m .group (1 )
2833 elif text .find (r'Divide by two' ) > - 1 :
29- nothing = int (nothing ) / 2
34+ next = int (next ) / 2
3035 else :
3136 break
3237 print ("Done\n " )
@@ -35,11 +40,9 @@ def solve(url, NOTHING, MAXRANGE):
3540
3641if __name__ == "__main__" :
3742 r = requests .get (url )
38- NOTHING = catch (r .text , r'nothing=([0-9]+)">' )
39- MAXRANGE = catch (r .text , r'([0-9]+) times is more than enough' )
40-
41- # NOTHING=12345, MAXRANGE=400
42- answer = solve (url , NOTHING , MAXRANGE )
43+ NOTHING , MAX_RANGE = analysis (r .text )
44+ # NOTHING=12345, MAX_RANGE=400
45+ answer = solve (url , NOTHING , MAX_RANGE )
4346 # peak.html
4447 print (PREFIX + answer )
4548 # http://www.pythonchallenge.com/pc/def/peak.html
0 commit comments