4 <title>Affine ciphers
</title>
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8"/>
6 <style type=
"text/css">
15 h1 { font-size:
3em; }
16 h2 { font-size:
2em; }
17 h3 { font-size:
1.6em; }
19 text-decoration: none;
22 -moz-border-radius:
5px;
23 -web-border-radius:
5px;
31 text-shadow:
0 0 20px #
333;
37 text-shadow:
0 0 20px #
333;
46 <textarea id=
"source">
48 # Breaking keyword ciphers
50 a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z
51 --|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|--
52 k | e | y | w | o | r | d | a | b | c | f | g | h | i | j | l | m | n | p | q | s | t | u | v | x | z
56 # Duplicate and extend your `affine_break()` function
58 * How to cycle through all the keys? What _are_ all the keys?
66 * `
2013/
4a.ciphertext`
67 * `
2013/
4b.ciphertext`
69 This will take a while. Fire up a system monitor. What's wrong?
73 # Python, threads, and the GIL
75 Thread-safe shared-memory code is hard.
77 Python's Global Interpreter Lock prevents shooting yourself in the foot.
79 Where you want true parallelism, need different threads (Python processes).
80 * Thread-safe shared-memory code is hard.
82 The `multiprocessing` library makes this easier.
84 But before we get there, a couple of diversions...
90 A common task is to apply a function to each item in a sequence, returning a sequence of the results.
96 >>> map(double, [
1,
2,
3])
100 * `map()` is a higher-order function: its first argument is the function that's applied.
102 How can we use this for keyword cipher breaking?
106 # Mapping keyword decipherings
108 Define a function that takes a possible key (keyword and cipher type) and returns the key and its fitness.
110 Use `map()` and `max()` to find the best key
116 How many arguments does this take?
118 How do you write a function that takes this many arguments?
124 ## Positional, keyword
126 * Common or garden parameters, as you're used to.
127 * `def keyword_encipher(message, keyword, wrap_alphabet=
0):`
130 * `def mean(x, *xs):`
132 First number goes in `x`, remaining go in the tuple `xs`
136 * `def myfunc(arg1=
0, **kwargs):`
138 `kwargs` will be a Dict of the remaining keywords (not `arg1`)
142 # Back to `multiprocessing`
144 What does `Pool.starmap()` do?
147 <script src=
"http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type=
"text/javascript">
150 <script type=
"text/javascript"
151 src=
"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
153 <script type=
"text/javascript">
154 var slideshow = remark.create({ ratio:
"16:9" });
159 skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
162 MathJax.Hub.Queue(function() {
163 $(MathJax.Hub.getAllJax()).map(function(index, elem) {
164 return(elem.SourceElement());
165 }).parent().addClass('has-jax');
167 MathJax.Hub.Configured();