Skip to content

Instantly share code, notes, and snippets.

@wacotaqo
Created January 19, 2009 21:19
Show Gist options
  • Select an option

  • Save wacotaqo/49182 to your computer and use it in GitHub Desktop.

Select an option

Save wacotaqo/49182 to your computer and use it in GitHub Desktop.
Python Challenge #4
===================
URL: http://www.pythonchallenge.com/pc/def/linkedlist.html
is referred to:
URL: http://www.pythonchallenge.com/pc/def/linkedlist.php
Title: Follow the chain
Image: Wooden stone sawing figures
Image has url: http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345
http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345
gives:
and the next nothing is 92512
http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=92512
gives
and the next nothing is 64505
And so on... need to code this.
import urllib
import re
url = r'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
linkedlist = []
def get_next_nothing(nothing):
next_row = urllib.urlopen('%s%s'%(url, nothing)).read()
s = re.search('next nothing is ([0123456789]*)', next_row)
if s:
return (s.groups(0)[0], next_row)
else:
return ('', next_row)
>>> while 1:
... if len(linkedlist) == 0:
... nothing = '12345'
... else:
... (old, nothing, oldtext) = linkedlist[-1]
... (next, text) = get_next_nothing(nothing)
... if next:
... print next
... linkedlist.append((nothing, next, text))
... nothing = next
... else:
... print "Unable to parse: %s"%text
... break
The above goes on till it gets stuck at:
...
43240
5868
19337
21122
92118
Unable to parse: Yes. Divide by two and keep going.
Having expected something like this: I added the data:
linkedlist.append(('92118', '46059', 'Yes. Divide by two and keep going.'))
And reran the loop which picks up on the last 'next'
Getting to ...
...
27719
65667
Unable to parse: peak.html
Total of 209 requests.
http://www.pythonchallenge.com/pc/def/peak.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment