Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change bottom up implementation #229

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions recursion_dynamic/knapsack_01/knapsack_solution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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)."
]
},
{
Expand Down Expand Up @@ -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"
]
Expand Down Expand Up @@ -159,7 +159,7 @@
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
"collapsed": true
},
"outputs": [],
"source": [
Expand All @@ -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",
Expand All @@ -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"
]
},
Expand All @@ -209,7 +203,7 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
"collapsed": true
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -256,7 +250,7 @@
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
"collapsed": true
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -328,9 +322,7 @@
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -394,9 +386,7 @@
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand All @@ -410,6 +400,15 @@
"source": [
"%run -i test_knapsack.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions recursion_dynamic/knapsack_01/test_knapsack.py
Original file line number Diff line number Diff line change
@@ -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):

Expand All @@ -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
Expand Down