Overview
This week we will finish our Base64 encoder / decoder projects, and discuss the possibility of adding a curses interface to the program after we finish.
Thursday, October 29th
Classwork / Homework
During our last class only six students were able to complete the assigned task during the class period. Since we want to maximize our learning and not get bogged down, everyone who finished during class earned an A for the task, and everyone else earned a C. We also didn't get to the Pydoc presentation I wanted to share. I'll do that first thing today.
Next I'll share a share solutions for the two functions from last class. Please pay close attention during that discussion, and commit yourself to understanding how it works. You'll need to understand them to complete today's task.
DIGITS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
def encode(s):
"""
>>> encode("W1sT/P")
[22, 53, 44, 19, 63, 15]
>>> encode("ABCDEFG")
[0, 1, 2, 3, 4, 5, 6]
>>> encode("abcdefg")
[26, 27, 28, 29, 30, 31, 32]
>>> encode("12345")
[53, 54, 55, 56, 57]
"""
ints = []
for ch in s:
ints.append(DIGITS.index(ch))
return ints
def decode(nums):
"""
>>> decode([22, 53, 44, 19, 63, 15])
'W1sT/P'
>>> decode([0, 1, 2, 3, 4, 5, 6])
'ABCDEFG'
>>> decode([26, 27, 28, 29, 30, 31, 32])
'abcdefg'
>>> decode([53, 54, 55, 56, 57])
'12345'
"""
s = ""
for i in nums:
s += DIGITS[i]
return s
if __name__ == '__main__':
import doctest
doctest.testmod()
Add the following function header and doctests to our program, then add a function body which makes the doctests pass.
def bits_to_base64(bitstr):
"""
>>> bits_to_base64('010001110100100001100111')
'R0hn'
>>> bits_to_base64('010101000110010101110011')
'VGVz'
>>> bits_to_base64('01001101')
'TQ=='
>>> bits_to_base64('0100110101100001')
'TWE='
>>> s = '100111010110100101010011010111100111011110001110110101110011000011'
>>> result = bits_to_base64(s)
>>> result
'nWlTXneO1zD'
>>> s2 = '10110101100010110011010101010110111100011101101011100101'
>>> result2 = bits_to_base64(s2)
>>> result2
'tYs1VvHa5Q=='
"""
This task will be evaluated using the following rubric:
- A: You finish a function body that makes all tests pass.
- B: You finish a function body that makes the first two tests pass.
- C: You work diligently all period long and are actively engaged in trying to understand the problem.
- D: You're there in body, but not in spirit.
- E: No show.
Tuesday, October 27th
Classwork / Homework
Chris tells me that many of you are still a bit lost as to what
binary numbers are
and how they relate directly to my oft repeated statement that It's all
just bits!
The main learning objective of this entire project is to bridge
that lack of understanding. So with that in mind, I'll help guide you by
giving you more byte size tasks.
Base64 to int
- Working with your partner read and discuss the Base64 entry on Wikipedia, paying special attention to the Base64 alphabet table presented in the article.
-
Write a function body that will make the following doctests pass:
def encode(s): """ >>> encode("W1sT/P") [22, 53, 44, 19, 63, 15] >>> encode("ABCDEFG") [0, 1, 2, 3, 4, 5, 6] >>> encode("abcdefg") [26, 27, 28, 29, 30, 31, 32] >>> encode("12345") [53, 54, 55, 56, 57] """ def decode(nums): """ >>> decode([22, 53, 44, 19, 63, 15]) 'W1sT/P' >>> decode([0, 1, 2, 3, 4, 5, 6]) 'ABCDEFG' >>> decode([26, 27, 28, 29, 30, 31, 32]) 'abcdefg' >>> decode([53, 54, 55, 56, 57]) '12345' """
I'll finish by taking a minute to introduce you to Pydoc, which you can use to browse the Python source code available on your computer.