X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=slides%2Fcaesar-encipher.html;h=4ef1d341125d73a444a6d2cbbc01a7e7dce08c7c;hb=b2bff955d11597f85c5e1935165a44fb0e5e487e;hp=1cdf8ddf0ad3cc22da75e10f27385c2f569ce118;hpb=2c871791002cd822e2e6d8a81c9728049d66c2c1;p=cipher-training.git diff --git a/slides/caesar-encipher.html b/slides/caesar-encipher.html index 1cdf8dd..4ef1d34 100644 --- a/slides/caesar-encipher.html +++ b/slides/caesar-encipher.html @@ -1,7 +1,7 @@ - Title + Caesar cipher @@ -35,7 +51,34 @@ ![centre-aligned Caesar wheel](caesarwheel1.gif) -* Letter-by-letter enciphering +Letter-by-letter enciphering + +--- + +layout: true + +.indexlink[[Index](index.html)] + +--- + +# Enciphering and deciphering + +## Arithmetic on letters + +Convert .plaintext[letter] → .plaintext[number] → +.ciphertext[number] → .ciphertext[letter] + +Functions you will need + +```python +ord() + +chr() + +% +``` + +* What are good test cases? --- @@ -43,6 +86,7 @@ ```python import string + string.ascii_letters string.ascii_lowercase string.ascii_uppercase @@ -50,6 +94,11 @@ string.digits string.punctuation ``` +--- +# DRY and YAGNI + +Is your code DRY? + --- # Doctest @@ -57,6 +106,19 @@ string.punctuation * Why document? * Why test? +```python +def caesar_encipher_letter(letter, shift): + """Encipher a letter, given a shift amount + + >>> caesar_encipher_letter('a', 1) + 'b' + """ + if letter in string.ascii_letters: + . + . + . +``` + --- # The magic doctest incantation @@ -71,6 +133,17 @@ if __name__ == "__main__": # Doing all the letters +## Test-first developement + +1. Write the tests. + * They will fail. There is no code. +2. Write code until the tests pass. +3. Refactor. + +--- + +# Doing all the letters + ## Abysmal ```python @@ -83,7 +156,7 @@ for i in range(len(plaintext)): # Doing all the letters -## (Merely) Bad +## Bad ```python ciphertext = '' @@ -91,6 +164,8 @@ for p in plaintext: ciphertext += caesar_encipher_letter(p, key) ``` +...but easily generalisable + --- # Doing all the letters @@ -98,8 +173,7 @@ for p in plaintext: ## Good (but unPythonic) ```python -ciphertext = map(lambda p: caesar_encipher_letter(p, key), - plaintext) +ciphertext = map(lambda p: caesar_encipher_letter(p, key), plaintext) ``` --- @@ -109,16 +183,21 @@ ciphertext = map(lambda p: caesar_encipher_letter(p, key), ## Best ```python -ciphertext = [caesar_encipher_letter(p, key) - for p in plaintext] +ciphertext = [caesar_encipher_letter(p, key) for p in plaintext] ``` +--- + +# Not all iterables are equal +```python +''.join() +``` \ No newline at end of file