The Python Challenge

For the funday, we'll try to go as far as we can in the Python Challenge. Let's see where it takes us!

Whenever you see keywords, make sure to read and practice more about them from the internet since they're super useful for doing loads of stuff in Python. Please, please go on and read up, from anywhere you wish to. (or pick up stuff from the resources on the course website)

The data files ain't being uploaded, you can go try the challenge for yourself!

In [1]:
#0 was easy, we just did this:
# Keywords: ¯\_(ツ)_/¯

2**38
Out[1]:
274877906944
In [2]:
#1 we created this function using list comprehension beautifully.
# Keywords: list comprehension, ASCII, ord, chr, and join

def plus2(ciphertext):
    return ''.join([chr(((ord(s) + 2) - ord('a')) % 26 + ord('a')) if s >= 'a' and s <= 'z' else s for s in ciphertext])
In [4]:
#2 was solved by a frequency check
# Keywords: file handling in Python, with, dictionaries

with open('data1.txt', 'r') as f:
    s = f.read()
    dic = {}
    for letter in s:
        if letter in dic:
            dic[letter] += 1
        else:
            dic[letter] = 1
In [6]:
#3 was searching for a particular 7-string 
# Keywords: join and split, list slicing, isupper

with open('data2.txt', 'r') as f:
    for line in ''.join(f.read().split('\n')):
        slices = [line[i : i+7] for i in range(len(line) - 7)]
        select = []
        for s in slices:
            if (s[0].isupper() and s[1].isupper() and s[2].isupper() and s[4].isupper() and s[5].isupper() and s[6].isupper() and s[3].islower()):
                select.append(s)
In [8]:
#4 we used a new library
## Note: Some libraries come preinstalled with Python, and some you'll need to install yourself.
## Keywords: urllib (the library), web scraping using Python (also check out bs4)
## keywords: Regular Expressions (library re), string formatting options, utf-8/unicode

from urllib.request import urlopen
import re

uri = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%s"

# num = "12345"
num = str(16044/2)

pattern = re.compile("and the next nothing is (\d+)")

def run(uri, num, pattern):
    while True:
        content = urlopen(uri % num).read().decode('utf-8')
        print(content)
        match = pattern.search(content)
        if match == None:
            break
        num = match.group(1)
In [9]:
#5 We printed an ASCII Art
# Keywords: pickle (the library), ASCII Art

import pickle

def run():
    with open('banner.p', 'rb') as b:
        for elem in pickle.load(b):
            for part in elem:
                print(part[0] * part[1], end='')
            print()

I hope you enjoyed. You should totally go ahead and finish off all the challenges (except that last 5). You'll learn a lot.

Here's a list of other super useful Python libraries you should go ahead and learn how to use:

  • numpy/scipy : for large scientific/math computations (they're super fast)
  • random : for random numbers/choices/etc
  • math : for simple math functions
  • tensorflow : automatic differentiation (you'll probably need a good tutorial for this one)
  • string : string processing functions
  • pandas : dataframe handling
  • csv : for tabular data
  • PIL/pyimage : for handling image data / basic image editing stuff