From 98a88e5d7272254b1b1f6172f4c1e5b848634287 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 10 Oct 2014 07:50:12 +0100 Subject: [PATCH] More tweaks --- hangman-better.ipynb | 195 +++++++++++++++++++++++++++---------------- programming.html | 15 +++- 2 files changed, 135 insertions(+), 75 deletions(-) diff --git a/hangman-better.ipynb b/hangman-better.ipynb index 87426dd..7691ed1 100644 --- a/hangman-better.ipynb +++ b/hangman-better.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:6b8d6551e13b3d60196077d54315af6fd1f78b4de386098da7115fe7cf3112fb" + "signature": "sha256:07f366ec645d178071697c25c43d438fd80cf3a676ad9bd8d7613f5bfa36adf5" }, "nbformat": 3, "nbformat_minor": 0, @@ -130,7 +130,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 37 + "prompt_number": 5 }, { "cell_type": "code", @@ -192,11 +192,11 @@ "output_type": "stream", "stream": "stdout", "text": [ - "42\n" + "43\n" ] } ], - "prompt_number": 38 + "prompt_number": 8 }, { "cell_type": "code", @@ -217,7 +217,7 @@ "output_type": "stream", "stream": "stdout", "text": [ - "330\n" + "316\n" ] } ], @@ -242,7 +242,7 @@ "output_type": "stream", "stream": "stdout", "text": [ - "8\n" + "5\n" ] } ], @@ -320,7 +320,7 @@ "output_type": "stream", "stream": "stdout", "text": [ - "472\n" + "473\n" ] } ], @@ -377,7 +377,7 @@ "output_type": "stream", "stream": "stdout", "text": [ - "452\n" + "489\n" ] } ], @@ -436,7 +436,7 @@ "output_type": "stream", "stream": "stdout", "text": [ - "975\n" + "979\n" ] } ], @@ -451,7 +451,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 50 + "prompt_number": 19 }, { "cell_type": "code", @@ -486,7 +486,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 62 + "prompt_number": 20 }, { "cell_type": "code", @@ -507,11 +507,11 @@ "output_type": "stream", "stream": "stdout", "text": [ - "491\n" + "512\n" ] } ], - "prompt_number": 63 + "prompt_number": 21 }, { "cell_type": "code", @@ -550,7 +550,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 25 + "prompt_number": 22 }, { "cell_type": "code", @@ -571,11 +571,11 @@ "output_type": "stream", "stream": "stdout", "text": [ - "995\n" + "990\n" ] } ], - "prompt_number": 27 + "prompt_number": 23 }, { "cell_type": "code", @@ -598,8 +598,8 @@ "output_type": "stream", "stream": "stdout", "text": [ - "992\n", - "994" + "986\n", + "996" ] }, { @@ -607,7 +607,7 @@ "stream": "stdout", "text": [ "\n", - "989" + "991" ] }, { @@ -623,11 +623,11 @@ "stream": "stdout", "text": [ "\n", - "1 loops, best of 3: 33.9 s per loop\n" + "1 loops, best of 3: 57.2 s per loop\n" ] } ], - "prompt_number": 28 + "prompt_number": 24 }, { "cell_type": "code", @@ -662,7 +662,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 29 + "prompt_number": 25 }, { "cell_type": "code", @@ -686,7 +686,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 30 + "prompt_number": 26 }, { "cell_type": "code", @@ -700,7 +700,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 31 + "prompt_number": 27 }, { "cell_type": "code", @@ -715,7 +715,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 32 + "prompt_number": 28 }, { "cell_type": "code", @@ -734,7 +734,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 33 + "prompt_number": 29 }, { "cell_type": "code", @@ -757,8 +757,8 @@ "output_type": "stream", "stream": "stdout", "text": [ - "465\n", - "458" + "463\n", + "492" ] }, { @@ -766,7 +766,7 @@ "stream": "stdout", "text": [ "\n", - "483" + "451" ] }, { @@ -774,7 +774,7 @@ "stream": "stdout", "text": [ "\n", - "482" + "448" ] }, { @@ -782,11 +782,11 @@ "stream": "stdout", "text": [ "\n", - "1 loops, best of 3: 18.5 s per loop\n" + "1 loops, best of 3: 30.9 s per loop\n" ] } ], - "prompt_number": 34 + "prompt_number": 30 }, { "cell_type": "code", @@ -809,8 +809,8 @@ "output_type": "stream", "stream": "stdout", "text": [ - "976\n", - "983" + "985\n", + "972" ] }, { @@ -818,7 +818,7 @@ "stream": "stdout", "text": [ "\n", - "983" + "976" ] }, { @@ -826,7 +826,7 @@ "stream": "stdout", "text": [ "\n", - "985" + "986" ] }, { @@ -834,11 +834,11 @@ "stream": "stdout", "text": [ "\n", - "1 loops, best of 3: 40.4 s per loop\n" + "1 loops, best of 3: 1min 8s per loop\n" ] } ], - "prompt_number": 35 + "prompt_number": 31 }, { "cell_type": "code", @@ -861,8 +861,8 @@ "output_type": "stream", "stream": "stdout", "text": [ - "513\n", - "507" + "502\n", + "491" ] }, { @@ -870,7 +870,7 @@ "stream": "stdout", "text": [ "\n", - "509" + "537" ] }, { @@ -878,7 +878,7 @@ "stream": "stdout", "text": [ "\n", - "523" + "524" ] }, { @@ -886,11 +886,11 @@ "stream": "stdout", "text": [ "\n", - "1 loops, best of 3: 8min 15s per loop\n" + "1 loops, best of 3: 13min 6s per loop\n" ] } ], - "prompt_number": 40 + "prompt_number": 32 }, { "cell_type": "code", @@ -913,8 +913,8 @@ "output_type": "stream", "stream": "stdout", "text": [ - "992\n", - "995" + "993\n", + "991" ] }, { @@ -922,7 +922,7 @@ "stream": "stdout", "text": [ "\n", - "998" + "994" ] }, { @@ -938,61 +938,108 @@ "stream": "stdout", "text": [ "\n", - "1 loops, best of 3: 34.4 s per loop\n" + "1 loops, best of 3: 56.6 s per loop\n" ] } ], - "prompt_number": 41 + "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ - "%%timeit\n", - "\n", - "wins = 0\n", "for _ in range(1000):\n", - " g = Game(random.choice(WORDS), player=PlayerAdaptivePatternNegLookahead(WORDS))\n", + " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n", " g.play_game()\n", - " if g.game_won:\n", - " wins += 1\n", - "print(wins)" + " if not g.game_won:\n", + " print(g.target, g.discovered, g.wrong_letters)" ], "language": "python", "metadata": {}, "outputs": [ { - "ename": "NameError", - "evalue": "name 'PlayerAdaptivePatternNegLookahead' is not defined", - "output_type": "pyerr", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mget_ipython\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun_cell_magic\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'timeit'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m''\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'\\nwins = 0\\nfor _ in range(1000):\\n g = Game(random.choice(WORDS), player=PlayerAdaptivePatternNegLookahead(WORDS))\\n g.play_game()\\n if g.game_won:\\n wins += 1\\nprint(wins)'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;32m/usr/local/lib/python3.4/dist-packages/IPython/core/interactiveshell.py\u001b[0m in \u001b[0;36mrun_cell_magic\u001b[1;34m(self, magic_name, line, cell)\u001b[0m\n\u001b[0;32m 2160\u001b[0m \u001b[0mmagic_arg_s\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvar_expand\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mline\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstack_depth\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2161\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbuiltin_trap\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2162\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmagic_arg_s\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcell\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2163\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2164\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;32m/usr/local/lib/python3.4/dist-packages/IPython/core/magics/execution.py\u001b[0m in \u001b[0;36mtimeit\u001b[1;34m(self, line, cell)\u001b[0m\n", - "\u001b[1;32m/usr/local/lib/python3.4/dist-packages/IPython/core/magic.py\u001b[0m in \u001b[0;36m\u001b[1;34m(f, *a, **k)\u001b[0m\n\u001b[0;32m 191\u001b[0m \u001b[1;31m# but it's overkill for just that one bit of state.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 192\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmagic_deco\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 193\u001b[1;33m \u001b[0mcall\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 194\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 195\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mcallable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;32m/usr/local/lib/python3.4/dist-packages/IPython/core/magics/execution.py\u001b[0m in \u001b[0;36mtimeit\u001b[1;34m(self, line, cell)\u001b[0m\n\u001b[0;32m 1011\u001b[0m \u001b[0mnumber\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1012\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0m_\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1013\u001b[1;33m \u001b[1;32mif\u001b[0m \u001b[0mtimer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>=\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1014\u001b[0m \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1015\u001b[0m \u001b[0mnumber\u001b[0m \u001b[1;33m*=\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;32m/usr/lib/python3.4/timeit.py\u001b[0m in \u001b[0;36mtimeit\u001b[1;34m(self, number)\u001b[0m\n\u001b[0;32m 176\u001b[0m \u001b[0mgc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdisable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 177\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 178\u001b[1;33m \u001b[0mtiming\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minner\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mit\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtimer\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 179\u001b[0m \u001b[1;32mfinally\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 180\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mgcold\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;32m\u001b[0m in \u001b[0;36minner\u001b[1;34m(_it, _timer)\u001b[0m\n", - "\u001b[1;31mNameError\u001b[0m: name 'PlayerAdaptivePatternNegLookahead' is not defined" + "output_type": "stream", + "stream": "stdout", + "text": [ + "jutting ['_', 'u', 't', 't', 'i', 'n', 'g'] ['e', 'a', 'o', 'l', 's', 'f', 'p', 'b', 'c', 'r']\n", + "faze" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " ['_', 'a', '_', 'e'] ['r', 'l', 'm', 'p', 's', 'g', 'b', 'd', 'v', 'k']\n", + "fate" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " ['_', 'a', '_', 'e'] ['r', 'l', 'm', 'p', 's', 'g', 'b', 'd', 'v', 'k']\n", + "dunk" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " ['_', 'u', 'n', 'k'] ['e', 's', 'o', 'a', 'i', 'l', 'f', 'r', 'j', 'p']\n", + "loons" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " ['_', 'o', 'o', 'n', 's'] ['e', 't', 'k', 'm', 'p', 'd', 'f', 'c', 'b', 'g']\n", + "lab" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " ['_', 'a', 'b'] ['t', 'p', 'g', 'w', 'm', 'd', 'y', 'r', 'c', 'n']\n", + "joked" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " ['_', 'o', 'k', 'e', 'd'] ['s', 'a', 'w', 'p', 't', 'r', 'h', 'b', 'y', 'c']\n" ] } ], - "prompt_number": 42 + "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ - "for _ in range(1000):\n", + "iterations = 10000\n", + "wins = 0\n", + "for _ in range(iterations):\n", " g = Game(random.choice(WORDS), player=PlayerAdaptivePattern(WORDS))\n", " g.play_game()\n", - " if not g.game_won:\n", - " print(g.target, g.discovered, g.wrong_letters)" + " if g.game_won:\n", + " wins += 1\n", + "print(wins / iterations)" ], "language": "python", "metadata": {}, - "outputs": [] + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0.9923\n" + ] + } + ], + "prompt_number": 35 }, { "cell_type": "code", diff --git a/programming.html b/programming.html index 64f013f..3323da4 100644 --- a/programming.html +++ b/programming.html @@ -203,7 +203,7 @@ Must be part of your mental model * Use any type as the value ```python -neil = {'first_name': Neil', 'surname': Smith', 'age': 44} +neil = {'first_name': 'Neil', 'surname': 'Smith', 'age': 44} neil['surname'] ``` @@ -419,8 +419,21 @@ p.x + p.y --- +# Data, not algorithms + +Don't put complex, changable logic into code + +Build a data structure that describes it, code that reads it. + +* Euler 11 directions +* Multiple choice quiz + +--- + # Algorithms: trading space for time +## Euler 14 + -- 2.34.1