X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=slides%2Fcaesar-encipher.html;h=4afd78dad4f3367c5107a99d0f40ff8f1e0aa037;hb=7203ac94911556e2b4bf4caab6f5285445faed3b;hp=a4d5b2df15bbec0f783b85e6a2d2658314a32742;hpb=7722e2c6e5d07284cb324c2426c1fc7d47842120;p=cipher-training.git diff --git a/slides/caesar-encipher.html b/slides/caesar-encipher.html index a4d5b2d..4afd78d 100644 --- a/slides/caesar-encipher.html +++ b/slides/caesar-encipher.html @@ -1,7 +1,7 @@ - Title + Caesar cipher @@ -50,6 +55,12 @@ Letter-by-letter enciphering --- +layout: true + +.indexlink[[Index](index.html)] + +--- + # Enciphering and deciphering ## Arithmetic on letters @@ -64,13 +75,41 @@ ord() chr() -mod() +% ``` * What are good test cases? --- +# Using the tools + +Before doing anything, create a new branch in Git + +* This will keep your changes isolated + +Experiment in IPython (ephemeral, for us) + +Once you've got something working, copy the code into a `.py` file (permanent and reusable) + +```python +from imp import reload + +import test +reload(test) +from test import * +``` + +Re-evaluate the second cell to reload the file into the IPython notebook + +When you've made progress, make a Git commit + +* Commit early and often! + +When you've finished, change back to `master` branch and `merge` the development branch + +--- + # The [string module](http://docs.python.org/3.3/library/string.html) is your friend ```python @@ -84,6 +123,7 @@ string.punctuation ``` --- + # DRY and YAGNI Is your code DRY? @@ -103,37 +143,13 @@ def caesar_encipher_letter(letter, shift): 'b' """ if letter in string.ascii_letters: + . + . + . ``` --- -# My tests - -```python -def caesar_encipher_letter(letter, shift): - """Encipher a letter, given a shift amount - - >>> caesar_encipher_letter('a', 1) - 'b' - >>> caesar_encipher_letter('a', 2) - 'c' - >>> caesar_encipher_letter('b', 2) - 'd' - >>> caesar_encipher_letter('x', 2) - 'z' - >>> caesar_encipher_letter('y', 2) - 'a' - >>> caesar_encipher_letter('z', 2) - 'b' - >>> caesar_encipher_letter('z', -1) - 'y' - >>> caesar_encipher_letter('a', -1) - 'z' - """ - if letter in string.ascii_letters: -``` ---- - # The magic doctest incantation ```python @@ -144,7 +160,7 @@ if __name__ == "__main__": --- -# Doing all the letters +# Doing the whole message ## Test-first developement @@ -155,7 +171,7 @@ if __name__ == "__main__": --- -# Doing all the letters +# Doing the whole message ## Abysmal @@ -165,9 +181,11 @@ for i in range(len(plaintext)): ciphertext += caesar_encipher_letter(plaintext[i], key) ``` +Try it in IPython + --- -# Doing all the letters +# Doing the whole message ## Bad @@ -177,9 +195,11 @@ for p in plaintext: ciphertext += caesar_encipher_letter(p, key) ``` +...but easily generalisable + --- -# Doing all the letters +# Doing the whole message ## Good (but unPythonic) @@ -189,7 +209,7 @@ ciphertext = map(lambda p: caesar_encipher_letter(p, key), plaintext) --- -# Doing all the letters +# Doing the whole message ## Best @@ -208,7 +228,7 @@ ciphertext = [caesar_encipher_letter(p, key) for p in plaintext] \ No newline at end of file