[ Home | Journal ]
The Python Challenge is a website that sets a number of programming challenges to solve with Python. Each challenge/question has its own URL, the answer to which then forms part of another URL. After successfully answering a question you can access a wiki page where people offer their solutions. To see the solutions to the previous level, replace pc with pcc, i.e. go to: http://www.pythonchallenge.com/pcc/def/ocr.html. Some challenges involve more than one solution.
There are probably any number of ways to solve these, here is my initial pass through the challenges/questions 0 through to 5. With some challenges its a case of trial and error.
Image shows 2^38.
#challenge 0 pow(2,28)
#k->m #o->q #e->g
So, given the input i, transform to output o. The input i, is "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
Hint see the title of the web page 'What about making trans?'. maketrans is a string method, from documentation 'Return a translation table suitable for passing to translate(), that will map each character in from into the character at the same position in to;""solution is i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url"
The solution is jvvr://yyy.ravjqpejcnngpig.eqo/re/fgh/ocr.jvon
#!/usr/bin/env python #challenge 1 import string alpha=string.lowercase to_convert="g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj" def t(s): if s in alpha: idx=alpha.index(s) return alpha[(alpha.index(s)+2)] if idx <=23 else alpha[(idx+1)-25] else: return s out=map(t,to_convert) print "".join(out)
The page states: "recognize the characters. maybe they are in the book, but MAYBE they are in the page source."
Viewing the page source:
"find rare characters in the mess below:" "%%$@_$^__#)^)&!_+]!*"..etc,etc
Lots of characters....apart from letters of the alphabet. Search for those letters, using the re (regular expression) module.
#!/usr/bin/env python #challenge 2 import urllib2,re url="http://www.pythonchallenge.com/pc/def/ocr.html" text = urllib2.urlopen(url) contents=text.read() res=re.findall('[a-z]',contents) idx=contents.index('<!--') idxs=contents[idx+1:].index('<!--') idx=idxs+idx print "result:","".join(re.findall('[a-z]',contents[idx:]))
#!/usr/bin/env python #challenge 3 import urllib2,re url="http://www.pythonchallenge.com/pc/def/equality.html" text = urllib2.urlopen(url) contents=text.read() #"One small letter, surrounded by EXACTLY three big bodyguards on each of its sides." print "".join(re.findall('[a-z][A-Z][A-Z][A-Z]([a-z])[A-Z][A-Z][A-Z][a-z]',contents))
<-- urllib may help. DON'T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough. -->A link points to "linkedlist.php?nothing=12345" So we need to loop of 400 attempts to 1)get a url 2)look for the next number in the html 3) that number is the new argument for the next url. But there are some one to watch out for, example http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=82682 Contents
There maybe misleading numbers in the text. One example is 82683. Look only for the next nothing and the next nothing is 63579.
#!/usr/bin/env python #challenge 4 import urllib2,re idx=8022#first12345 base_url="http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" for i in range(0,400): text = urllib2.urlopen(base_url+str(idx)) contents=text.read() print "Contents:",contents res=re.search('and the next nothing is ',contents) if res: res=res.start() idx=contents[res+24:res+29] else: print "Stopping" break
We need to unpack the contents of banner.p With the following snippet....
import pickle pkl_file = open('banner.p', 'rb') data = pickle.load(pkl_file) print data...we see the following output.
jamespk@sal:~/code/challenge/5$ python 5.py [(' ', 95)] [(' ', 14), ('#', 5), (' ', 70), ('#', 5), (' ', 1)]
But what to do with the output? The answer is the hint in the file name 'banner'. Banner is program that outputs a large ASCII art version of the text that is supplied to it as its argument. So if we take the second line of output, we should display 14 * ' ', followed by 5 * '#', followed by 70 * ' ', etc,etc.
#!/usr/bin/env python #challenge 5 import pickle pkl_file = open('banner.p', 'rb') data = pickle.load(pkl_file) for i in data: str="" for j in i: str += j * j print str pkl_file.close()
"Understanding Algorithms and Data Structures" by Brunskill and Turner was on the reading list of one the units on my CS course. It has some interesting exercises. Page 21 (in the chapter "Necessary Mathematics") has an exercise on sketching and comparing graphs for various mathematical functions. I thought I'd have a go at it with Python and Matplotlib (a Python library for graphs).Using a combination of range and map (which applies a function to every item of an iterable), keeps the code fairly succinct:
#!/usr/bin/env python import matplotlib.pyplot as plt import math_functions as mf functions=[mf.f,mf.g,mf.h,mf.j,mf.m,mf.n] figure=plt.figure() ax=figure.add_subplot(111) for i in functions: plt.plot(range(5), map(i,range(1,6)), 'o-', label=i.__name__) print i.__name__,map(i,range(1,6)) plt.legend(loc='upper left') plt.ylabel('Y') plt.xlabel('X') ax.set_title("Excercises 2.1") plt.show()
Made with Pyblosxom