From: Neil Smith Date: Thu, 8 Jun 2017 14:49:43 +0000 (+0100) Subject: Clarified instructions X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;ds=sidebyside;h=1eee453aa49cca632fbaca9cd3dfda7642fd4387;p=ou-summer-of-code-2017.git Clarified instructions --- diff --git a/03-door-codes/door-codes-solution.ipynb b/03-door-codes/door-codes-solution.ipynb index 3f39c49..43b633e 100644 --- a/03-door-codes/door-codes-solution.ipynb +++ b/03-door-codes/door-codes-solution.ipynb @@ -21,18 +21,18 @@ "2. \"Add\" the current letter of the pass phrase to the second letter of the code, replacing the second letter of the code\n", "3. Move on to the next letter of the pass phrase\n", "\n", - "\"Adding\" letters is done by converting the letters to their position in the alphabet (starting at one), adding, then converting the number back to a letter. Numbers greater than 26 are \"wrapped around\". For instance, to add `u` + `k`, convert them to numbers (`u`=`21`, `k`=`11`), add them (`21` + `11` = `32`), then convert back to a letter (`32` is larger than 26, so it becomes `32` - 26 = `6`, which is `f`).\n", + "\"Adding\" letters is done by converting the letters to their position in the alphabet (starting at one), adding, then converting the number back to a letter. Numbers greater than 26 are \"wrapped around\". For instance, to add `t` + `h`, convert them to numbers (`t`=`20`, `h`=`8`), add them (`20` + `8` = `28`), then convert back to a letter (`28` is larger than 26, so it becomes `28` - `26` = `2`, which is `b`).\n", "\n", - "Anything that isn't a lower-case letter is ignored.\n", + "All letters are converted to lower-case, and anything that isn't a letter is ignored.\n", "\n", "For example, to find the code from the pass phrase `the cat`, the code starts as being the first two letters `th`, then the subsequent letters are used to update the code as follows:\n", "\n", "| old code | code as
numbers | passphrase
letter | number of
letter | new first
part of code |new second
part of code | new code |\n", "|:---|:---|:---|:---|:---|:---|:---|\n", - "| th | (20, 8) | e | 5 | 28 | 13 | bm |\n", - "| bm | (2, 13) | c | 3 | 15 | 16 | op |\n", - "| op | (15, 16) | a | 1 | 31 | 17 | eq |\n", - "| eq | (5, 17) | t | 20 | 22 | 37 | vk |\n", + "| `th` | (20, 8) | `e` | 5 | 28 | 13 | `bm` |\n", + "| `bm` | (2, 13) | `c` | 3 | 15 | 16 | `op` |\n", + "| `op` | (15, 16) | `a` | 1 | 31 | 17 | `eq` |\n", + "| `eq` | (5, 17) | `t` | 20 | 22 | 37 | `vk` |\n", "\n", "So the final code is `vk`\n", "\n", @@ -45,7 +45,7 @@ }, { "cell_type": "code", - "execution_count": 98, + "execution_count": 2, "metadata": { "collapsed": true }, @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 3, "metadata": { "collapsed": true }, @@ -71,7 +71,27 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('a', 'z', 'z', 'a')" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c(1), c(0), c(26), c(27)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, "metadata": { "collapsed": true }, @@ -83,8 +103,10 @@ }, { "cell_type": "code", - "execution_count": 101, - "metadata": {}, + "execution_count": 26, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def whash1(word, show_steps=False):\n", @@ -99,10 +121,15 @@ " h[0] = c(o(h[0]) + o(h[1]))\n", " h[1] = c(o(h[1]) + o(l))\n", " if show_steps:\n", - " print('| {} | {} | {} | {} | {} | {} | {} |'.format(\n", + " print('| `{}` | {} | `{}` | {} | {} | {} | `{}` |'.format(\n", " ''.join(h_before), \n", " (o(h_before[0]), o(h_before[1])),\n", - " l, \n", + " l, | old code | code as
numbers | passphrase
letter | number of
letter | new first
part of code |new second
part of code | new code |\n", + "|:---|:---|:---|:---|:---|:---|:---|\n", + "| `th` | (20, 8) | `e` | 5 | 28 | 13 | `bm` |\n", + "| `bm` | (2, 13) | `c` | 3 | 15 | 16 | `op` |\n", + "| `op` | (15, 16) | `a` | 1 | 31 | 17 | `eq` |\n", + "| `eq` | (5, 17) | `t` | 20 | 22 | 37 | `vk` |\n", " o(l), \n", " o(h_before[0]) + o(h_before[1]), \n", " o(h_before[1]) + o(l), \n", @@ -112,7 +139,7 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 27, "metadata": {}, "outputs": [ { @@ -121,10 +148,10 @@ "text": [ "| old code | code as
numbers | passphrase
letter | number of
letter | new first
part of code |new second
part of code | new code |\n", "|:---|:---|:---|:---|:---|:---|:---|\n", - "| th | (20, 8) | e | 5 | 28 | 13 | bm |\n", - "| bm | (2, 13) | c | 3 | 15 | 16 | op |\n", - "| op | (15, 16) | a | 1 | 31 | 17 | eq |\n", - "| eq | (5, 17) | t | 20 | 22 | 37 | vk |\n" + "| `th` | (20, 8) | `e` | 5 | 28 | 13 | `bm` |\n", + "| `bm` | (2, 13) | `c` | 3 | 15 | 16 | `op` |\n", + "| `op` | (15, 16) | `a` | 1 | 31 | 17 | `eq` |\n", + "| `eq` | (5, 17) | `t` | 20 | 22 | 37 | `vk` |\n" ] }, { @@ -133,7 +160,7 @@ "'vk'" ] }, - "execution_count": 102, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -144,7 +171,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 7, "metadata": { "collapsed": true }, @@ -156,7 +183,7 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -165,7 +192,7 @@ "'mc'" ] }, - "execution_count": 104, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -184,29 +211,38 @@ "\n", "Indeed, the algorithm has been changed, \"to improve security,\" they say.\n", "\n", - "The new algorithm starts with the same initial value of the code for every passphrase. In in this case, the initial code is `ri`. The algorithm also uses two contants, `m1`= 5 and `m2` = 11.\n", + "The new algorithm starts with the same initial value of the code for every passphrase. In in this case, the initial code is `ri`. The algorithm also uses two contants, `alpha`= 5 and `beta` = 11.\n", "\n", "Then, for each letter in the pass phrase, the code is updated as follows:\n", "\n", - "1. \"Add\" the second letter of the code to the first letter of the code \"multiplied by\" `m1`, replacing the first letter of the code\n", - "2. \"Add\" the current letter of the pass phrase to the second letter of the code \"multiplied by\" `m2`, replacing the second letter of the code\n", + "1. \"Multiply\" the second letter of the code by `alpha` then add the first letter of the code, replacing the first code letter\n", + "2. \"Multiply\" the current letter of the pass phrase by `beta` and add the second letter of the code, replacing the second letter of the code\n", "\n", "\"Multiplying\" letters is done by converting the letters to their position in the alphabet (starting at one) and multiplying. For instance, to multiply `u` by 11, convert `u` to `21`, multiply by 11 (`21` × `11` = `231`), then convert back to a letter (`231` is larger than 26, so it becomes `22`, which is `w`).\n", "\n", "Again, anything that isn't a lower-case letter is ignored.\n", "\n", - "For example, to find the code from the pass phrase `the cat`, the code starts as being the first two letters `th`, then the subsequent letters are used to update the code as follows:\n", + "For example, to find the code from the pass phrase `the cat`, the code starts as being the first two letters `ri`. When the first letter is encrypted, the first letter of the code becomes:\n", + "\n", + "`r` + `i` × `alpha` = `18` + `9` × `5` = `63` = `k`\n", + "\n", + "and the second letter of the code becomes:\n", + "\n", + "`i` + `t` × `beta` = `9` + `20` × `11` = `229` = `u`\n", + "\n", + "The passphrase letters are used to update the code as follows:\n", "\n", "| old code | code as
numbers | passphrase
letter | number of
letter | new first
part of code |new second
part of code | new code |\n", "|:---|:---|:---|:---|:---|:---|:---|\n", - "| ri | (18, 9) | t | 20 | 63 | 229 | ku |\n", - "| ku | (11, 21) | h | 8 | 116 | 109 | le |\n", - "| le | (12, 5) | e | 5 | 37 | 60 | kh |\n", - "| kh | (11, 8) | c | 3 | 51 | 41 | yo |\n", - "| yo | (25, 15) | a | 1 | 100 | 26 | vz |\n", - "| vz | (22, 26) | t | 20 | 152 | 246 | vl |\n", + "| `ri` | (18, 9) | `t` | 20 | 63 | 229 | `ku` |\n", + "| `ku` | (11, 21) | `h` | 8 | 116 | 109 | `le` |\n", + "| `le` | (12, 5) | `e` | 5 | 37 | 60 | `kh` |\n", + "| `kh` | (11, 8) | `c` | 3 | 51 | 41 | `yo` |\n", + "| `yo` | (25, 15) | `a` | 1 | 100 | 26 | `vz` |\n", + "| `vz` | (22, 26) | `t` | 20 | 152 | 246 | `vl` |\n", + "\n", "\n", - "So the final code is `pl`\n", + "So the final code is `vl`\n", "\n", "## Part 2\n", "Your passphrase remains \"the traveller in the grey riding-coat, who called himself mr. melville, was contemplating the malice of which the gods are capable.\"\n", @@ -216,7 +252,7 @@ }, { "cell_type": "code", - "execution_count": 105, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -225,7 +261,7 @@ "(21, 231, 22, 'w')" ] }, - "execution_count": 105, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -236,11 +272,53 @@ }, { "cell_type": "code", - "execution_count": 113, + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(18, 9, 45, 63, 'k')" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "o('r'), o('i'), o('i') * 5, o('r') + o('i') * 5, c(o('r') + o('i') * 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(9, 20, 220, 229, 'u')" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "o('i'), o('t'), o('t') * 11, o('i') + o('t') * 11, c(o('i') + o('t') * 11)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ - "def whash2(word, h0=None, m1=5, m2=11, show_steps=False):\n", + "def whash2(word, h0=None, alpha=5, beta=11, show_steps=False):\n", " if show_steps:\n", " print('| old code | code as
numbers | passphrase
letter | number of
letter | new first
part of code |'\n", " 'new second
part of code | new code |')\n", @@ -252,23 +330,23 @@ " for l in word:\n", " if show_steps:\n", " h_before = h.copy()\n", - " h[0] = c(o(h[0]) + o(h[1]) * m1)\n", - " h[1] = c(o(h[1]) + o(l) * m2)\n", + " h[0] = c(o(h[0]) + o(h[1]) * alpha)\n", + " h[1] = c(o(h[1]) + o(l) * beta)\n", " if show_steps:\n", - " print('| {} | {} | {} | {} | {} | {} | {} |'.format(\n", + " print('| `{}` | {} | `{}` | {} | {} | {} | `{}` |'.format(\n", " ''.join(h_before), \n", " (o(h_before[0]), o(h_before[1])),\n", " l, \n", " o(l), \n", - " o(h_before[0]) + o(h_before[1]) * m1, \n", - " o(h_before[1]) + o(l) * m2, \n", + " o(h_before[0]) + o(h_before[1]) * alpha, \n", + " o(h_before[1]) + o(l) * beta, \n", " ''.join(h)))\n", " return ''.join(h)" ] }, { "cell_type": "code", - "execution_count": 116, + "execution_count": 25, "metadata": {}, "outputs": [ { @@ -277,12 +355,12 @@ "text": [ "| old code | code as
numbers | passphrase
letter | number of
letter | new first
part of code |new second
part of code | new code |\n", "|:---|:---|:---|:---|:---|:---|:---|\n", - "| ri | (18, 9) | t | 20 | 63 | 229 | ku |\n", - "| ku | (11, 21) | h | 8 | 116 | 109 | le |\n", - "| le | (12, 5) | e | 5 | 37 | 60 | kh |\n", - "| kh | (11, 8) | c | 3 | 51 | 41 | yo |\n", - "| yo | (25, 15) | a | 1 | 100 | 26 | vz |\n", - "| vz | (22, 26) | t | 20 | 152 | 246 | vl |\n" + "| `ri` | (18, 9) | `t` | 20 | 63 | 229 | `ku` |\n", + "| `ku` | (11, 21) | `h` | 8 | 116 | 109 | `le` |\n", + "| `le` | (12, 5) | `e` | 5 | 37 | 60 | `kh` |\n", + "| `kh` | (11, 8) | `c` | 3 | 51 | 41 | `yo` |\n", + "| `yo` | (25, 15) | `a` | 1 | 100 | 26 | `vz` |\n", + "| `vz` | (22, 26) | `t` | 20 | 152 | 246 | `vl` |\n" ] }, { @@ -291,7 +369,7 @@ "'vl'" ] }, - "execution_count": 116, + "execution_count": 25, "metadata": {}, "output_type": "execute_result" } @@ -302,7 +380,7 @@ }, { "cell_type": "code", - "execution_count": 117, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -311,7 +389,7 @@ "'qb'" ] }, - "execution_count": 117, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -346,7 +424,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.2+" + "version": "3.6.1" } }, "nbformat": 4,