Skip to content

Instantly share code, notes, and snippets.

@n9986
Created June 21, 2012 11:49
Show Gist options
  • Save n9986/2965305 to your computer and use it in GitHub Desktop.
Save n9986/2965305 to your computer and use it in GitHub Desktop.
pythonchallenge.com solutions (1 to 5)
# I kept solving the puzzles until I reached the donation page. I give up. No dollars please.
################################################################################
# For challenge 5
def challenge5():
import pprint
import pickle
f = open('banner.p', 'rb')
for line in pickle.load(f):
print ''.join(map(lambda pair: pair[0]*pair[1], line))
################################################################################
# For challenge 4
def challenge4():
import re
import urllib
url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
i = 63579
while 1:
print "Trying url = " + url + str(i)
u = urllib.urlopen(url + str(i))
s = u.read()
print '\t' + s
print
i = re.findall(r'([0-9]+)', s)[0]
u.close()
################################################################################
# For challenge 3
def challenge3():
import re
import urllib
u = urllib.urlopen("http://www.pythonchallenge.com/pc/def/equality.html")
thestring = u.read()
u.close()
thestring = thestring.split('\n')
thestring = thestring[22:1271]
thestring = "".join(thestring)
# print thestring
# p = re.compile(r'(\w)\1{2}[a-z](\w)\2{2}')
#print p.search('the is a good boy the the tt UUUiYYYAAAaTYW KKKjOPS OSHzRTQQ').group()
#'the the'
print re.findall(r'[a-z][A-Z][A-Z][A-Z]([a-z])[A-Z][A-Z][A-Z][a-z]', thestring)
# print p.search(thestring).group()
################################################################################
# For challenge 2
def challenge2():
import collections
import urllib
u = urllib.urlopen("http://www.pythonchallenge.com/pc/def/ocr.html")
thestring = u.read()
u.close()
thestring = thestring.split('\n')
thestring = thestring[38:1257]
thestring = "".join(thestring)
print thestring
d = collections.defaultdict(int)
for c in thestring:
d[c] += 1
for c in sorted(d, key=d.get, reverse=True):
print '%s %6d' % (c, d[c])
################################################################################
# For challenge 1
def challlenge1():
from string import maketrans
a = "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."
# b = ""
it = 'abcdefghijklmnopqrstuvwxyz'
ot = 'cdefghijklmnopqrstuvwxyzab'
tt = maketrans(it, ot)
# for c in a:
# x = c
#
# if c.isalnum():
# if c == 'y':
# x = 'a'
# elif c == 'z':
# x = 'b'
# else:
# x = chr(ord(c) + 2)
#
# b = b + x
print a.translate(tt)
print "http://www.pythonchallenge.com/pc/def/map.html".translate(tt)
# print b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment