4 <title>A Level Miscellany
</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;
51 <textarea id=
"source">
53 # A-level Miscellany ![Open University logo](oulogo_hor.png)
58 * Python file handling
64 .indexlink[![Open University logo](oulogo_hor.png) [Index](index.html)]
70 Convert any of a large range of value to a small range of values
74 1. Putting many things in few buckets
75 2. Cryptographic signing
79 # Hashing many things into a few buckets
80 Useful for dividing items into files, or balancing items across any data structure.
82 Some examples (from [Geoff Kuenning's old course notes](http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200101/homework10/hashfuncs.html))
83 .float-right[![Chaining hash table collisions: from https://commons.wikimedia.org/wiki/File:Hash_table_5_0_1_1_1_1_1_LL.svg#mediaviewer/File:Hash_table_5_0_1_1_1_1_1_LL.svg](Hash_table_5_0_1_1_1_1_1_LL.svg)]
85 * **Division method** (Cormen) Choose a prime that isn't close to a power of
2. h(k) = k mod m. Works badly for many types of patterns in the input data.
87 * **Knuth Variant on Division** h(k) = k(k+
3) mod m. Supposedly works much better than the raw division method.
89 * **Multiplication Method** (Cormen). Choose m to be a power of
2. Let A be some random-looking real number. Knuth suggests M =
0.5*(sqrt(
5) -
1). Then do the following:
91 2. x = fractional part of s
92 3. h(k) = floor(m × x)
94 Problem of hash collisions, where multiple items provide the same hash value. In a data structure, do something like chain multiple values together.
98 # Cryptographic hash functions
100 Maps a large file into a smaller space of values, e.g. SHA-
256 maps any file to a
256-bit digest
102 Cryptographic hashes have additional features:
105 * One way: can't derive a source from a hash
106 * Sensitive: modifying a source changes the hash
107 * Few collisions: different souces have different hashes
109 MD5, SHA-
1 in common use, but broken. SHA-
2 developed by NSA, but seems secure...
111 Useful for validating messages, downloaded files, etc.
113 * Alice posts message and hash
114 * Bob downloads message and hash
115 * Bob computes hash for message and compares to what Alice posted
116 * If they're equal, Bob's happy that the download was successful
117 * If Eve changed the message in transit, the hashes won't be the same
121 # Signing messages: authentication
123 Assume a public key encryption scheme.
125 * Alice finds a message's hash, encrypts it with her private key
126 * Alice sends the message plus encrypted hash to Bob
127 * Bob decrypts the hash with Alice's public key
128 * Bob hashes message and compares it what Alice sent
129 * If they're the same, Bob knows
130 1. The message wasn't changed in transit
131 2. Alice must have sent the message, as only she had the private key
133 See also TU100 Block
5 Part
3 on cryptography and security.
138 Hashes used as proof of work for 'mining' bitcoins.
140 * Block is transaction history + nonce word
141 * Challenge is to find a nonce word that gives a hash in the correct range
142 * No shortcut: must try many, many nonces and see what works
146 # Boolean algegra identities
148 |Rule | Left | Right |
150 |`\(\text{Associativity of } \vee \)`|`\( x \vee (y \vee z) \)`|`\( = (x \vee y) \vee z \)`|
151 |`\(\text{Associativity of } \wedge \)`|`\( x \wedge (y \wedge z) \)`|`\( = (x \wedge y) \wedge z \)`|
152 |`\(\text{Commutativity of } \vee \)`|`\( x \vee y \)`|`\( = y \vee x \)`|
153 |`\(\text{Commutativity of } \wedge \)`|`\( x \wedge y \)`|`\( = y \wedge x \)`|
154 |`\(\text{Distributivity of } \wedge \text{ over } \vee \quad \)`|`\( x \wedge (y \vee z) \)`|`\( = (x \wedge y) \vee (x \wedge z) \)`|
155 |`\(\text{Identity for } \vee \)`|`\( x \vee
0 \)`|`\( = x \)`|
156 |`\(\text{Identity for } \wedge \)`|`\( x \wedge
1 \)`|`\( = x \)`|
157 |`\(\text{Annihilator for } \wedge \)`|`\( x \wedge
0 \)`|`\( =
0 \)`|
158 |`\(\text{Double negation } \)`|`\( \neg \neg x \)`|`\( = x \)`|
159 |`\(\text{De Morgan's Law } \)`|`\( \neg ( x \wedge y ) \)`|`\( = \neg x \vee \neg y \)`|
160 |`\(\text{De Morgan's Law } \)`|`\( \neg ( x \vee y ) \)`|`\( = \neg x \wedge \neg y \)`|
162 You can prove all these by truth tables.
168 `\( \wedge = \& = × = \cdot = \text{and}\)` ; `\( \vee = | = + = \text{or}\)` ; `\( \neg x = \: \sim x = x' = \overline x = \text{not}\)`
172 `implies` is a connective: `\(a \text{ implies } b = a \rightarrow b = a \supset b = \neg a \vee b \)`
174 `\( A \)` | `\( B \)` | `\( \neg A \)` | `\( \neg A \vee B \)` | `\( A \rightarrow B \)`
175 :--------:|:---------:|:--------------:|:---------------------:|:-----------------------:
181 If A is true, B must be true. If A is false, B can be anything.
183 Use other notation for derivations and proofs: see your friendly mathematicians.
189 .float-right[![Karnaugh maps](Karnaugh_map2.png)]
191 A way of visually simplifying Boolean expressions.
193 Draw the biggest loops you can, don't worry if the overlap.
197 # File handling in Python
199 * Reading, writing appending
200 * `read()` and `readlines()`
208 <script src=
"http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type=
"text/javascript">
211 <script type=
"text/javascript"
212 src=
"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
214 <script type=
"text/javascript">
215 var slideshow = remark.create({ ratio:
"16:9" });
220 skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
223 MathJax.Hub.Queue(function() {
224 $(MathJax.Hub.getAllJax()).map(function(index, elem) {
225 return(elem.SourceElement());
226 }).parent().addClass('has-jax');
228 MathJax.Hub.Configured();