4 <title>Caesar cipher
</title>
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8"/>
6 <style type=
"text/css">
15 h1 { font-size:
3em; }
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 # The magic doctest incantation
116 if __name__ ==
"__main__":
123 # Doing all the letters
125 ## Test-first developement
128 * They will fail. There is no code.
129 2. Write code until the tests pass.
134 # Doing all the letters
140 for i in range(len(plaintext)):
141 ciphertext += caesar_encipher_letter(plaintext[i], key)
146 # Doing all the letters
153 ciphertext += caesar_encipher_letter(p, key)
156 ...but easily generalisable
160 # Doing all the letters
162 ## Good (but unPythonic)
165 ciphertext = map(lambda p: caesar_encipher_letter(p, key), plaintext)
170 # Doing all the letters
175 ciphertext = [caesar_encipher_letter(p, key) for p in plaintext]
179 # Not all iterables are equal
186 <script src=
"http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type=
"text/javascript">
188 <script type=
"text/javascript">
189 var slideshow = remark.create({ ratio:
"16:9" });