Finished for the day
[cipher-training.git] / slides / caesar-encipher.html
index 1cdf8ddf0ad3cc22da75e10f27385c2f569ce118..f45a166ca80d71f37605364c4dac44c9e43925d1 100644 (file)
@@ -1,7 +1,7 @@
 <!DOCTYPE html>
 <html>
   <head>
-    <title>Title</title>
+    <title>Caesar cipher</title>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
     <style type="text/css">
       /* Slideshow styles */
         font-weight: 400;
         margin-bottom: 0;
       }
-      h1 { font-size: 4em; }
+      h1 { font-size: 3em; }
       h2 { font-size: 2em; }
       h3 { font-size: 1.6em; }
       a, a > code {
-        /* color: rgb(249, 38, 114); */
         text-decoration: none;
       }
       code {
         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;
+      }
     </style>
   </head>
   <body>
 
 ![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()
+
+%
+```
+
+* 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,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
@@ -69,6 +120,60 @@ if __name__ == "__main__":
 
 ---
 
+# Accents
+
+```python
+>>> caesar_encipher_letter('é', 1)
+```
+What does it produce?
+
+What should it produce?
+
+## Unicode, combining codepoints, and normal forms
+
+Text encodings will bite you when you least expect it.
+
+* urlencoding is the other pain point.
+
+---
+
+# Five minutes on StackOverflow later...
+
+```python
+def unaccent(text):
+    """Remove all accents from letters. 
+    It does this by converting the unicode string to decomposed compatibility
+    form, dropping all the combining accents, then re-encoding the bytes.
+
+    >>> unaccent('hello')
+    'hello'
+    >>> unaccent('HELLO')
+    'HELLO'
+    >>> unaccent('héllo')
+    'hello'
+    >>> unaccent('héllö')
+    'hello'
+    >>> unaccent('HÉLLÖ')
+    'HELLO'
+    """
+    return unicodedata.normalize('NFKD', text).\
+        encode('ascii', 'ignore').\
+        decode('utf-8')
+```
+
+---
+
+# 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
@@ -83,7 +188,7 @@ for i in range(len(plaintext)):
 
 # Doing all the letters
 
-## (Merely) Bad
+## Bad
 
 ```python
 ciphertext = ''
@@ -91,6 +196,8 @@ for p in plaintext:
     ciphertext += caesar_encipher_letter(p, key)
 ```
 
+...but easily generalisable
+
 ---
 
 # Doing all the letters
@@ -98,8 +205,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 +215,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()
+```
 
     </textarea>
     <script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
     </script>
     <script type="text/javascript">
-      var slideshow = remark.create();
+      var slideshow = remark.create({ ratio: "16:9" });
     </script>
   </body>
 </html>
\ No newline at end of file