Finished caesar cipher slides
[cipher-training.git] / slides / caesar-encipher.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Title</title>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
6 <style type="text/css">
7 /* Slideshow styles */
8 body {
9 font-size: 20px;
10 }
11 h1, h2, h3 {
12 font-weight: 400;
13 margin-bottom: 0;
14 }
15 h1 { font-size: 4em; }
16 h2 { font-size: 2em; }
17 h3 { font-size: 1.6em; }
18 a, a > code {
19 text-decoration: none;
20 }
21 code {
22 -moz-border-radius: 5px;
23 -web-border-radius: 5px;
24 background: #e7e8e2;
25 border-radius: 5px;
26 font-size: 16px;
27 }
28 .plaintext {
29 background: #272822;
30 color: #80ff80;
31 text-shadow: 0 0 20px #333;
32 padding: 2px 5px;
33 }
34 .ciphertext {
35 background: #272822;
36 color: #ff6666;
37 text-shadow: 0 0 20px #333;
38 padding: 2px 5px;
39 }
40 </style>
41 </head>
42 <body>
43 <textarea id="source">
44
45 # Caesar ciphers
46
47 ![centre-aligned Caesar wheel](caesarwheel1.gif)
48
49 Letter-by-letter enciphering
50
51 ---
52
53 # Enciphering and deciphering
54
55 ## Arithmetic on letters
56
57 Convert .plaintext[letter] → .plaintext[number] →
58 .ciphertext[number] → .ciphertext[letter]
59
60 Functions you will need
61
62 ```python
63 ord()
64
65 chr()
66
67 mod()
68 ```
69
70 * What are good test cases?
71
72 ---
73
74 # The [string module](http://docs.python.org/3.3/library/string.html) is your friend
75
76 ```python
77 import string
78
79 string.ascii_letters
80 string.ascii_lowercase
81 string.ascii_uppercase
82 string.digits
83 string.punctuation
84 ```
85
86 ---
87 # DRY and YAGNI
88
89 Is your code DRY?
90
91 ---
92
93 # Doctest
94
95 * Why document?
96 * Why test?
97
98 ```python
99 def caesar_encipher_letter(letter, shift):
100 """Encipher a letter, given a shift amount
101
102 >>> caesar_encipher_letter('a', 1)
103 'b'
104 """
105 if letter in string.ascii_letters:
106 ```
107
108 ---
109
110 # My tests
111
112 ```python
113 def caesar_encipher_letter(letter, shift):
114 """Encipher a letter, given a shift amount
115
116 >>> caesar_encipher_letter('a', 1)
117 'b'
118 >>> caesar_encipher_letter('a', 2)
119 'c'
120 >>> caesar_encipher_letter('b', 2)
121 'd'
122 >>> caesar_encipher_letter('x', 2)
123 'z'
124 >>> caesar_encipher_letter('y', 2)
125 'a'
126 >>> caesar_encipher_letter('z', 2)
127 'b'
128 >>> caesar_encipher_letter('z', -1)
129 'y'
130 >>> caesar_encipher_letter('a', -1)
131 'z'
132 """
133 if letter in string.ascii_letters:
134 ```
135 ---
136
137 # The magic doctest incantation
138
139 ```python
140 if __name__ == "__main__":
141 import doctest
142 doctest.testmod()
143 ```
144
145 ---
146
147 # Doing all the letters
148
149 ## Test-first developement
150
151 1. Write the tests.
152 * They will fail. There is no code.
153 2. Write code until the tests pass.
154 3. Refactor.
155
156 ---
157
158 # Doing all the letters
159
160 ## Abysmal
161
162 ```python
163 ciphertext = ''
164 for i in range(len(plaintext)):
165 ciphertext += caesar_encipher_letter(plaintext[i], key)
166 ```
167
168 ---
169
170 # Doing all the letters
171
172 ## Bad
173
174 ```python
175 ciphertext = ''
176 for p in plaintext:
177 ciphertext += caesar_encipher_letter(p, key)
178 ```
179
180 ---
181
182 # Doing all the letters
183
184 ## Good (but unPythonic)
185
186 ```python
187 ciphertext = map(lambda p: caesar_encipher_letter(p, key), plaintext)
188 ```
189
190 ---
191
192 # Doing all the letters
193
194 ## Best
195
196 ```python
197 ciphertext = [caesar_encipher_letter(p, key) for p in plaintext]
198 ```
199 ---
200
201 # Not all iterables are equal
202
203 ```python
204 ''.join()
205 ```
206
207 </textarea>
208 <script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
209 </script>
210 <script type="text/javascript">
211 var slideshow = remark.create();
212 </script>
213 </body>
214 </html>