My Python Challenge Solutions
# -- 0 --
# problem : http://www.pythonchallenge.com/pc/def/0.html
2**38
# -- 1 --
# problem : http://www.pythonchallenge.com/pc/def/map.html
# solution : http://www.pythonchallenge.com/pcc/def/ocr.html
input_string = "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 decode_string(string):
def decode_char(char):
return chr((((ord(char) + 2) - 97) % 26) + 97) if char.isalpha() else char
return "".join(decode_char(char) for char in string)
decode_string(input_string)
decode_string("map")
# -- 2 --
# problem : http://www.pythonchallenge.com/pc/def/ocr.html
# solution : http://www.pythonchallenge.com/pcc/def/equality.html
import urllib.request
import re
def get_source(url):
with urllib.request.urlopen(url) as response:
html = response.read()
return html
def print_source(url):
print(get_source(url).decode())
html = get_source("http://www.pythonchallenge.com/pc/def/ocr.html")
# https://docs.python.org/3.6/howto/regex.html#greedy-versus-non-greedy
# https://docs.python.org/3.6/howto/regex.html#compilation-flags
input_string = re.findall(b"<!--(.*?)-->", html, flags = re.DOTALL)[-1].strip().decode()
"".join(char for char in input_string if char.isalpha())
# -- 3 --
# problem : http://www.pythonchallenge.com/pc/def/equality.html
# solution : http://www.pythonchallenge.com/pcc/def/linkedlist.php
html = get_source("http://www.pythonchallenge.com/pc/def/equality.html")
input_string = re.findall(b"<!--(.*?)-->", html, flags = re.DOTALL)[-1].strip().decode()
"".join(re.findall(r"[^A-Z]+[A-Z]{3}([a-z]{1})[A-Z]{3}[^A-Z]+", input_string))
# -- 4 --
# problem : http://www.pythonchallenge.com/pc/def/linkedlist.php
# solution : http://www.pythonchallenge.com/pcc/def/peak.html
print_source("http://www.pythonchallenge.com/pc/def/linkedlist.php")
get_source("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345")
nothing = "12345"
while True:
source = get_source("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing={0}".format(nothing))
try:
nothing = re.findall(b"and the next nothing is ([0-9]+)", source)[0].decode()
except:
if source == b"Yes. Divide by two and keep going.":
nothing = int(nothing)//2
else:
print(source)
break
# -- 5 --
# problem : http://www.pythonchallenge.com/pc/def/peak.html
# solution : http://www.pythonchallenge.com/pcc/def/channel.html
print_source("http://www.pythonchallenge.com/pc/def/peak.html")
import pickle
banner = pickle.loads(get_source("http://www.pythonchallenge.com/pc/def/banner.p"))
banner
def row_string(list_of_tuples):
return "".join(char*num for char, num in list_of_tuples)
print(*map(row_string, banner), sep = "\n")
# -- 6 --
# problem : http://www.pythonchallenge.com/pc/def/channel.html
# solution : http://www.pythonchallenge.com/pcc/def/oxygen.html
print_source("http://www.pythonchallenge.com/pc/def/channel.html")
import shutil
# Download the file from `url` and save it locally under `file_name`:
def download_file(url,file_name):
with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
download_file("http://www.pythonchallenge.com/pc/def/channel.zip", "channel.zip")
from zipfile import ZipFile
def read_archive_member(archive_filename, member_filename):
with ZipFile(archive_filename) as archive:
with archive.open(member_filename) as member:
return member.read().decode()
print(read_archive_member(archive_filename = "channel.zip", member_filename = "readme.txt"))
read_archive_member(archive_filename = "channel.zip", member_filename = "90052.txt")
nothing = "90052"
while True:
try:
contents = read_archive_member(archive_filename = "channel.zip", member_filename = "{0}.txt".format(nothing))
nothing = re.findall("Next nothing is ([0-9]+)", contents)[0]
except:
print(contents)
break
# https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.comment
with ZipFile("channel.zip") as archive:
print(archive.comment)
# https://docs.python.org/3/library/zipfile.html#zipfile.ZipInfo.comment
archive = "channel.zip"
nothing = "90052"
comments = []
while True:
try:
member = "{0}.txt".format(nothing)
contents = read_archive_member(archive, member)
comments.append(ZipFile(archive).getinfo(member).comment.decode())
nothing = re.findall("Next nothing is ([0-9]+)", contents)[0]
except:
print(*comments, sep = "")
break
print_source("http://www.pythonchallenge.com/pc/def/hockey.html")
print_source("http://www.pythonchallenge.com/pc/def/oxygen.html")
# -- 7 --
# problem : http://www.pythonchallenge.com/pc/def/oxygen.html
# solution : http://www.pythonchallenge.com/pcc/def/integrity.html
download_file("http://www.pythonchallenge.com/pc/def/oxygen.png","oxygen.png")
from PIL import Image
im = Image.open("oxygen.png")
print(im.size, im.mode)
chars = []
for x in range(0, 630, 7):
(r, g, b, a) = im.getpixel((x, 43))
if r == g == b:
chars.append(chr(r))
print(*chars, sep = "")
print(*map(chr, [105, 110, 116, 101, 103, 114, 105, 116, 121]), sep = "")
# -- 8 --
# problem : http://www.pythonchallenge.com/pc/def/integrity.html
# solution : http://www.pythonchallenge.com/pcc/return/good.html:huge:file
print_source("http://www.pythonchallenge.com/pc/def/integrity.html")
# The server reports that it is from inflate.
# Opposite of "inflate" -- "compress"
# un -- (u)ser(n)ame
# pw -- (p)ass(w)ord
import codecs
source = get_source("http://www.pythonchallenge.com/pc/def/integrity.html")
# http://stackoverflow.com/a/37059682
# http://stackoverflow.com/a/23151714
comment = codecs.escape_decode(re.findall(b"<!--(.*?)-->", source, flags = re.DOTALL)[0])[0]
un = re.findall(b"\nun: '(.*?)'", comment)[0]
pw = re.findall(b"\npw: '(.*?)'", comment)[0]
# https://en.wikipedia.org/wiki/List_of_file_signatures
# bz2 -- Compressed file using Bzip2 algorithm -- BZh
import bz2
bz2.decompress(un)
bz2.decompress(pw)
# -- 9 --
# problem : [url-- http://www.pythonchallenge.com/pc/return/good.html] [username-- huge] [password-- file]
# solution :