Tuesday, August 5, 2014

Solution to python challenge level 4

http://www.pythonchallenge.com/pc/def/linkedlist.php


































Just click on the image, it takes you to the following URL:

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


contents of the above URL: and the next nothing is 44827

As you can see, if you replace the above URL with nothing=44827, you will get a new nothing id and so on.... 

We are supposed to automate this process as follows:
- open the URL
- get the id = nothing is ####
- update the URL and continue till we get the id as number


Following is the Python Code:

import urllib2
import re

url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
id = '12345'

response = urllib2.urlopen(url + id)
text = response.read()
print text

while(True):

    response = urllib2.urlopen(url + id)
    text = response.read()

    print text

    new_id = "".join(re.findall(r'nothing is (\d*)', text))

    if new_id:
        print new_id
        id = new_id

    else:
        print "anwser = ", text
        break
-----------------------------------------------------------------------------------------

16044
Anwser =  Yes. Divide by two and keep going.


New answer = 8022

Just replace this id with the ID in above and run again


Final answer = peak.html


Next Challenge = http://www.pythonchallenge.com/pc/def/peak.html





No comments:

Post a Comment