5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8"/>
6 <style type=
"text/css">
15 h1 { font-size:
4em; }
16 h2 { font-size:
2em; }
17 h3 { font-size:
1.6em; }
19 text-decoration: none;
22 -moz-border-radius:
5px;
23 -web-border-radius:
5px;
31 text-shadow:
0 0 20px #
333;
37 text-shadow:
0 0 20px #
333;
43 <textarea id=
"source">
47 ![centre-aligned Caesar wheel](caesarwheel1.gif)
49 Letter-by-letter enciphering
53 # Enciphering and deciphering
55 ## Arithmetic on letters
57 Convert .plaintext[letter] → .plaintext[number] →
58 .ciphertext[number] → .ciphertext[letter]
60 Functions you will need
70 * What are good test cases?
74 # The [string module](http://docs.python.org/
3.3/library/string.html) is your friend
80 string.ascii_lowercase
81 string.ascii_uppercase
99 def caesar_encipher_letter(letter, shift):
100 """Encipher a letter, given a shift amount
102 >>> caesar_encipher_letter('a', 1)
105 if letter in string.ascii_letters:
113 def caesar_encipher_letter(letter, shift):
114 """Encipher a letter, given a shift amount
116 >>> caesar_encipher_letter('a', 1)
118 >>> caesar_encipher_letter('a', 2)
120 >>> caesar_encipher_letter('b', 2)
122 >>> caesar_encipher_letter('x', 2)
124 >>> caesar_encipher_letter('y', 2)
126 >>> caesar_encipher_letter('z', 2)
128 >>> caesar_encipher_letter('z', -1)
130 >>> caesar_encipher_letter('a', -1)
133 if letter in string.ascii_letters:
137 # The magic doctest incantation
140 if __name__ ==
"__main__":
147 # Doing all the letters
149 ## Test-first developement
152 * They will fail. There is no code.
153 2. Write code until the tests pass.
158 # Doing all the letters
164 for i in range(len(plaintext)):
165 ciphertext += caesar_encipher_letter(plaintext[i], key)
170 # Doing all the letters
177 ciphertext += caesar_encipher_letter(p, key)
182 # Doing all the letters
184 ## Good (but unPythonic)
187 ciphertext = map(lambda p: caesar_encipher_letter(p, key), plaintext)
192 # Doing all the letters
197 ciphertext = [caesar_encipher_letter(p, key) for p in plaintext]
201 # Not all iterables are equal
208 <script src=
"http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type=
"text/javascript">
210 <script type=
"text/javascript">
211 var slideshow = remark.create();