From 5347be263de306d813f09432169d2254874c9acf Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 29 Jun 2017 10:01:55 +0100 Subject: [PATCH] Got the solutions working --- 09-resolving-the-bill/interleaving.ipynb | 1547 +++++++++++++++++- 09-resolving-the-bill/subsequence.ipynb | 573 +++++++ 09-resolving-the-bill/tipping-solution.ipynb | 604 ------- 3 files changed, 2054 insertions(+), 670 deletions(-) create mode 100644 09-resolving-the-bill/subsequence.ipynb delete mode 100644 09-resolving-the-bill/tipping-solution.ipynb diff --git a/09-resolving-the-bill/interleaving.ipynb b/09-resolving-the-bill/interleaving.ipynb index 22140c9..e115998 100644 --- a/09-resolving-the-bill/interleaving.ipynb +++ b/09-resolving-the-bill/interleaving.ipynb @@ -19,7 +19,19 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import random\n", + "import string" + ] + }, + { + "cell_type": "code", + "execution_count": 139, "metadata": { "collapsed": true }, @@ -35,9 +47,7 @@ { "cell_type": "code", "execution_count": 8, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -58,15 +68,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "`dp_table[i, j]` is True if `s3[:i+j]` can be formed from interleaving `s1[:i]` and `s2[:j]`." + "`dp_table[i, j]` is True if first `i` + `j` characters of `s3` can be formed from interleaving of first `i` characters of `s1` and first `j` characters of `s2`." ] }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, + "execution_count": 9, + "metadata": {}, "outputs": [ { "data": { @@ -79,7 +87,7 @@ " [False, False, False, False, False, False]]" ] }, - "execution_count": 11, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -92,9 +100,8 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 10, "metadata": { - "collapsed": false, "scrolled": true }, "outputs": [ @@ -139,7 +146,7 @@ " (5, 5): False}" ] }, - "execution_count": 24, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -153,7 +160,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 135, "metadata": { "collapsed": true }, @@ -167,21 +174,33 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 136, "metadata": { - "collapsed": false + "collapsed": true }, + "outputs": [], + "source": [ + "def show_table(table):\n", + " return '\\n'.join(\n", + " ' '.join('T' if table[i, j] else '.' for j in sorted(set([k[1] for k in table])))\n", + " for i in sorted(set([k[0] for k in table]))) " + ] + }, + { + "cell_type": "code", + "execution_count": 137, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "F F F F F F\n", - "F F F F F F\n", - "F F F F F F\n", - "F F F F F F\n", - "F F F F F F\n", - "F F F F F F\n" + "T . . . . .\n", + "T . . . . .\n", + "T T T T . .\n", + ". T T T . .\n", + ". . . . . .\n", + ". . . . . .\n" ] } ], @@ -191,10 +210,8 @@ }, { "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, + "execution_count": 140, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -239,12 +256,12 @@ "xx 5 3 c b c False\n", "xx 5 4 c c c False\n", "xx 5 5 c a c False\n", - "T F F F F F\n", - "T F F F F F\n", - "T T T T F F\n", - "F T T T F F\n", - "F F F F F F\n", - "F F F F F F\n" + "T . . . . .\n", + "T . . . . .\n", + "T T T T . .\n", + ". T T T . .\n", + ". . . . . .\n", + ". . . . . .\n" ] }, { @@ -260,7 +277,7 @@ " (3, 3): (2, 3, 'b', 's1')}" ] }, - "execution_count": 39, + "execution_count": 140, "metadata": {}, "output_type": "execute_result" } @@ -313,23 +330,27 @@ }, { "cell_type": "code", - "execution_count": 49, - "metadata": { - "collapsed": false - }, + "execution_count": 83, + "metadata": {}, "outputs": [], "source": [ - "def is_interleave(seq1, seq2, seq3, return_backpointers=False, debug=False):\n", + "def is_interleave(seq1, seq2, seq3, return_backpointers=False, return_table=False, debug=False):\n", " \"\"\"Return true if seq3 is some interleaved merge of seq1 and seq2.\n", " If return_backpointers, also return the set of backpointers to\n", " reconstruct the interleaving\"\"\"\n", " \n", - " # dp_table[i, j] is True if seq3[:i+j-1] is made up of an interleaving\n", - " # of the first i characters of seq1 and the first j characters of seq2\n", + " # dp_table[i, j] is True if first i+j characters of seq is made up of \n", + " # an interleaving of the first i characters of seq1 and the \n", + " # first j characters of seq2\n", " \n", " if len(seq1) + len(seq2) != len(seq3):\n", - " if return_backpointers:\n", - " return False, {}\n", + " if return_backpointers or return_table:\n", + " retval = [False]\n", + " if return_backpointers:\n", + " retval += [{}]\n", + " if return_table:\n", + " retval += [{}]\n", + " return tuple(retval)\n", " else:\n", " return False\n", " \n", @@ -343,7 +364,7 @@ " for j in range(len(seq2)+1):\n", " if i == 0 and j == 0:\n", " dp_table[i, j] = True\n", - " if debug: print('aa', i, j, '!', '!', '!', dp_table[i, j])\n", + " if debug: print('xxxx', i, j, '!', '!', '!', dp_table[i, j])\n", " elif i == 0:\n", " # extend by character from seq2\n", " if dp_table[i, j-1] and seq2[j-1] == seq3[i+j-1]:\n", @@ -368,20 +389,23 @@ " backpointers[i, j] = (i-1, j, seq1[i-1], 'seq1') \n", " if debug: print('seq1', i, j, seq1[i-1], seq2[j-1], seq3[i+j-1], dp_table[i, j])\n", " if not dp_table[i, j]:\n", - " if debug: print('xx', i, j, seq1[i-1], seq2[j-1], seq3[i+j-1], dp_table[i, j])\n", + " if debug: print('xxxx', i, j, seq1[i-1], seq2[j-1], seq3[i+j-1], dp_table[i, j])\n", "\n", - " if return_backpointers:\n", - " return dp_table[len(seq1), len(seq2)], backpointers\n", + " if return_backpointers or return_table:\n", + " retval = [dp_table[len(seq1), len(seq2)]]\n", + " if return_backpointers:\n", + " retval += [backpointers]\n", + " if return_table:\n", + " retval += [dp_table]\n", + " return tuple(retval)\n", " else:\n", " return dp_table[len(seq1), len(seq2)]" ] }, { "cell_type": "code", - "execution_count": 50, - "metadata": { - "collapsed": false - }, + "execution_count": 67, + "metadata": {}, "outputs": [ { "data": { @@ -389,7 +413,7 @@ "True" ] }, - "execution_count": 50, + "execution_count": 67, "metadata": {}, "output_type": "execute_result" } @@ -400,10 +424,8 @@ }, { "cell_type": "code", - "execution_count": 51, - "metadata": { - "collapsed": false - }, + "execution_count": 71, + "metadata": {}, "outputs": [ { "data": { @@ -426,7 +448,7 @@ " (5, 5): (4, 5, 'c', 'seq1')})" ] }, - "execution_count": 51, + "execution_count": 71, "metadata": {}, "output_type": "execute_result" } @@ -437,10 +459,81 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 72, "metadata": { - "collapsed": false + "scrolled": true }, + "outputs": [ + { + "data": { + "text/plain": [ + "(True,\n", + " {(1, 0): (0, 0, 'a', 'seq1'),\n", + " (2, 0): (1, 0, 'a', 'seq1'),\n", + " (2, 1): (2, 0, 'd', 'seq2'),\n", + " (2, 2): (2, 1, 'b', 'seq2'),\n", + " (2, 3): (2, 2, 'b', 'seq2'),\n", + " (2, 4): (2, 3, 'c', 'seq2'),\n", + " (3, 1): (2, 1, 'b', 'seq1'),\n", + " (3, 2): (2, 2, 'b', 'seq1'),\n", + " (3, 4): (2, 4, 'b', 'seq1'),\n", + " (4, 2): (3, 2, 'c', 'seq1'),\n", + " (4, 3): (4, 2, 'b', 'seq2'),\n", + " (4, 4): (3, 4, 'c', 'seq1'),\n", + " (4, 5): (4, 4, 'a', 'seq2'),\n", + " (5, 3): (4, 3, 'c', 'seq1'),\n", + " (5, 5): (4, 5, 'c', 'seq1')},\n", + " {(0, 0): True,\n", + " (0, 1): False,\n", + " (0, 2): False,\n", + " (0, 3): False,\n", + " (0, 4): False,\n", + " (0, 5): False,\n", + " (1, 0): True,\n", + " (1, 1): False,\n", + " (1, 2): False,\n", + " (1, 3): False,\n", + " (1, 4): False,\n", + " (1, 5): False,\n", + " (2, 0): True,\n", + " (2, 1): True,\n", + " (2, 2): True,\n", + " (2, 3): True,\n", + " (2, 4): True,\n", + " (2, 5): False,\n", + " (3, 0): False,\n", + " (3, 1): True,\n", + " (3, 2): True,\n", + " (3, 3): False,\n", + " (3, 4): True,\n", + " (3, 5): False,\n", + " (4, 0): False,\n", + " (4, 1): False,\n", + " (4, 2): True,\n", + " (4, 3): True,\n", + " (4, 4): True,\n", + " (4, 5): True,\n", + " (5, 0): False,\n", + " (5, 1): False,\n", + " (5, 2): False,\n", + " (5, 3): True,\n", + " (5, 4): False,\n", + " (5, 5): True})" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "is_interleave(s1, s2, s3t, return_backpointers=True, return_table=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, "outputs": [ { "data": { @@ -448,7 +541,7 @@ "False" ] }, - "execution_count": 52, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -459,11 +552,35 @@ }, { "cell_type": "code", - "execution_count": 54, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "xxxx 0 0 ! ! ! True\n", + "seq2 0 1 ! b a False\n", + "seq2 0 2 ! b a False\n", + "seq2 0 3 ! b a False\n", + "seq1 1 0 a ! a True\n", + "xxxx 1 1 a b a False\n", + "xxxx 1 2 a b a False\n", + "xxxx 1 3 a b b False\n", + "seq1 2 0 a ! a True\n", + "xxxx 2 1 a b a False\n", + "xxxx 2 2 a b b False\n", + "xxxx 2 3 a b a False\n", + "seq1 3 0 a ! a True\n", + "seq2 3 1 a b b True\n", + "xxxx 3 2 a b a False\n", + "xxxx 3 3 a b b False\n", + "seq1 4 0 a ! b False\n", + "seq1 4 1 a b a True\n", + "seq2 4 2 a b b True\n", + "seq2 4 3 a b b True\n" + ] + }, { "data": { "text/plain": [ @@ -477,13 +594,1311 @@ " (4, 3): (4, 2, 'b', 'seq2')})" ] }, - "execution_count": 54, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "is_interleave('aaaa', 'bbb', 'aaababb', return_backpointers=True, debug=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": {}, + "outputs": [], + "source": [ + "def show_backtrace(bps):\n", + " i = max([0] + [k[0] for k in bps])\n", + " j = max([0] + [k[1] for k in bps])\n", + " chars = ''\n", + " if (i, j) in bps:\n", + " while i != 0 or j != 0:\n", + " if bps[i, j][3] == 'seq1':\n", + " chars += bps[i, j][2].upper()\n", + " else:\n", + " chars += bps[i, j][2]\n", + " i, j = bps[i, j][0], bps[i, j][1] \n", + " return ''.join(list(reversed(chars)))\n", + " else:\n", + " return ''" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def make_string(length, alphabet=None):\n", + " if not alphabet:\n", + " alphabet = 'abcdefgh'\n", + " return ''.join(random.choice(alphabet) for _ in range(length)) " + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'abgdfggfabffcagcagdhaedeeecedcdbaeaahdfgegacbhbdba'" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "make_string(50)" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def interleave(s1, s2, wander_limit=10, debug=False):\n", + " i1 = i2 = wander = 0\n", + " interleaved = []\n", + " while i1 <= len(s1) and i2 <= len(s2):\n", + " if i1 == len(s1):\n", + " if debug: print(i1, i2, wander, 'remaining s2', s2[i2:])\n", + " interleaved += s2[i2:]\n", + " i2 = len(s2) + 1\n", + " elif i2 == len(s2):\n", + " if debug: print(i1, i2, wander, 'remaining s1', s1[i1:])\n", + " interleaved += s1[i1:]\n", + " i1 = len(s1) + 1\n", + " else:\n", + " if wander == wander_limit:\n", + " step = -1\n", + " elif wander == -wander_limit:\n", + " step = +1\n", + " else:\n", + " step = random.choice([+1, -1])\n", + " if step == +1:\n", + " if debug: print(i1, i2, wander, 'adding', s1[i1])\n", + " interleaved += s1[i1]\n", + " i1 += 1\n", + " wander += 1\n", + " else:\n", + " if debug: print(i1, i2, wander, 'adding', s2[i2])\n", + " interleaved += s2[i2]\n", + " i2 += 1\n", + " wander -= 1\n", + " return ''.join(interleaved)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 142, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 0 0 adding d\n", + "1 0 1 adding v\n", + "1 1 0 adding v\n", + "1 2 -1 adding f\n", + "2 2 0 adding v\n", + "2 3 -1 adding w\n", + "2 4 -2 adding g\n", + "3 4 -1 adding y\n", + "3 5 -2 adding a\n", + "4 5 -1 adding w\n", + "4 6 -2 adding w\n", + "4 7 -3 adding w\n", + "4 8 -4 adding g\n", + "5 8 -3 adding h\n", + "6 8 -2 adding u\n", + "6 9 -3 adding v\n", + "6 10 -4 adding w\n", + "6 11 -5 adding f\n", + "7 11 -4 adding z\n", + "7 12 -5 adding f\n", + "8 12 -4 adding d\n", + "9 12 -3 adding x\n", + "9 13 -4 adding y\n", + "9 14 -5 adding g\n", + "10 14 -4 adding h\n", + "11 14 -3 adding u\n", + "11 15 -4 adding g\n", + "12 15 -3 adding b\n", + "13 15 -2 adding u\n", + "13 16 -3 adding z\n", + "13 17 -4 adding a\n", + "14 17 -3 adding u\n", + "14 18 -4 adding x\n", + "14 19 -5 adding a\n", + "15 19 -4 adding d\n", + "16 19 -3 adding g\n", + "17 19 -2 adding c\n", + "18 19 -1 adding x\n", + "18 20 -2 remaining s1 cg\n" + ] + }, + { + "data": { + "text/plain": [ + "('dfgaghffdghgbaadgccg',\n", + " 'vvvwywwwuvwzxyuuzuxx',\n", + " 'dvvfvwgyawwwghuvwfzfdxyghugbuzauxadgcxcg',\n", + " 40)" + ] + }, + "execution_count": 142, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s1 = make_string(20)\n", + "s2 = make_string(20, alphabet='uvwxyz')\n", + "il = interleave(s1, s2, wander_limit=5, debug=True)\n", + "s1, s2, il, len(il)" + ] + }, + { + "cell_type": "code", + "execution_count": 143, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "xxxx 0 0 ! ! ! True\n", + "seq2 0 1 ! d d True\n", + "seq2 0 2 ! f v False\n", + "seq2 0 3 ! g v False\n", + "seq2 0 4 ! a f False\n", + "seq2 0 5 ! g v False\n", + "seq2 0 6 ! h w False\n", + "seq2 0 7 ! f g False\n", + "seq2 0 8 ! f y False\n", + "seq2 0 9 ! d a False\n", + "seq2 0 10 ! g w False\n", + "seq2 0 11 ! h w False\n", + "seq2 0 12 ! g w False\n", + "seq2 0 13 ! b g False\n", + "seq2 0 14 ! a h False\n", + "seq2 0 15 ! a u False\n", + "seq2 0 16 ! d v False\n", + "seq2 0 17 ! g w False\n", + "seq2 0 18 ! c f False\n", + "seq2 0 19 ! c z False\n", + "seq2 0 20 ! g f False\n", + "seq1 1 0 v ! d False\n", + "seq1 1 1 v d v True\n", + "xxxx 1 2 v f v False\n", + "xxxx 1 3 v g f False\n", + "xxxx 1 4 v a v False\n", + "xxxx 1 5 v g w False\n", + "xxxx 1 6 v h g False\n", + "xxxx 1 7 v f y False\n", + "xxxx 1 8 v f a False\n", + "xxxx 1 9 v d w False\n", + "xxxx 1 10 v g w False\n", + "xxxx 1 11 v h w False\n", + "xxxx 1 12 v g g False\n", + "xxxx 1 13 v b h False\n", + "xxxx 1 14 v a u False\n", + "xxxx 1 15 v a v False\n", + "xxxx 1 16 v d w False\n", + "xxxx 1 17 v g f False\n", + "xxxx 1 18 v c z False\n", + "xxxx 1 19 v c f False\n", + "xxxx 1 20 v g d False\n", + "seq1 2 0 v ! v False\n", + "seq1 2 1 v d v True\n", + "seq2 2 2 v f f True\n", + "xxxx 2 3 v g v False\n", + "xxxx 2 4 v a w False\n", + "xxxx 2 5 v g g False\n", + "xxxx 2 6 v h y False\n", + "xxxx 2 7 v f a False\n", + "xxxx 2 8 v f w False\n", + "xxxx 2 9 v d w False\n", + "xxxx 2 10 v g w False\n", + "xxxx 2 11 v h g False\n", + "xxxx 2 12 v g h False\n", + "xxxx 2 13 v b u False\n", + "xxxx 2 14 v a v False\n", + "xxxx 2 15 v a w False\n", + "xxxx 2 16 v d f False\n", + "xxxx 2 17 v g z False\n", + "xxxx 2 18 v c f False\n", + "xxxx 2 19 v c d False\n", + "xxxx 2 20 v g x False\n", + "seq1 3 0 v ! v False\n", + "xxxx 3 1 v d f False\n", + "seq1 3 2 v f v True\n", + "xxxx 3 3 v g w False\n", + "xxxx 3 4 v a g False\n", + "xxxx 3 5 v g y False\n", + "xxxx 3 6 v h a False\n", + "xxxx 3 7 v f w False\n", + "xxxx 3 8 v f w False\n", + "xxxx 3 9 v d w False\n", + "xxxx 3 10 v g g False\n", + "xxxx 3 11 v h h False\n", + "xxxx 3 12 v g u False\n", + "xxxx 3 13 v b v False\n", + "xxxx 3 14 v a w False\n", + "xxxx 3 15 v a f False\n", + "xxxx 3 16 v d z False\n", + "xxxx 3 17 v g f False\n", + "xxxx 3 18 v c d False\n", + "xxxx 3 19 v c x False\n", + "xxxx 3 20 v g y False\n", + "seq1 4 0 w ! f False\n", + "xxxx 4 1 w d v False\n", + "seq1 4 2 w f w True\n", + "seq2 4 3 w g g True\n", + "xxxx 4 4 w a y False\n", + "xxxx 4 5 w g a False\n", + "xxxx 4 6 w h w False\n", + "xxxx 4 7 w f w False\n", + "xxxx 4 8 w f w False\n", + "xxxx 4 9 w d g False\n", + "xxxx 4 10 w g h False\n", + "xxxx 4 11 w h u False\n", + "xxxx 4 12 w g v False\n", + "xxxx 4 13 w b w False\n", + "xxxx 4 14 w a f False\n", + "xxxx 4 15 w a z False\n", + "xxxx 4 16 w d f False\n", + "xxxx 4 17 w g d False\n", + "xxxx 4 18 w c x False\n", + "xxxx 4 19 w c y False\n", + "xxxx 4 20 w g g False\n", + "seq1 5 0 y ! v False\n", + "xxxx 5 1 y d w False\n", + "xxxx 5 2 y f g False\n", + "seq1 5 3 y g y True\n", + "seq2 5 4 y a a True\n", + "xxxx 5 5 y g w False\n", + "xxxx 5 6 y h w False\n", + "xxxx 5 7 y f w False\n", + "xxxx 5 8 y f g False\n", + "xxxx 5 9 y d h False\n", + "xxxx 5 10 y g u False\n", + "xxxx 5 11 y h v False\n", + "xxxx 5 12 y g w False\n", + "xxxx 5 13 y b f False\n", + "xxxx 5 14 y a z False\n", + "xxxx 5 15 y a f False\n", + "xxxx 5 16 y d d False\n", + "xxxx 5 17 y g x False\n", + "xxxx 5 18 y c y False\n", + "xxxx 5 19 y c g False\n", + "xxxx 5 20 y g h False\n", + "seq1 6 0 w ! w False\n", + "xxxx 6 1 w d g False\n", + "xxxx 6 2 w f y False\n", + "xxxx 6 3 w g a False\n", + "seq1 6 4 w a w True\n", + "xxxx 6 5 w g w False\n", + "xxxx 6 6 w h w False\n", + "xxxx 6 7 w f g False\n", + "xxxx 6 8 w f h False\n", + "xxxx 6 9 w d u False\n", + "xxxx 6 10 w g v False\n", + "xxxx 6 11 w h w False\n", + "xxxx 6 12 w g f False\n", + "xxxx 6 13 w b z False\n", + "xxxx 6 14 w a f False\n", + "xxxx 6 15 w a d False\n", + "xxxx 6 16 w d x False\n", + "xxxx 6 17 w g y False\n", + "xxxx 6 18 w c g False\n", + "xxxx 6 19 w c h False\n", + "xxxx 6 20 w g u False\n", + "seq1 7 0 w ! g False\n", + "xxxx 7 1 w d y False\n", + "xxxx 7 2 w f a False\n", + "xxxx 7 3 w g w False\n", + "seq1 7 4 w a w True\n", + "xxxx 7 5 w g w False\n", + "xxxx 7 6 w h g False\n", + "xxxx 7 7 w f h False\n", + "xxxx 7 8 w f u False\n", + "xxxx 7 9 w d v False\n", + "xxxx 7 10 w g w False\n", + "xxxx 7 11 w h f False\n", + "xxxx 7 12 w g z False\n", + "xxxx 7 13 w b f False\n", + "xxxx 7 14 w a d False\n", + "xxxx 7 15 w a x False\n", + "xxxx 7 16 w d y False\n", + "xxxx 7 17 w g g False\n", + "xxxx 7 18 w c h False\n", + "xxxx 7 19 w c u False\n", + "xxxx 7 20 w g g False\n", + "seq1 8 0 w ! y False\n", + "xxxx 8 1 w d a False\n", + "xxxx 8 2 w f w False\n", + "xxxx 8 3 w g w False\n", + "seq1 8 4 w a w True\n", + "seq2 8 5 w g g True\n", + "seq2 8 6 w h h True\n", + "xxxx 8 7 w f u False\n", + "xxxx 8 8 w f v False\n", + "xxxx 8 9 w d w False\n", + "xxxx 8 10 w g f False\n", + "xxxx 8 11 w h z False\n", + "xxxx 8 12 w g f False\n", + "xxxx 8 13 w b d False\n", + "xxxx 8 14 w a x False\n", + "xxxx 8 15 w a y False\n", + "xxxx 8 16 w d g False\n", + "xxxx 8 17 w g h False\n", + "xxxx 8 18 w c u False\n", + "xxxx 8 19 w c g False\n", + "xxxx 8 20 w g b False\n", + "seq1 9 0 u ! a False\n", + "xxxx 9 1 u d w False\n", + "xxxx 9 2 u f w False\n", + "xxxx 9 3 u g w False\n", + "xxxx 9 4 u a g False\n", + "xxxx 9 5 u g h False\n", + "seq1 9 6 u h u True\n", + "xxxx 9 7 u f v False\n", + "xxxx 9 8 u f w False\n", + "xxxx 9 9 u d f False\n", + "xxxx 9 10 u g z False\n", + "xxxx 9 11 u h f False\n", + "xxxx 9 12 u g d False\n", + "xxxx 9 13 u b x False\n", + "xxxx 9 14 u a y False\n", + "xxxx 9 15 u a g False\n", + "xxxx 9 16 u d h False\n", + "xxxx 9 17 u g u False\n", + "xxxx 9 18 u c g False\n", + "xxxx 9 19 u c b False\n", + "xxxx 9 20 u g u False\n", + "seq1 10 0 v ! w False\n", + "xxxx 10 1 v d w False\n", + "xxxx 10 2 v f w False\n", + "xxxx 10 3 v g g False\n", + "xxxx 10 4 v a h False\n", + "xxxx 10 5 v g u False\n", + "seq1 10 6 v h v True\n", + "xxxx 10 7 v f w False\n", + "xxxx 10 8 v f f False\n", + "xxxx 10 9 v d z False\n", + "xxxx 10 10 v g f False\n", + "xxxx 10 11 v h d False\n", + "xxxx 10 12 v g x False\n", + "xxxx 10 13 v b y False\n", + "xxxx 10 14 v a g False\n", + "xxxx 10 15 v a h False\n", + "xxxx 10 16 v d u False\n", + "xxxx 10 17 v g g False\n", + "xxxx 10 18 v c b False\n", + "xxxx 10 19 v c u False\n", + "xxxx 10 20 v g z False\n", + "seq1 11 0 w ! w False\n", + "xxxx 11 1 w d w False\n", + "xxxx 11 2 w f g False\n", + "xxxx 11 3 w g h False\n", + "xxxx 11 4 w a u False\n", + "xxxx 11 5 w g v False\n", + "seq1 11 6 w h w True\n", + "seq2 11 7 w f f True\n", + "xxxx 11 8 w f z False\n", + "xxxx 11 9 w d f False\n", + "xxxx 11 10 w g d False\n", + "xxxx 11 11 w h x False\n", + "xxxx 11 12 w g y False\n", + "xxxx 11 13 w b g False\n", + "xxxx 11 14 w a h False\n", + "xxxx 11 15 w a u False\n", + "xxxx 11 16 w d g False\n", + "xxxx 11 17 w g b False\n", + "xxxx 11 18 w c u False\n", + "xxxx 11 19 w c z False\n", + "xxxx 11 20 w g a False\n", + "seq1 12 0 z ! w False\n", + "xxxx 12 1 z d g False\n", + "xxxx 12 2 z f h False\n", + "xxxx 12 3 z g u False\n", + "xxxx 12 4 z a v False\n", + "xxxx 12 5 z g w False\n", + "xxxx 12 6 z h f False\n", + "seq1 12 7 z f z True\n", + "seq2 12 8 z f f True\n", + "seq2 12 9 z d d True\n", + "xxxx 12 10 z g x False\n", + "xxxx 12 11 z h y False\n", + "xxxx 12 12 z g g False\n", + "xxxx 12 13 z b h False\n", + "xxxx 12 14 z a u False\n", + "xxxx 12 15 z a g False\n", + "xxxx 12 16 z d b False\n", + "xxxx 12 17 z g u False\n", + "xxxx 12 18 z c z False\n", + "xxxx 12 19 z c a False\n", + "xxxx 12 20 z g u False\n", + "seq1 13 0 x ! g False\n", + "xxxx 13 1 x d h False\n", + "xxxx 13 2 x f u False\n", + "xxxx 13 3 x g v False\n", + "xxxx 13 4 x a w False\n", + "xxxx 13 5 x g f False\n", + "xxxx 13 6 x h z False\n", + "xxxx 13 7 x f f False\n", + "xxxx 13 8 x f d False\n", + "seq1 13 9 x d x True\n", + "xxxx 13 10 x g y False\n", + "xxxx 13 11 x h g False\n", + "xxxx 13 12 x g h False\n", + "xxxx 13 13 x b u False\n", + "xxxx 13 14 x a g False\n", + "xxxx 13 15 x a b False\n", + "xxxx 13 16 x d u False\n", + "xxxx 13 17 x g z False\n", + "xxxx 13 18 x c a False\n", + "xxxx 13 19 x c u False\n", + "xxxx 13 20 x g x False\n", + "seq1 14 0 y ! h False\n", + "xxxx 14 1 y d u False\n", + "xxxx 14 2 y f v False\n", + "xxxx 14 3 y g w False\n", + "xxxx 14 4 y a f False\n", + "xxxx 14 5 y g z False\n", + "xxxx 14 6 y h f False\n", + "xxxx 14 7 y f d False\n", + "xxxx 14 8 y f x False\n", + "seq1 14 9 y d y True\n", + "seq2 14 10 y g g True\n", + "seq2 14 11 y h h True\n", + "xxxx 14 12 y g u False\n", + "xxxx 14 13 y b g False\n", + "xxxx 14 14 y a b False\n", + "xxxx 14 15 y a u False\n", + "xxxx 14 16 y d z False\n", + "xxxx 14 17 y g a False\n", + "xxxx 14 18 y c u False\n", + "xxxx 14 19 y c x False\n", + "xxxx 14 20 y g a False\n", + "seq1 15 0 u ! u False\n", + "xxxx 15 1 u d v False\n", + "xxxx 15 2 u f w False\n", + "xxxx 15 3 u g f False\n", + "xxxx 15 4 u a z False\n", + "xxxx 15 5 u g f False\n", + "xxxx 15 6 u h d False\n", + "xxxx 15 7 u f x False\n", + "xxxx 15 8 u f y False\n", + "xxxx 15 9 u d g False\n", + "xxxx 15 10 u g h False\n", + "seq1 15 11 u h u True\n", + "seq2 15 12 u g g True\n", + "seq2 15 13 u b b True\n", + "xxxx 15 14 u a u False\n", + "xxxx 15 15 u a z False\n", + "xxxx 15 16 u d a False\n", + "xxxx 15 17 u g u False\n", + "xxxx 15 18 u c x False\n", + "xxxx 15 19 u c a False\n", + "xxxx 15 20 u g d False\n", + "seq1 16 0 u ! v False\n", + "xxxx 16 1 u d w False\n", + "xxxx 16 2 u f f False\n", + "xxxx 16 3 u g z False\n", + "xxxx 16 4 u a f False\n", + "xxxx 16 5 u g d False\n", + "xxxx 16 6 u h x False\n", + "xxxx 16 7 u f y False\n", + "xxxx 16 8 u f g False\n", + "xxxx 16 9 u d h False\n", + "xxxx 16 10 u g u False\n", + "xxxx 16 11 u h g False\n", + "xxxx 16 12 u g b False\n", + "seq1 16 13 u b u True\n", + "xxxx 16 14 u a z False\n", + "xxxx 16 15 u a a False\n", + "xxxx 16 16 u d u False\n", + "xxxx 16 17 u g x False\n", + "xxxx 16 18 u c a False\n", + "xxxx 16 19 u c d False\n", + "xxxx 16 20 u g g False\n", + "seq1 17 0 z ! w False\n", + "xxxx 17 1 z d f False\n", + "xxxx 17 2 z f z False\n", + "xxxx 17 3 z g f False\n", + "xxxx 17 4 z a d False\n", + "xxxx 17 5 z g x False\n", + "xxxx 17 6 z h y False\n", + "xxxx 17 7 z f g False\n", + "xxxx 17 8 z f h False\n", + "xxxx 17 9 z d u False\n", + "xxxx 17 10 z g g False\n", + "xxxx 17 11 z h b False\n", + "xxxx 17 12 z g u False\n", + "seq1 17 13 z b z True\n", + "seq2 17 14 z a a True\n", + "xxxx 17 15 z a u False\n", + "xxxx 17 16 z d x False\n", + "xxxx 17 17 z g a False\n", + "xxxx 17 18 z c d False\n", + "xxxx 17 19 z c g False\n", + "xxxx 17 20 z g c False\n", + "seq1 18 0 u ! f False\n", + "xxxx 18 1 u d z False\n", + "xxxx 18 2 u f f False\n", + "xxxx 18 3 u g d False\n", + "xxxx 18 4 u a x False\n", + "xxxx 18 5 u g y False\n", + "xxxx 18 6 u h g False\n", + "xxxx 18 7 u f h False\n", + "xxxx 18 8 u f u False\n", + "xxxx 18 9 u d g False\n", + "xxxx 18 10 u g b False\n", + "xxxx 18 11 u h u False\n", + "xxxx 18 12 u g z False\n", + "xxxx 18 13 u b a False\n", + "seq1 18 14 u a u True\n", + "xxxx 18 15 u a x False\n", + "xxxx 18 16 u d a False\n", + "xxxx 18 17 u g d False\n", + "xxxx 18 18 u c g False\n", + "xxxx 18 19 u c c False\n", + "xxxx 18 20 u g x False\n", + "seq1 19 0 x ! z False\n", + "xxxx 19 1 x d f False\n", + "xxxx 19 2 x f d False\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "xxxx 19 3 x g x False\n", + "xxxx 19 4 x a y False\n", + "xxxx 19 5 x g g False\n", + "xxxx 19 6 x h h False\n", + "xxxx 19 7 x f u False\n", + "xxxx 19 8 x f g False\n", + "xxxx 19 9 x d b False\n", + "xxxx 19 10 x g u False\n", + "xxxx 19 11 x h z False\n", + "xxxx 19 12 x g a False\n", + "xxxx 19 13 x b u False\n", + "seq1 19 14 x a x True\n", + "seq2 19 15 x a a True\n", + "seq2 19 16 x d d True\n", + "seq2 19 17 x g g True\n", + "seq2 19 18 x c c True\n", + "xxxx 19 19 x c x False\n", + "xxxx 19 20 x g c False\n", + "seq1 20 0 x ! f False\n", + "xxxx 20 1 x d d False\n", + "xxxx 20 2 x f x False\n", + "xxxx 20 3 x g y False\n", + "xxxx 20 4 x a g False\n", + "xxxx 20 5 x g h False\n", + "xxxx 20 6 x h u False\n", + "xxxx 20 7 x f g False\n", + "xxxx 20 8 x f b False\n", + "xxxx 20 9 x d u False\n", + "xxxx 20 10 x g z False\n", + "xxxx 20 11 x h a False\n", + "xxxx 20 12 x g u False\n", + "xxxx 20 13 x b x False\n", + "xxxx 20 14 x a a False\n", + "xxxx 20 15 x a d False\n", + "xxxx 20 16 x d g False\n", + "xxxx 20 17 x g c False\n", + "seq1 20 18 x c x True\n", + "seq2 20 19 x c c True\n", + "seq2 20 20 x g g True\n", + "T T . . . . . . . . . . . . . . . . . . .\n", + ". T . . . . . . . . . . . . . . . . . . .\n", + ". T T . . . . . . . . . . . . . . . . . .\n", + ". . T . . . . . . . . . . . . . . . . . .\n", + ". . T T . . . . . . . . . . . . . . . . .\n", + ". . . T T . . . . . . . . . . . . . . . .\n", + ". . . . T . . . . . . . . . . . . . . . .\n", + ". . . . T . . . . . . . . . . . . . . . .\n", + ". . . . T T T . . . . . . . . . . . . . .\n", + ". . . . . . T . . . . . . . . . . . . . .\n", + ". . . . . . T . . . . . . . . . . . . . .\n", + ". . . . . . T T . . . . . . . . . . . . .\n", + ". . . . . . . T T T . . . . . . . . . . .\n", + ". . . . . . . . . T . . . . . . . . . . .\n", + ". . . . . . . . . T T T . . . . . . . . .\n", + ". . . . . . . . . . . T T T . . . . . . .\n", + ". . . . . . . . . . . . . T . . . . . . .\n", + ". . . . . . . . . . . . . T T . . . . . .\n", + ". . . . . . . . . . . . . . T . . . . . .\n", + ". . . . . . . . . . . . . . T T T T T . .\n", + ". . . . . . . . . . . . . . . . . . T T T\n" + ] + } + ], + "source": [ + "v, bp, t = is_interleave(s2, s1, il, return_backpointers=True, return_table=True, debug=True)\n", + "print(show_table(t))" + ] + }, + { + "cell_type": "code", + "execution_count": 144, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'dVVfVWgYaWWWghUVWfZfdXYghUgbUZaUXadgcXcg'" + ] + }, + "execution_count": 144, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "show_backtrace(bp)" + ] + }, + { + "cell_type": "code", + "execution_count": 145, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T T . . . . . . . . . . . . . . . . . . .\n", + ". T . . . . . . . . . . . . . . . . . . .\n", + ". T T . . . . . . . . . . . . . . . . . .\n", + ". . T . . . . . . . . . . . . . . . . . .\n", + ". . T T . . . . . . . . . . . . . . . . .\n", + ". . . T T . . . . . . . . . . . . . . . .\n", + ". . . . T . . . . . . . . . . . . . . . .\n", + ". . . . T . . . . . . . . . . . . . . . .\n", + ". . . . T T T . . . . . . . . . . . . . .\n", + ". . . . . . T . . . . . . . . . . . . . .\n", + ". . . . . . T . . . . . . . . . . . . . .\n", + ". . . . . . T T . . . . . . . . . . . . .\n", + ". . . . . . . T T T . . . . . . . . . . .\n", + ". . . . . . . . . T . . . . . . . . . . .\n", + ". . . . . . . . . T T T . . . . . . . . .\n", + ". . . . . . . . . . . T T T . . . . . . .\n", + ". . . . . . . . . . . . . T . . . . . . .\n", + ". . . . . . . . . . . . . T T . . . . . .\n", + ". . . . . . . . . . . . . . T . . . . . .\n", + ". . . . . . . . . . . . . . T T T T T . .\n", + ". . . . . . . . . . . . . . . . . . T T T\n", + "dVVfVWgYaWWWghUVWfZfdXYghUgbUZaUXadgcXcg\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 145, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v, bp, t = is_interleave(s2, s1, il, return_backpointers=True, return_table=True)\n", + "print(show_table(t))\n", + "print(show_backtrace(bp))\n", + "v" + ] + }, + { + "cell_type": "code", + "execution_count": 151, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('cfhgbbfabcehcbfdfcafdfecefaecfagbdfeecfdbhebgbfhee',\n", + " 'begacfabcgegaegefgcgaabeefahahgfhbdbhhdabbaaahhdcb',\n", + " 'bcfehggacfbbabcgegfaeabgceefhcbgcgaabfdfcaefedfefacehahgffhbdbahechfagdabbbdaafeahechdfdcbbhebgbfhee',\n", + " 100)" + ] + }, + "execution_count": 151, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s1 = make_string(50)\n", + "s2 = make_string(50)\n", + "il = interleave(s1, s2)\n", + "s1, s2, il, len(il)" + ] + }, + { + "cell_type": "code", + "execution_count": 152, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + "T T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . T T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . T T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . T T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . T T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . T T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . T T T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . T T T T T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . T T T T . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . T . T . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . T T T . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . T T . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . T T . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . T T . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . T T T . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T T T . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T T . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T T . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T T . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T T . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T T T T T T T T T T\n", + "BcfEhgGACFbbABCGEGfAEabGceEFhcbGCGAABfdfcaEfEdfeFAceHAHGfFHBDBaHecHfagDAbBBdAAfeAHecHDfdCbBhebgbfhee\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 152, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v, bp, t = is_interleave(s2, s1, il, return_backpointers=True, return_table=True)\n", + "print(show_table(t))\n", + "print(show_backtrace(bp))\n", + "v" + ] + }, + { + "cell_type": "code", + "execution_count": 157, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1000 loops, best of 3: 1.24 ms per loop\n" + ] + } + ], + "source": [ + "%%timeit\n", + "is_interleave(s2, s1, il)" + ] + }, + { + "cell_type": "code", + "execution_count": 148, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + "T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + "T T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + "T . T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . T T T T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . T . . T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . T . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . T T T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . T T T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . T T T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . T T T T T T T T T T T T T T . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . T . . . . . . . . . T . . T T T . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T T T . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T T T . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T T . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T T . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T . T T . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T T T . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T T T . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T T . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T T T\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T\n", + "FCgcGdDEgebbEhBCCgEcehHDAFBebgGECehcgdfdadbeggEgFCfggGAGaAhEEHfAgGAGHeFdaDfEcbfGcBDEbHdEaaFdfBBCABHC\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 148, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v, bp, t = is_interleave(s1, s2, il, return_backpointers=True, return_table=True)\n", + "print(show_table(t))\n", + "print(show_backtrace(bp))\n", + "v" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 115, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "show_backtrace(bp).lower() == il" + ] + }, + { + "cell_type": "code", + "execution_count": 149, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('hchdchbfgbfhfcadcdehgebefhebedbdadfbbcdadfgbefebeb', False, False)" + ] + }, + "execution_count": 149, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s3 = make_string(50)\n", + "s3, is_interleave(s1, s3, il), is_interleave(s2, s3, il)" + ] + }, + { + "cell_type": "code", + "execution_count": 150, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + "T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + "T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + "T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n", + "FCG\n" + ] + }, + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 150, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v, bp, t = is_interleave(s1, s3, il, return_backpointers=True, return_table=True)\n", + "print(show_table(t))\n", + "print(show_backtrace(bp))\n", + "v" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example" + ] + }, + { + "cell_type": "code", + "execution_count": 174, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def show_annotated_table(table, bps):\n", + " return '\\n'.join(' '.join('*' if (i, j) == (0, 0) else bps[i, j][2] if table[i, j] else '.' for j in sorted(set([k[1] for k in table])))\n", + " for i in sorted(set([k[0] for k in table])))" + ] + }, + { + "cell_type": "code", + "execution_count": 195, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def show_backtrace_star(bps):\n", + " i = max([0] + [k[0] for k in bps])\n", + " j = max([0] + [k[1] for k in bps])\n", + " chars = ''\n", + " stars = ''\n", + " if (i, j) in bps:\n", + " while i != 0 or j != 0:\n", + " chars += bps[i, j][2]\n", + " if bps[i, j][3] == 'seq1':\n", + " stars += '*'\n", + " else:\n", + " stars += ' '\n", + " i, j = bps[i, j][0], bps[i, j][1] \n", + " return ''.join(list(reversed(chars))) + '\\n' + ''.join(list(reversed(stars)))\n", + " else:\n", + " return ''" + ] + }, + { + "cell_type": "code", + "execution_count": 203, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('ddcbcaabac', 'abcaddcbcc', 'abddcbccaaadbadccbcc', 20)" + ] + }, + "execution_count": 203, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s1 = make_string(10, alphabet='abcd')\n", + "s2 = make_string(10, alphabet='abcd')\n", + "s3 = make_string(10, alphabet='abcd')\n", + "il = interleave(s1, s2)\n", + "s1, s2, il, len(il)" + ] + }, + { + "cell_type": "code", + "execution_count": 204, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* a b . . . . . . . .\n", + ". . d . . . . . . . .\n", + ". . d c . . . . . . .\n", + ". . c . . . . . . . .\n", + ". . b c . . . . . . .\n", + ". . c c a . . . . . .\n", + ". . . a a . . . . . .\n", + ". . . a a d . . . . .\n", + ". . . . . b . . . . .\n", + ". . . . . a d c . . .\n", + ". . . . . . c c b c c\n", + "abDDCBcCaAAdBAdcCbcc\n", + "abddcbccaaadbadccbcc\n", + " **** * ** ** * \n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 204, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v, bp, t = is_interleave(s1, s2, il, return_backpointers=True, return_table=True)\n", + "print(show_annotated_table(t, bp))\n", + "print(show_backtrace(bp))\n", + "print(show_backtrace_star(bp))\n", + "v" + ] + }, + { + "cell_type": "code", + "execution_count": 198, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* b . . . . . . . . .\n", + "b b c a . . . . . . .\n", + ". c . c . . . . . . .\n", + ". a c . . . . . . . .\n", + ". . b . . . . . . . .\n", + ". . d a a . . . . . .\n", + ". . a a . . . . . . .\n", + ". . . d a . . . . . .\n", + ". . . . b a d a . . .\n", + ". . . . . . . c . . .\n", + ". . . . . . . c b b b\n", + "bBCAcBDaADaBadaCCbbb\n", + "bbcacbdaadabadaccbbb\n", + " *** ** ** * ** \n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 198, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v, bp, t = is_interleave(s2, s1, il, return_backpointers=True, return_table=True)\n", + "print(show_annotated_table(t, bp))\n", + "print(show_backtrace(bp))\n", + "print(show_backtrace_star(bp))\n", + "v" + ] + }, + { + "cell_type": "code", + "execution_count": 199, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* b . . . . . . . . .\n", + "b b c . . . . . . . .\n", + ". c . . . . . . . . .\n", + ". a c . . . . . . . .\n", + ". . . . . . . . . . .\n", + ". . . . . . . . . . .\n", + ". . . . . . . . . . .\n", + ". . . . . . . . . . .\n", + ". . . . . . . . . . .\n", + ". . . . . . . . . . .\n", + ". . . . . . . . . . .\n", + "bBCAc\n", + "bbcac\n", + " *** \n" + ] + }, + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 199, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v, bp, t = is_interleave(s1, s3, il, return_backpointers=True, return_table=True)\n", + "print(show_annotated_table(t, bp))\n", + "print(show_backtrace(bp))\n", + "print(show_backtrace_star(bp))\n", + "v" + ] + }, + { + "cell_type": "code", + "execution_count": 200, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* b . . . . . . . . .\n", + "b b c . . . . . . . .\n", + ". c . . . . . . . . .\n", + ". a c . . . . . . . .\n", + ". . b . . . . . . . .\n", + ". . d . . . . . . . .\n", + ". . a . . . . . . . .\n", + ". . . . . . . . . . .\n", + ". . . . . . . . . . .\n", + ". . . . . . . . . . .\n", + ". . . . . . . . . . .\n", + "bBCAcBDA\n", + "bbcacbda\n", + " *** ***\n" + ] + }, + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 200, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "is_interleave('aaaa', 'bbb', 'aaababb', return_backpointers=True)" + "v, bp, t = is_interleave(s2, s3, il, return_backpointers=True, return_table=True)\n", + "print(show_annotated_table(t, bp))\n", + "print(show_backtrace(bp))\n", + "print(show_backtrace_star(bp))\n", + "v" ] }, { @@ -512,7 +1927,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.2" + "version": "3.5.2+" } }, "nbformat": 4, diff --git a/09-resolving-the-bill/subsequence.ipynb b/09-resolving-the-bill/subsequence.ipynb new file mode 100644 index 0000000..c945df8 --- /dev/null +++ b/09-resolving-the-bill/subsequence.ipynb @@ -0,0 +1,573 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Subsequence strings\n", + "\n", + "Given two strings `a` and `b`, is `a` a subsequence of `b`?\n", + "\n", + "For example,\n", + "Given:\n", + "a = \"aabcc\",\n", + "b = \"dabaabcacb\",\n", + "\n", + "return True : dAbAaBCaCb\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "s1 = \"aabcc\"\n", + "s2t = \"dabaabcacb\"\n", + "s2f = \"dabacc\"\n", + "\n", + "s2 = s2t" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`dp_table[i, j]` is True if first `i` characters of `s1` can be found in first `j` characters of `s2`." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{(0, 0): False,\n", + " (0, 1): False,\n", + " (0, 2): False,\n", + " (0, 3): False,\n", + " (0, 4): False,\n", + " (0, 5): False,\n", + " (0, 6): False,\n", + " (0, 7): False,\n", + " (0, 8): False,\n", + " (0, 9): False,\n", + " (0, 10): False,\n", + " (1, 0): False,\n", + " (1, 1): False,\n", + " (1, 2): False,\n", + " (1, 3): False,\n", + " (1, 4): False,\n", + " (1, 5): False,\n", + " (1, 6): False,\n", + " (1, 7): False,\n", + " (1, 8): False,\n", + " (1, 9): False,\n", + " (1, 10): False,\n", + " (2, 0): False,\n", + " (2, 1): False,\n", + " (2, 2): False,\n", + " (2, 3): False,\n", + " (2, 4): False,\n", + " (2, 5): False,\n", + " (2, 6): False,\n", + " (2, 7): False,\n", + " (2, 8): False,\n", + " (2, 9): False,\n", + " (2, 10): False,\n", + " (3, 0): False,\n", + " (3, 1): False,\n", + " (3, 2): False,\n", + " (3, 3): False,\n", + " (3, 4): False,\n", + " (3, 5): False,\n", + " (3, 6): False,\n", + " (3, 7): False,\n", + " (3, 8): False,\n", + " (3, 9): False,\n", + " (3, 10): False,\n", + " (4, 0): False,\n", + " (4, 1): False,\n", + " (4, 2): False,\n", + " (4, 3): False,\n", + " (4, 4): False,\n", + " (4, 5): False,\n", + " (4, 6): False,\n", + " (4, 7): False,\n", + " (4, 8): False,\n", + " (4, 9): False,\n", + " (4, 10): False,\n", + " (5, 0): False,\n", + " (5, 1): False,\n", + " (5, 2): False,\n", + " (5, 3): False,\n", + " (5, 4): False,\n", + " (5, 5): False,\n", + " (5, 6): False,\n", + " (5, 7): False,\n", + " (5, 8): False,\n", + " (5, 9): False,\n", + " (5, 10): False}" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dp_table = {(i, j): False\n", + " for i in range(len(s1)+1)\n", + " for j in range(len(s2)+1)}\n", + "dp_table" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def show_table(table):\n", + " return '\\n'.join(\n", + " ' '.join(str(table[i, j])[0] for j in sorted(set([k[1] for k in table])))\n", + " for i in sorted(set([k[0] for k in table]))) " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "F F F F F F F F F F F\n", + "F F F F F F F F F F F\n", + "F F F F F F F F F F F\n", + "F F F F F F F F F F F\n", + "F F F F F F F F F F F\n", + "F F F F F F F F F F F\n" + ] + } + ], + "source": [ + "print(show_table(dp_table))" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "aaaa aaababb\n", + "aa 0 0 ! ! True\n", + "aa 0 1 ! ! True\n", + "aa 0 2 ! ! True\n", + "aa 0 3 ! ! True\n", + "aa 0 4 ! ! True\n", + "aa 0 5 ! ! True\n", + "aa 0 6 ! ! True\n", + "aa 0 7 ! ! True\n", + "s1 1 1 a a True\n", + "s2 1 2 a a True\n", + "s1 1 2 a a True\n", + "s2 1 3 a a True\n", + "s1 1 3 a a True\n", + "s2 1 4 a b True\n", + "s2 1 5 a a True\n", + "s1 1 5 a a True\n", + "s2 1 6 a b True\n", + "s2 1 7 a b True\n", + "s1 2 2 a a True\n", + "s2 2 3 a a True\n", + "s1 2 3 a a True\n", + "s2 2 4 a b True\n", + "s2 2 5 a a True\n", + "s1 2 5 a a True\n", + "s2 2 6 a b True\n", + "s2 2 7 a b True\n", + "s1 3 3 a a True\n", + "s2 3 4 a b True\n", + "s2 3 5 a a True\n", + "s1 3 5 a a True\n", + "s2 3 6 a b True\n", + "s2 3 7 a b True\n", + "xx 4 4 a b False\n", + "s1 4 5 a a True\n", + "s2 4 6 a b True\n", + "s2 4 7 a b True\n", + "T T T T T T T T\n", + "F T T T T T T T\n", + "F F T T T T T T\n", + "F F F T T T T T\n", + "F F F F F T T T\n" + ] + }, + { + "data": { + "text/plain": [ + "{(1, 1): (0, 0, 'a', 's1'),\n", + " (1, 2): (0, 1, 'a', 's1'),\n", + " (1, 3): (0, 2, 'a', 's1'),\n", + " (1, 4): (1, 3, 'b', 's2'),\n", + " (1, 5): (0, 4, 'a', 's1'),\n", + " (1, 6): (1, 5, 'b', 's2'),\n", + " (1, 7): (1, 6, 'b', 's2'),\n", + " (2, 2): (1, 1, 'a', 's1'),\n", + " (2, 3): (1, 2, 'a', 's1'),\n", + " (2, 4): (2, 3, 'b', 's2'),\n", + " (2, 5): (1, 4, 'a', 's1'),\n", + " (2, 6): (2, 5, 'b', 's2'),\n", + " (2, 7): (2, 6, 'b', 's2'),\n", + " (3, 3): (2, 2, 'a', 's1'),\n", + " (3, 4): (3, 3, 'b', 's2'),\n", + " (3, 5): (2, 4, 'a', 's1'),\n", + " (3, 6): (3, 5, 'b', 's2'),\n", + " (3, 7): (3, 6, 'b', 's2'),\n", + " (4, 5): (3, 4, 'a', 's1'),\n", + " (4, 6): (4, 5, 'b', 's2'),\n", + " (4, 7): (4, 6, 'b', 's2')}" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s2 = s2f\n", + "\n", + "s1 = 'aaaa'\n", + "s2 = 'aaababb'\n", + "\n", + "print(s1, s2)\n", + "\n", + "dp_table = {(i, j): False\n", + " for i in range(len(s1)+1)\n", + " for j in range(len(s2)+1)}\n", + "\n", + "backpointers = {}\n", + "\n", + "for i in range(len(s1)+1):\n", + " for j in range(i, len(s2)+1):\n", + " if i == 0 or j == 0:\n", + " dp_table[i, j] = True\n", + " print('aa', i, j, '!', '!', dp_table[i, j])\n", + " else:\n", + " # extend by character from s2\n", + " if dp_table[i, j-1]:\n", + " dp_table[i, j] = True\n", + " backpointers[i, j] = (i, j-1, s2[j-1], 's2')\n", + " print('s2', i, j, s1[i-1], s2[j-1], dp_table[i, j]) \n", + " # extend by character from s1\n", + " if dp_table[i-1, j-1] and s1[i-1] == s2[j-1]:\n", + " dp_table[i, j] = True\n", + " backpointers[i, j] = (i-1, j-1, s1[i-1], 's1') \n", + " print('s1', i, j, s1[i-1], s2[j-1], dp_table[i, j])\n", + " if not dp_table[i, j]:\n", + " print('xx', i, j, s1[i-1], s2[j-1], dp_table[i, j])\n", + "\n", + "print(show_table(dp_table))\n", + "backpointers" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def show_backtrace(bps):\n", + " i = max([0] + [k[0] for k in bps])\n", + " j = max([0] + [k[1] for k in bps])\n", + " chars = ''\n", + " if (i, j) in bps:\n", + " while i != 0 or j != 0:\n", + " if bps[i, j][3] == 's1':\n", + " chars += bps[i, j][2].upper()\n", + " else:\n", + " chars += bps[i, j][2]\n", + " i, j = bps[i, j][0], bps[i, j][1] \n", + " return ''.join(list(reversed(chars)))\n", + " else:\n", + " return ''" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'AAAbAbb'" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "show_backtrace(backpointers)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [], + "source": [ + "def is_subseq(seq1, seq2, return_backpointers=False, debug=False):\n", + " \"\"\"Return true if seq1 is a subsequence of seq2.\n", + " If return_backpointers, also return the set of backpointers to\n", + " reconstruct the subsequence\"\"\"\n", + " \n", + " # dp_table[i, j] is True if first i characters of seq1 can\n", + " # be found in the first j characters of seq2\n", + " \n", + " dp_table = {(i, j): False\n", + " for i in range(len(seq1)+1)\n", + " for j in range(len(seq2)+1)}\n", + "\n", + " backpointers = {}\n", + " \n", + " for i in range(len(seq1)+1):\n", + " for j in range(i, len(seq2)+1):\n", + " if i == 0 or j == 0:\n", + " dp_table[i, j] = True\n", + " if debug: print('aa', i, j, '!', '!', dp_table[i, j])\n", + " else:\n", + " # extend by character from s2\n", + " if dp_table[i, j-1]:\n", + " dp_table[i, j] = True\n", + " backpointers[i, j] = (i, j-1, seq2[j-1], 's2')\n", + " if debug: print('s2', i, j, seq1[i-1], seq2[j-1], dp_table[i, j]) \n", + " # extend by character from s1\n", + " if dp_table[i-1, j-1] and seq1[i-1] == seq2[j-1]:\n", + " dp_table[i, j] = True\n", + " backpointers[i, j] = (i-1, j-1, seq1[i-1], 's1') \n", + " if debug: print('s1', i, j, seq1[i-1], seq2[j-1], dp_table[i, j])\n", + " if not dp_table[i, j]:\n", + " if debug: print('xx', i, j, seq1[i-1], seq2[j-1], dp_table[i, j]) \n", + " \n", + " if return_backpointers:\n", + " return dp_table[len(seq1), len(seq2)], backpointers\n", + " else:\n", + " return dp_table[len(seq1), len(seq2)]" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "is_subseq(s1, s2t)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tf, bps = is_subseq(s1, s2t, return_backpointers=True)\n", + "tf" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'AABCaCb'" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "show_backtrace(bps)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "is_subseq(s1, s2f)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "aa 0 0 ! ! True\n", + "aa 0 1 ! ! True\n", + "aa 0 2 ! ! True\n", + "aa 0 3 ! ! True\n", + "aa 0 4 ! ! True\n", + "aa 0 5 ! ! True\n", + "aa 0 6 ! ! True\n", + "aa 0 7 ! ! True\n", + "s1 1 1 a a True\n", + "s2 1 2 a a True\n", + "s1 1 2 a a True\n", + "s2 1 3 a a True\n", + "s1 1 3 a a True\n", + "s2 1 4 a b True\n", + "s2 1 5 a a True\n", + "s1 1 5 a a True\n", + "s2 1 6 a b True\n", + "s2 1 7 a b True\n", + "s1 2 2 a a True\n", + "s2 2 3 a a True\n", + "s1 2 3 a a True\n", + "s2 2 4 a b True\n", + "s2 2 5 a a True\n", + "s1 2 5 a a True\n", + "s2 2 6 a b True\n", + "s2 2 7 a b True\n", + "s1 3 3 a a True\n", + "s2 3 4 a b True\n", + "s2 3 5 a a True\n", + "s1 3 5 a a True\n", + "s2 3 6 a b True\n", + "s2 3 7 a b True\n", + "xx 4 4 a b False\n", + "s1 4 5 a a True\n", + "s2 4 6 a b True\n", + "s2 4 7 a b True\n" + ] + }, + { + "data": { + "text/plain": [ + "(True,\n", + " {(1, 1): (0, 1, 'a', 's1'),\n", + " (1, 2): (0, 2, 'a', 's1'),\n", + " (1, 3): (0, 3, 'a', 's1'),\n", + " (1, 4): (1, 3, 'b', 's2'),\n", + " (1, 5): (0, 5, 'a', 's1'),\n", + " (1, 6): (1, 5, 'b', 's2'),\n", + " (1, 7): (1, 6, 'b', 's2'),\n", + " (2, 2): (1, 2, 'a', 's1'),\n", + " (2, 3): (1, 3, 'a', 's1'),\n", + " (2, 4): (2, 3, 'b', 's2'),\n", + " (2, 5): (1, 5, 'a', 's1'),\n", + " (2, 6): (2, 5, 'b', 's2'),\n", + " (2, 7): (2, 6, 'b', 's2'),\n", + " (3, 3): (2, 3, 'a', 's1'),\n", + " (3, 4): (3, 3, 'b', 's2'),\n", + " (3, 5): (2, 5, 'a', 's1'),\n", + " (3, 6): (3, 5, 'b', 's2'),\n", + " (3, 7): (3, 6, 'b', 's2'),\n", + " (4, 5): (3, 5, 'a', 's1'),\n", + " (4, 6): (4, 5, 'b', 's2'),\n", + " (4, 7): (4, 6, 'b', 's2')})" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "is_subseq('aaaa', 'aaababb', return_backpointers=True, debug=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.2+" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/09-resolving-the-bill/tipping-solution.ipynb b/09-resolving-the-bill/tipping-solution.ipynb deleted file mode 100644 index 7635c18..0000000 --- a/09-resolving-the-bill/tipping-solution.ipynb +++ /dev/null @@ -1,604 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# How much do you tip the staff?\n", - "\n", - "Each staff member has a rating, and knows the rating of adjacent members. you have to give a tip to each person, with these conditions:\n", - "\n", - "* each person gets at least on pound\n", - "* if a person's neighbour has the same rating, this person gets at least as much as that neighbour\n", - "* if a person's neighbour has a lower rating, this person gets more than that neighbour" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "import random" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[7, 3, 9, 4, 9]" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ratings = [random.randrange(1, 10) for _ in range(5)]\n", - "ratings" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 1, 1, 1, 1]" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "payments = [1] * len(ratings)\n", - "payments" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 1, 2, 1, 2]" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "for i in range(1, len(payments)):\n", - " if ratings[i] == ratings[i-1]:\n", - " payments[i] = payments[i-1]\n", - " elif ratings[i] > ratings[i-1]:\n", - " payments[i] = payments[i-1] + 1\n", - "payments" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[3, 2, 1, 0]" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "[i for i in reversed(range(len(payments)-1))]" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[2, 1, 2, 1, 2]" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "for i in reversed(range(len(payments) - 1)):\n", - " if ratings[i] == ratings[i+1]:\n", - " payments[i] = max(payments[i], payments[i+1])\n", - " elif ratings[i] > ratings[i+1]:\n", - " payments[i] = max(payments[i], payments[i+1] + 1)\n", - "payments" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def payments_dp(ratings):\n", - " payments = [1] * len(ratings)\n", - " \n", - " for i in range(1, len(payments)):\n", - " if ratings[i] == ratings[i-1]:\n", - " payments[i] = payments[i-1]\n", - " elif ratings[i] > ratings[i-1]:\n", - " payments[i] = payments[i-1] + 1\n", - " \n", - " for i in reversed(range(len(payments) - 1)):\n", - " if ratings[i] == ratings[i+1]:\n", - " payments[i] = max(payments[i], payments[i+1])\n", - " elif ratings[i] > ratings[i+1]:\n", - " payments[i] = max(payments[i], payments[i+1] + 1)\n", - " \n", - " return payments" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[2, 1, 2, 1, 2]" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "payments_dp(ratings)" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false, - "scrolled": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "([76,\n", - " 34,\n", - " 76,\n", - " 40,\n", - " 14,\n", - " 91,\n", - " 88,\n", - " 96,\n", - " 14,\n", - " 1,\n", - " 22,\n", - " 97,\n", - " 75,\n", - " 59,\n", - " 27,\n", - " 24,\n", - " 96,\n", - " 70,\n", - " 79,\n", - " 3],\n", - " [2, 1, 3, 2, 1, 2, 1, 3, 2, 1, 2, 5, 4, 3, 2, 1, 2, 1, 2, 1])" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ratings = [random.randrange(1, 100) for _ in range(20)]\n", - "ratings, payments_dp(ratings)" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 4, 5, 6, 7]" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "payments_dp([1,2,3,4,5,6,7])" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "def is_valid(ratings, payments):\n", - " valid = True\n", - " for i in range(len(ratings)):\n", - " if i == 0:\n", - " left_r = ratings[i]\n", - " left_p = payments[i]\n", - " else:\n", - " left_r = ratings[i-1]\n", - " left_p = payments[i-1]\n", - " if i == len(ratings)-1:\n", - " right_r = ratings[i]\n", - " right_p = payments[i]\n", - " else:\n", - " right_r = ratings[i+1]\n", - " right_p = payments[i+1]\n", - "# print(i, ':', left_r, ratings[i], right_r, ':', left_p, payments[i], right_p)\n", - " if ratings[i] > left_r and payments[i] <= left_p:\n", - " valid = False\n", - " elif ratings[i] == left_r and payments[i] != left_p:\n", - " valid = False\n", - " elif ratings[i] < left_r and payments[i] >= left_p:\n", - " valid = False\n", - " if ratings[i] > right_r and payments[i] <= right_p:\n", - " valid = False\n", - " elif ratings[i] == right_r and payments[i] != right_p:\n", - " valid = False\n", - " elif ratings[i] < right_r and payments[i] >= right_p:\n", - " valid = False\n", - " \n", - " return valid" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 38, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "is_valid(ratings, [1]*len(ratings))" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "is_valid(ratings, payments_dp(ratings))" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def payments_naive(ratings):\n", - " payments = [1] * len(ratings)\n", - " changes = True\n", - " while changes:\n", - " changes = False\n", - " for i in range(len(ratings)):\n", - " if i == 0:\n", - " left_r = ratings[i]\n", - " left_p = payments[i]\n", - " else:\n", - " left_r = ratings[i-1]\n", - " left_p = payments[i-1]\n", - " if i == len(ratings)-1:\n", - " right_r = ratings[i]\n", - " right_p = payments[i]\n", - " else:\n", - " right_r = ratings[i+1]\n", - " right_p = payments[i+1]\n", - " \n", - " if ratings[i] > left_r and payments[i] <= left_p:\n", - " payments[i] = left_p + 1\n", - " changes = True\n", - " elif ratings[i] == left_r and payments[i] != left_p:\n", - " payments[i] = left_p\n", - " changes = True\n", - " elif ratings[i] > right_r and payments[i] <= right_p:\n", - " payments[i] = right_p + 1\n", - " changes = True\n", - " elif ratings[i] == right_r and payments[i] != right_p:\n", - " payments[i] = right_p\n", - " changes = True\n", - "\n", - " return payments" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[2, 1, 3, 2, 1, 2, 1, 3, 2, 1, 2, 5, 4, 3, 2, 1, 2, 1, 2, 1]" - ] - }, - "execution_count": 41, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "payments_naive(ratings)" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[2, 1, 3, 2, 1, 2, 1, 3, 2, 1, 2, 5, 4, 3, 2, 1, 2, 1, 2, 1]" - ] - }, - "execution_count": 42, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "payments_dp(ratings)" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[76, 34, 76, 40, 14, 91, 88, 96, 14, 1, 22, 97, 75, 59, 27, 24, 96, 70, 79, 3]" - ] - }, - "execution_count": 43, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ratings" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 44, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "is_valid(ratings, payments_naive(ratings))" - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "ratings = [random.randrange(1, 100) for _ in range(10**7)]" - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 56, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "is_valid(ratings, payments_naive(ratings))" - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 57, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "is_valid(ratings, payments_dp(ratings))" - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 loop, best of 3: 1min 17s per loop\n" - ] - } - ], - "source": [ - "%%timeit\n", - "payments_naive(ratings)" - ] - }, - { - "cell_type": "code", - "execution_count": 59, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 loop, best of 3: 7.77 s per loop\n" - ] - } - ], - "source": [ - "%%timeit\n", - "payments_dp(ratings)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.5.2" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} -- 2.34.1