-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge04.py
More file actions
45 lines (38 loc) · 1.37 KB
/
challenge04.py
File metadata and controls
45 lines (38 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# encoding: utf-8
"""
challenge04.py
http://www.pythonchallenge.com/pc/def/linkedlist.php
Created by whimsy on 2010-08-31.
Copyright (c) 2010 Will Crawford. All rights reserved.
"""
import sys
import os
import urllib
import re
# This feels really hack-ish =\
def main():
print "You must be connected to the internet to run this challenge solution."
nugget ='12345'
page = urllib.urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' + nugget)
nugget = re.search(r"(\d+)", page.read())
previousnugget = nugget
while nugget:
page = urllib.urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' + nugget.group(0))
previousnugget = nugget
nugget = re.search(r"(\d+)$", page.read())
if nugget:
print nugget.group(0)
# We have an edge case here.
page = urllib.urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' + str(int(previousnugget.group(0))/2))
nugget = re.search(r"(\d+)$", page.read())
while nugget:
print nugget.group(0)
page = urllib.urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' + nugget.group(0))
previousnugget = nugget
nugget = re.search(r"(\d+)$", page.read())
# We're done now.
page = urllib.urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' + previousnugget.group(0))
print page.read()
return 0
if __name__ == '__main__': main()