From: Neil Smith Date: Sat, 8 Mar 2014 03:05:20 +0000 (-0500) Subject: Finished caesar cipher slides X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=7722e2c6e5d07284cb324c2426c1fc7d47842120;p=cipher-training.git Finished caesar cipher slides --- diff --git a/slides/caesar-encipher.html b/slides/caesar-encipher.html index 1cdf8dd..a4d5b2d 100644 --- a/slides/caesar-encipher.html +++ b/slides/caesar-encipher.html @@ -16,7 +16,6 @@ h2 { font-size: 2em; } h3 { font-size: 1.6em; } a, a > code { - /* color: rgb(249, 38, 114); */ text-decoration: none; } code { @@ -26,6 +25,18 @@ border-radius: 5px; font-size: 16px; } + .plaintext { + background: #272822; + color: #80ff80; + text-shadow: 0 0 20px #333; + padding: 2px 5px; + } + .ciphertext { + background: #272822; + color: #ff6666; + text-shadow: 0 0 20px #333; + padding: 2px 5px; + } @@ -35,7 +46,28 @@ ![centre-aligned Caesar wheel](caesarwheel1.gif) -* Letter-by-letter enciphering +Letter-by-letter enciphering + +--- + +# Enciphering and deciphering + +## Arithmetic on letters + +Convert .plaintext[letter] → .plaintext[number] → +.ciphertext[number] → .ciphertext[letter] + +Functions you will need + +```python +ord() + +chr() + +mod() +``` + +* What are good test cases? --- @@ -43,6 +75,7 @@ ```python import string + string.ascii_letters string.ascii_lowercase string.ascii_uppercase @@ -50,6 +83,11 @@ string.digits string.punctuation ``` +--- +# DRY and YAGNI + +Is your code DRY? + --- # Doctest @@ -57,6 +95,43 @@ 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: +``` + +--- + +# 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 @@ -71,6 +146,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 +169,7 @@ for i in range(len(plaintext)): # Doing all the letters -## (Merely) Bad +## Bad ```python ciphertext = '' @@ -98,8 +184,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,10 +194,15 @@ 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() +```