diff --git a/recursion_dynamic/knapsack_01/knapsack_solution.ipynb b/recursion_dynamic/knapsack_01/knapsack_solution.ipynb index aff43197..57b527d9 100644 --- a/recursion_dynamic/knapsack_01/knapsack_solution.ipynb +++ b/recursion_dynamic/knapsack_01/knapsack_solution.ipynb @@ -4,7 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." + "This notebook was prepared by [Donne Martin](https://github.com/donnemartin) and [Rodrigo Ferro](https://github.com/rodferro). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." ] }, { @@ -42,7 +42,7 @@ "* Can we assume the inputs are valid?\n", " * No\n", "* Are the inputs in sorted order by val/weight?\n", - " * Yes, if not we'd need to sort them first\n", + " * Yes\n", "* Can we assume this fits memory?\n", " * Yes" ] @@ -159,7 +159,7 @@ "cell_type": "code", "execution_count": 2, "metadata": { - "collapsed": false + "collapsed": true }, "outputs": [], "source": [ @@ -170,15 +170,13 @@ " raise TypeError('input_items or total_weight cannot be None')\n", " if not input_items or total_weight == 0:\n", " return 0\n", - " items = list([Item(label='', value=0, weight=0)] + input_items)\n", + " items = [Item(label='', value=0, weight=0)] + input_items\n", " num_rows = len(items)\n", " num_cols = total_weight + 1\n", - " T = [[None] * num_cols for _ in range(num_rows)]\n", - " for i in range(num_rows):\n", - " for j in range(num_cols):\n", - " if i == 0 or j == 0:\n", - " T[i][j] = 0\n", - " elif j >= items[i].weight:\n", + " T = [[0] * num_cols for _ in range(num_rows)]\n", + " for i in range(1, num_rows):\n", + " for j in range(1, num_cols):\n", + " if j >= items[i].weight:\n", " T[i][j] = max(items[i].value + T[i - 1][j - items[i].weight],\n", " T[i - 1][j])\n", " else:\n", @@ -187,14 +185,10 @@ " i = num_rows - 1\n", " j = num_cols - 1\n", " while T[i][j] != 0:\n", - " if T[i - 1][j] == T[i][j]:\n", - " i -= 1\n", - " elif T[i][j - 1] == T[i][j]:\n", - " j -= 1\n", - " else:\n", + " if T[i][j] != T[i - 1][j]:\n", " results.append(items[i])\n", - " i -= 1\n", " j -= items[i].weight\n", + " i -= 1\n", " return results" ] }, @@ -209,7 +203,7 @@ "cell_type": "code", "execution_count": 3, "metadata": { - "collapsed": false + "collapsed": true }, "outputs": [], "source": [ @@ -256,7 +250,7 @@ "cell_type": "code", "execution_count": 4, "metadata": { - "collapsed": false + "collapsed": true }, "outputs": [], "source": [ @@ -328,9 +322,7 @@ { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -342,8 +334,7 @@ ], "source": [ "%%writefile test_knapsack.py\n", - "from nose.tools import assert_equal, assert_raises\n", - "\n", + "from nose.tools import assert_equal, assert_raises, assert_true\n", "\n", "class TestKnapsack(object):\n", "\n", @@ -359,8 +350,9 @@ " total_weight = 8\n", " expected_value = 13\n", " results = knapsack.fill_knapsack(items, total_weight)\n", - " assert_equal(results[0].label, 'd')\n", - " assert_equal(results[1].label, 'b')\n", + " labels = [x.label for x in results]\n", + " assert_true('b' in labels)\n", + " assert_true('d' in labels)\n", " total_value = 0\n", " for item in results:\n", " total_value += item.value\n", @@ -394,9 +386,7 @@ { "cell_type": "code", "execution_count": 6, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -410,6 +400,15 @@ "source": [ "%run -i test_knapsack.py" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] } ], "metadata": { @@ -428,9 +427,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.6.4" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/recursion_dynamic/knapsack_01/test_knapsack.py b/recursion_dynamic/knapsack_01/test_knapsack.py index 5388b1be..625b5324 100644 --- a/recursion_dynamic/knapsack_01/test_knapsack.py +++ b/recursion_dynamic/knapsack_01/test_knapsack.py @@ -1,5 +1,4 @@ -from nose.tools import assert_equal, assert_raises - +from nose.tools import assert_equal, assert_raises, assert_true class TestKnapsack(object): @@ -15,8 +14,9 @@ def test_knapsack_bottom_up(self): total_weight = 8 expected_value = 13 results = knapsack.fill_knapsack(items, total_weight) - assert_equal(results[0].label, 'd') - assert_equal(results[1].label, 'b') + labels = [x.label for x in results] + assert_true('b' in labels) + assert_true('d' in labels) total_value = 0 for item in results: total_value += item.value