what's going on here?

J

John Salerno

Ok, long story: I'm trying to solve level 4 of the Python Challenge. I
hate to post here, but the hint forum over there is dead. Here's the
link: http://www.pythonchallenge.com/pc/def/linkedlist.php

Apparently you need to use a linked list to solve it, so I read up on
them but I still don't understand how to implement one to solve this
problem. (Any hints there would be appreciated too.) So I wrote this
code instead. It goes to each URL, reads the source code, gets the next
number, etc. I realize it uses some terrible tricks, like the regex and
the try/except clause, which is way too specific to solve the problem in
a general way. Anyway, here's the code:

# Python Challenge, level 4

import urllib
import re

url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
pattern = re.compile(r'\bnext nothing is (\d+)')

def getNextNum(url):
global pattern
global nextNum
site = urllib.urlopen(url)
try:
source = site.read()
site.close()
number = re.search(pattern, source).group(1)
except:
print nextNum
number = str(int(nextNum) / 2)
return number

nextNum = '12345'
f = open(r'C:\Python24\myscripts\challenges\numbers.txt', 'w')
try:
for x in range(300):
f.write(nextNum + '\n')
nextNum = getNextNum(url + nextNum)
finally:
f.close()

print url + nextNum

Now, I tried this earlier on my work computer and it got as far as
printing out two 'nextNum's in the except block, then it appeared that
the website timed out and I could no longer access it for a while.

I tried later on my home computer and now I keep getting this error:

Traceback (most recent call last):
File "C:\Python24\myscripts\challenges\linked_list.py", line 27, in
-toplevel-
nextNum = getNextNum(url + nextNum)
File "C:\Python24\myscripts\challenges\linked_list.py", line 12, in
getNextNum
site = urllib.urlopen(url)
File "C:\Python24\lib\urllib.py", line 82, in urlopen
return opener.open(url)
File "C:\Python24\lib\urllib.py", line 190, in open
return getattr(self, name)(url)
File "C:\Python24\lib\urllib.py", line 322, in open_http
return self.http_error(url, fp, errcode, errmsg, headers)
File "C:\Python24\lib\urllib.py", line 339, in http_error
return self.http_error_default(url, fp, errcode, errmsg, headers)
File "C:\Python24\lib\urllib.py", line 579, in http_error_default
return addinfourl(fp, headers, "http:" + url)
File "C:\Python24\lib\urllib.py", line 871, in __init__
addbase.__init__(self, fp)
File "C:\Python24\lib\urllib.py", line 818, in __init__
self.read = self.fp.read
AttributeError: 'NoneType' object has no attribute 'read'

I didn't get this before, so I wonder if it's a website error. There
seems to be a problem with getting the source code. It also appears that
the script, when run at home, doesn't get as far as the same script when
I ran it earlier today at work.

So I figure it's either a website problem, or some kind of strange
difference between the two computers I'm using (but both use 2.4.3 and
nothing else seems different).

I hope someone can point me in the right direction. I'm curious why it
fails in a different way at home than at work, but also I'd like to know
if it's even possible to solve the problem in this way, or if I *have*
to use a linked list.
 
A

Ant

You are along the right lines. Try printing out the content of each URL
- one of the pages will match your expression, but has additional
instructions... I think you are reaching the end of their false trail
when you get None returned from the url.

The set of pages themselves are the linked list - each contains a
reference to the next element in the sequence. You don't need one to
solve the problem, the problem *is* the linked list!

An example of a linked list in Python is the following if you are
interested:


class Element (object):
next = None
def __init__(self, content):
self.content = content

class LinkedList (object):
first = None
last = None

def add(self, item):
elmnt = Element(item)
if not self.first:
self.first = elmnt
if self.last:
self.last.next = elmnt
self.last = elmnt
def __iter__(self):
current = self.first
while current:
yield current.content
current = current.next

ll = LinkedList()

for i in range(10):
ll.add(i)

for x in ll:
print "Res: %s" % x
 
J

John Salerno

Ant said:
You are along the right lines. Try printing out the content of each URL
- one of the pages will match your expression, but has additional
instructions... I think you are reaching the end of their false trail
when you get None returned from the url.

But the weird thing is that when I tried the same script earlier in the
day, it went through about 200+ links before encountering a situation
that my script didn't handle. But now when I get this latest error that
I posted, it's only going through about 150 links before stopping.
The set of pages themselves are the linked list - each contains a
reference to the next element in the sequence. You don't need one to
solve the problem, the problem *is* the linked list!

Interesting! That's good news, and I'm glad I didn't spend hours trying
to use one to solve it! :)
 
J

John Salerno

John said:
But the weird thing is that when I tried the same script earlier in the
day, it went through about 200+ links before encountering a situation
that my script didn't handle. But now when I get this latest error that
I posted, it's only going through about 150 links before stopping.


Interesting! That's good news, and I'm glad I didn't spend hours trying
to use one to solve it! :)

Ok, I'm confused. I ran the script again today (this time at work again)
and it worked! I made no changes, so I'm not sure what the issue was.
But thank god I'm passed this problem, although I'm sure it only gets
worse now!
 
R

Roel Schroeven

John Salerno schreef:
But thank god I'm passed this problem, although I'm sure it only gets
worse now!

Yes, I'm afraid it does. I got stuck at puzzle 27 and gave up
temporarily. I'm going to try again though when I feel I need a challenge :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top