First bit of the A-level miscellany
[cas-master-teacher-training.git] / programming.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Programming strategy</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 .indexlink {
41 position: absolute;
42 bottom: 1em;
43 left: 1em;
44 }
45 .float-right {
46 float: right;
47 }
48 </style>
49 </head>
50 <body>
51 <textarea id="source">
52
53 # Programming strategy ![Open University logo](oulogo_hor.png)
54 ## Moving up a level
55
56 Data structures
57
58 Algorithms
59
60 Seeing alternatives
61
62 ---
63
64 layout: true
65
66 .indexlink[![Open University logo](oulogo_hor.png) [Index](index.html)]
67
68 ---
69
70 # Data structures before algorithms
71
72 * What data do you need?
73 * What operations must you perform on it?
74
75 These go a long way to defining the algorithms you must use.
76
77 ## Python built-in types
78
79 * Numbers (float and integer)
80 * Strings
81 * Tuples
82 * Lists
83 * Dictionaries (a.k.a. hashes or maps)
84
85 Many tasks will fit into these
86
87 More in the `collections` standard library, such as `namedtuple` and `Counter`
88
89 ---
90
91 # String
92
93 * Immutable in Python
94 * Single or double quotes (beware apostrophies)
95 * Triple-quoted contain newlines
96
97 ```python
98 'This is a string'
99 ```
100 ```python
101 "This isn't a problem"
102 ```
103 ```python
104 """This is
105 a multiline
106 string"""
107 ```
108
109 ---
110 # Tuple
111
112 * Immutable in Python
113 * Fixed-size collection
114 * Indexed by position
115
116 ```python
117 neil = ('Neil', 'Smith', 44)
118 first_name = neil[0]
119 surname = neil[1]
120 ```
121
122 * Unpacking with assignment
123
124 ```python
125 first_name, surname, age = neil
126 ```
127
128 ```python
129 for _, surname, age in people:
130 ....
131 ```
132
133 ---
134
135 # List
136
137 * Ordered collection
138 * Mutable
139 * Index by position and slices
140
141 ```python
142 members = ['Freddie', 'Brian', 'Roger', 'John']
143 print(members[1])
144 print(members[1:2])
145 print(members[1:3])
146 print(members[1:1])
147 print(members[-1])
148 print(members[2:])
149 print(members[:2])
150 ```
151
152 ---
153 # Lists: beware copy depth!
154
155 ```python
156 members = ['Freddie', 'Brian', 'Roger', 'John']
157 tour_lineup = members
158 tour_lineup[0] = 'Paul'
159 print(tour_lineup)
160 print(members)
161 ```
162
163 ---
164
165 # Copying a list
166
167 ```python
168 tour_lineup = members[:]
169 ```
170
171 ```python
172 tour_lineup = list(members)
173 ```
174
175 ```python
176 import copy
177 tour_lineup = copy.copy(members)
178 tour_lineup = copy.deepcopy(members)
179 ```
180
181 `==` tests value equality; `is` tests object identity
182
183 ```python
184 members = ['Freddie', 'Brian', 'Roger', 'John']
185 tour_lineup = members[:]
186 tour_lineup == members
187 tour_lineup is members
188 ```
189
190 ## Value vs Reference
191
192 Must be part of your mental model
193
194 ---
195
196 # Dicts
197
198 * A set of key-value pairs
199 * Mutable
200 * Items in arbirary order
201 * In Python, can use any immutable type as the key
202 * Use any type as the value
203
204 ```python
205 neil = {'first_name': 'Neil', 'surname': 'Smith', 'age': 44}
206 neil['surname']
207 ```
208
209 ---
210 # Euler 11
211
212 ```python
213 GRID_STRING = """08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
214 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
215 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
216 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
217 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
218 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
219 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
220 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
221 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
222 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
223 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
224 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
225 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
226 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
227 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
228 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
229 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
230 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
231 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
232 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"""
233 ```
234
235 How many data structures can you think of for this puzzle?
236
237 ---
238
239 # Euler 11 structures
240
241 * List of lists: either a list of rows or a list of columns
242 * One large list, find a number by row × 20 + column
243 * Tuple of tuples, one large tuple, list of tuples, tuple of lists...
244 * Dict with keys as (row, column)
245 * Keep it as a string: `int(row * 20 + column) * 3)`
246 * (Beware when joining rows)
247 * List of strings, tuple of strings, dict of strings...
248
249 ---
250
251 # Comprehensions
252
253 Do something to each element of an iterable, returning a sequence of the results
254
255 * Iterable: anything you can use as `for item in iterable`
256 * Sequence: `tuple`, `list`, `dict`
257
258 Examples:
259
260 * List of numbers: `[i for i in range(10)]`
261 * List of squares: `[i ** 2 for i in range(10)]`
262 * List of numbers from a string: `[int(n) for n in str.split()]`
263 * Numbers and their squares: `{i: i ** 2 for i in range(10)}`
264 * Even numbers and their squares: `{i: i ** 2 for i in range(10) if i % 2 == 0}`
265
266 ---
267 # Comprehensions with multiple sequences
268
269 All the numbers in the Euler 11 grid:
270 ```python
271 [n for row in grid for n in row]
272 ```
273
274 Pythagorean triples:
275 <table>
276 <tr valign="top">
277 <td>
278 ```python
279 [(a, b, c)
280 for a in range(1, 21)
281 for b in range(1, 21)
282 for c in range(1, 21)
283 if a &lt;= b
284 if b &lt;= c
285 if a**2 + b**2 == c**2]
286 ```
287 </td>
288 <td>
289 ```python
290 [(a, b, c)
291 for a in range(1, 21)
292 for b in range(a, 21)
293 for c in range(b, 21)
294 if a**2 + b**2 == c**2]
295 ```
296 </td>
297 <td>
298 ```python
299 [(a, b, int((a**2 + b**2)**0.5))
300 for a in range(1, 21)
301 for b in range(a, 21)
302 if int((a**2 + b**2)**0.5) == (a**2 + b**2)**0.5]
303 ```
304 </td>
305 </tr></table>
306
307 ---
308
309 # Task
310
311 Turn the Euler 11 grid into:
312
313 * A list of lists, as a list of rows
314 * A dict with keys of (x, y)
315
316 Do both with explicit iteration, then using only comprehensions.
317
318 ```python
319 grid_nums = [int(n) for n in GRID_STRING.split()]
320 ```
321
322 ---
323
324 # Solutions with explicit loops
325
326 ```python
327 g1 = []
328 for rowstart in range(0, ROWS * COLUMNS, COLUMNS):
329 g1.append(grid_nums[rowstart:rowstart+COLUMNS])
330
331 # g1[row][column]
332 ```
333
334 ```python
335 g2 = {}
336 for x in range(COLUMNS):
337 for y in range(ROWS):
338 g2[(x, y)] = grid_nums[x + y * COLUMNS]
339
340 # g2[(x, y)]
341 ```
342
343 Note the need for the creation of the empty container
344
345 ---
346
347 # Solutions with comprehensions
348
349 ```python
350 g3 = [grid_nums[row * COLUMNS:(row+1) * COLUMNS]
351 for row in range(ROWS)]
352
353 # g3[row][column]
354 ```
355
356 ```python
357 g4 = {(x, y): grid_nums[x + y * COLUMNS]
358 for x in range(COLUMNS)
359 for y in range(ROWS)}
360
361 # g4[(x, y)]
362 ```
363
364 ```python
365 g4s = {(x, y): int(GRID_STRING[(x + y * COLUMNS) * 3:(x + y * COLUMNS) * 3 + 2])
366 for x in range(COLUMNS)
367 for y in range(ROWS)}
368
369 # g4s[(x, y)]
370 ```
371
372 ---
373
374 # Other Python data structures
375
376 ## Set
377 Unordered, mutable, no duplicates
378
379 * Useful for removing duplicates from sequences: `list(set(items))`
380
381 ---
382 # collections.defaultdict
383 Like a dict, but doesn't throw an error if you ask for missing key
384
385 ```python
386 >>> d = {}
387 >>> d[99]
388 Traceback (most recent call last):
389 File "<stdin>", line 1, in <module>
390 KeyError: 99
391 >>> import collections
392 >>> d = collections.defaultdict(int)
393 >>> d[99]
394 0
395 ```
396
397 ---
398
399 # collections.Counter
400 Counts items in a sequence:
401
402 ```python
403 collections.Counter(l for l in open('sherlock-holmes.txt').read()
404 if l in string.ascii_letters)
405 ```
406
407 ---
408
409 # collections.namedtuple
410 Dumb `object` for just storing data in named fields
411
412 ```python
413 Point = collections.namedtuple('Point', ['x', 'y'])
414 p = Point(11, y=22)
415 p[0] + p[1]
416 x, y = p
417 p.x + p.y
418 ```
419
420 ---
421
422 # Data, not algorithms
423
424 Don't put complex, changable logic into code
425
426 Build a data structure that describes it, code that reads it.
427
428 * Euler 11 directions
429 * Multiple choice quiz
430
431 ---
432
433 # Algorithms: trading space for time
434
435 ## Euler 14
436
437 </textarea>
438 <script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
439 </script>
440
441 <script type="text/javascript"
442 src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
443
444 <script type="text/javascript">
445 var slideshow = remark.create({ ratio: "16:9" });
446
447 // Setup MathJax
448 MathJax.Hub.Config({
449 tex2jax: {
450 skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
451 }
452 });
453 MathJax.Hub.Queue(function() {
454 $(MathJax.Hub.getAllJax()).map(function(index, elem) {
455 return(elem.SourceElement());
456 }).parent().addClass('has-jax');
457 });
458 MathJax.Hub.Configured();
459 </script>
460 </body>
461 </html>