Python Challenge – level 4 solution

# rework using raw_input instead of hard-coded '12345'
# read content from an url
# match a pattern (so-called 'nothing') from the content
# put parameters in the url and read again

import urllib
import re

# Initial nothing is 12345
firstnothing = raw_input("Input the start nothing: ")
f= urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" + firstnothing)
s = f.read()
m = re.findall('[0-9]+',s)
while len(m) <> 0:
    params = urllib.urlencode({'nothing':m[len(m)-1]})
    f = urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php?%s" % params)
    s= f.read()
    m = re.findall('[0-9]+',s)
    print m
    
print s
>>>execfile('pc4re.py')
Input the start nothing: 12345
['45439']
...
['16044']
[]
Yes. Divide by two and keep going.
>>>execfile('pc4re.py')
Input the start nothing: 8022
['89879']
...
['66831']
[]
peak.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s