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;
48 <textarea id=
"source">
52 ![centre-aligned Caesar wheel](caesarwheel1.gif)
54 Letter-by-letter enciphering
60 .indexlink[[Index](index.html)]
64 # Enciphering and deciphering
66 ## Arithmetic on letters
68 Convert .plaintext[letter] → .plaintext[number] →
69 .ciphertext[number] → .ciphertext[letter]
71 Functions you will need
81 * What are good test cases?
85 # The [string module](http://docs.python.org/
3.3/library/string.html) is your friend
91 string.ascii_lowercase
92 string.ascii_uppercase
110 def caesar_encipher_letter(letter, shift):
111 """Encipher a letter, given a shift amount
113 >>> caesar_encipher_letter('a', 1)
116 if letter in string.ascii_letters:
124 # The magic doctest incantation
127 if __name__ ==
"__main__":
134 # Doing all the letters
136 ## Test-first developement
139 * They will fail. There is no code.
140 2. Write code until the tests pass.
145 # Doing all the letters
151 for i in range(len(plaintext)):
152 ciphertext += caesar_encipher_letter(plaintext[i], key)
157 # Doing all the letters
164 ciphertext += caesar_encipher_letter(p, key)
167 ...but easily generalisable
171 # Doing all the letters
173 ## Good (but unPythonic)
176 ciphertext = map(lambda p: caesar_encipher_letter(p, key), plaintext)
181 # Doing all the letters
186 ciphertext = [caesar_encipher_letter(p, key) for p in plaintext]
190 # Not all iterables are equal
197 <script src=
"http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type=
"text/javascript">
199 <script type=
"text/javascript">
200 var slideshow = remark.create({ ratio:
"16:9" });