<!DOCTYPE html>
<html>
<head>
- <title>Affine ciphers</title>
+ <title>Keyword ciphers</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
/* Slideshow styles */
color: #ff6666;
text-shadow: 0 0 20px #333;
padding: 2px 5px;
+ }
+ .indexlink {
+ position: absolute;
+ bottom: 1em;
+ left: 1em;
}
.float-right {
float: right;
---
+layout: true
+
+.indexlink[[Index](index.html)]
+
+---
+
# The cipher
* Still character-by-character substitution, still monosubstitution.
# Keyword arguments
Used to:
+
1. give a default value for a parameter
2. allow parameters to be named (not just positional)
Pass this parameter to the `keyword_alphabet` procedure.
-## Note: `Enum` introduced in Python 3.4. This is a better solution.
+## wrap_alphabet has no inherent meaning
+Use Python 3.4's `Enum`
+```python
+from enum import Enum
+class KeywordWrapAlphabet(Enum):
+ from_a = 1
+ from_last = 2
+ from_largest = 3
+```
+
+(Use integers in earlier Pythons)
---
# Deduplicating a sequence
* Santitise the keyword before we use it
+---
+# Making the keyword alphabet
+
## Cases
1. As we're deduplicating anyway, just add the entire alphabet to the end of the keyword, then deduplicate.
`deduplicate(keyword + string.ascii_lowercase)`
2. and 3. How to find the appropriate letter of the keyword.
-Indexing pulls out letters. `'keyword'[0]` = 'k' ; `'keyword'[3]` = 'w' ; `'keyword'[-1]` = 'd'
-Slices pulls out substrings. `'keyword'[1:4]` = 'eyw' ; `'keyword'[:3]` = 'key' ; `'keyword'[5:]` = 'rd'
-
`deduplicate(keyword + string_ascii_lowercase[from:] + string.ascii_lowercase)`
Question: why not take a slice of the second alphabet copy?
`sorted()` will put a string in lexicographical order.
`.find()` will find an item in a sequence
+---
+
+# Keyword enciphering
+
+Now we've got the keyword-based cipher alphabet, simply use `.translate()` to do the enciphering/deciphering.
+
+Use `''.maketrans()` to make the translation table.
+
+Sorted!
+
+Does it pass the tests?
</textarea>
<script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">