473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

what's going on here?

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.tx t', '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.
Apr 4 '06 #1
5 1433
John Salerno wrote:
Ok, long story


Ok, I guess I should have used a better title for the thread. I hope
someone still sees this post! :)
Apr 4 '06 #2
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

Apr 4 '06 #3
Ant wrote:
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! :)
Apr 4 '06 #4
John Salerno wrote:
Ant wrote:
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! :)


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!
Apr 4 '06 #5
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 :)

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
Apr 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: Tony Marston | last post by:
Several months ago I started a thread with the title "What is/is not considered to be good OO programming" which started a long and interesting discussion. I have condensed the arguments into a...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
9
by: andrew | last post by:
Hi, I posted this message recently, but received no response, so I figured I'd try again. I can't find anybody who can tell me what's going on here. I'm having a problem with fieldsets in IE...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
8
by: Midnight Java Junkie | last post by:
Dear Colleagues: I feel that the dumbest questions are those that are never asked. I have been given the opportunity to get into .NET. Our organization has a subscription with Microsoft that...
63
by: Jake Barnes | last post by:
In the course of my research I stumbled upon this article by Alex Russel and Tim Scarfe: http://www.developer-x.com/content/innerhtml/default.html The case is made that innerHTML should never...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
6
by: LurfysMa | last post by:
I am working on an electronic flashcard application. I have one version up and running using Visual Basic (6.0) and Access 2000. My long-range plans are to put it up on a website and sell...
10
by: JoeC | last post by:
I have been programming for a while and I have seen this syntax before and I copied this from a book but the book didn't explain what is going on here. class engine{ protected: static engine*...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.