Merge branch 'development' into presentation-slides
[cipher-training.git] / slides / caesar-encipher.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Caesar cipher</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: 3em; }
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 %
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
111 ---
112
113 # The magic doctest incantation
114
115 ```python
116 if __name__ == "__main__":
117 import doctest
118 doctest.testmod()
119 ```
120
121 ---
122
123 # Doing all the letters
124
125 ## Test-first developement
126
127 1. Write the tests.
128 * They will fail. There is no code.
129 2. Write code until the tests pass.
130 3. Refactor.
131
132 ---
133
134 # Doing all the letters
135
136 ## Abysmal
137
138 ```python
139 ciphertext = ''
140 for i in range(len(plaintext)):
141 ciphertext += caesar_encipher_letter(plaintext[i], key)
142 ```
143
144 ---
145
146 # Doing all the letters
147
148 ## Bad
149
150 ```python
151 ciphertext = ''
152 for p in plaintext:
153 ciphertext += caesar_encipher_letter(p, key)
154 ```
155
156 ...but easily generalisable
157
158 ---
159
160 # Doing all the letters
161
162 ## Good (but unPythonic)
163
164 ```python
165 ciphertext = map(lambda p: caesar_encipher_letter(p, key), plaintext)
166 ```
167
168 ---
169
170 # Doing all the letters
171
172 ## Best
173
174 ```python
175 ciphertext = [caesar_encipher_letter(p, key) for p in plaintext]
176 ```
177 ---
178
179 # Not all iterables are equal
180
181 ```python
182 ''.join()
183 ```
184
185 </textarea>
186 <script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
187 </script>
188 <script type="text/javascript">
189 var slideshow = remark.create({ ratio: "16:9" });
190 </script>
191 </body>
192 </html>