what's going on here?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    #1

    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.pythonchall enge.com/pc/def/linkedlist.php? nothing='
    pattern = re.compile(r'\b next nothing is (\d+)')

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

    nextNum = '12345'
    f = open(r'C:\Pytho n24\myscripts\c hallenges\numbe rs.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\my scripts\challen ges\linked_list .py", line 27, in
    -toplevel-
    nextNum = getNextNum(url + nextNum)
    File "C:\Python24\my scripts\challen ges\linked_list .py", line 12, in
    getNextNum
    site = urllib.urlopen( url)
    File "C:\Python24\li b\urllib.py", line 82, in urlopen
    return opener.open(url )
    File "C:\Python24\li b\urllib.py", line 190, in open
    return getattr(self, name)(url)
    File "C:\Python24\li b\urllib.py", line 322, in open_http
    return self.http_error (url, fp, errcode, errmsg, headers)
    File "C:\Python24\li b\urllib.py", line 339, in http_error
    return self.http_error _default(url, fp, errcode, errmsg, headers)
    File "C:\Python24\li b\urllib.py", line 579, in http_error_defa ult
    return addinfourl(fp, headers, "http:" + url)
    File "C:\Python24\li b\urllib.py", line 871, in __init__
    addbase.__init_ _(self, fp)
    File "C:\Python24\li b\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.
  • John Salerno

    #2
    Re: what's going on here?

    John Salerno wrote:[color=blue]
    > Ok, long story[/color]

    Ok, I guess I should have used a better title for the thread. I hope
    someone still sees this post! :)

    Comment

    • Ant

      #3
      Re: what's going on here?

      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

      Comment

      • John Salerno

        #4
        Re: what's going on here?

        Ant wrote:[color=blue]
        > 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.[/color]

        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.
        [color=blue]
        > 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![/color]

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

        Comment

        • John Salerno

          #5
          Re: what's going on here?

          John Salerno wrote:[color=blue]
          > Ant wrote:[color=green]
          >> 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.[/color]
          >
          > 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.
          >[color=green]
          >> 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![/color]
          >
          > Interesting! That's good news, and I'm glad I didn't spend hours trying
          > to use one to solve it! :)[/color]

          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!

          Comment

          • Roel Schroeven

            #6
            Re: what's going on here?

            John Salerno schreef:[color=blue]
            > But thank god I'm passed this problem, although I'm sure it only gets
            > worse now![/color]

            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

            Comment

            Working...