mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-11 03:48:03 +00:00
@@ -132,9 +132,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Item(object):\n",
|
||||
@@ -158,9 +156,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Knapsack(object):\n",
|
||||
@@ -208,9 +204,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class KnapsackTopDown(object):\n",
|
||||
@@ -255,9 +249,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Result(object):\n",
|
||||
@@ -328,9 +320,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -342,15 +332,15 @@
|
||||
],
|
||||
"source": [
|
||||
"%%writefile test_knapsack.py\n",
|
||||
"from nose.tools import assert_equal, assert_raises\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestKnapsack(object):\n",
|
||||
"class TestKnapsack(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_knapsack_bottom_up(self):\n",
|
||||
" knapsack = Knapsack()\n",
|
||||
" assert_raises(TypeError, knapsack.fill_knapsack, None, None)\n",
|
||||
" assert_equal(knapsack.fill_knapsack(0, 0), 0)\n",
|
||||
" self.assertRaises(TypeError, knapsack.fill_knapsack, None, None)\n",
|
||||
" self.assertEqual(knapsack.fill_knapsack(0, 0), 0)\n",
|
||||
" items = []\n",
|
||||
" items.append(Item(label='a', value=2, weight=2))\n",
|
||||
" items.append(Item(label='b', value=4, weight=2))\n",
|
||||
@@ -359,18 +349,18 @@
|
||||
" 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",
|
||||
" self.assertEqual(results[0].label, 'd')\n",
|
||||
" self.assertEqual(results[1].label, 'b')\n",
|
||||
" total_value = 0\n",
|
||||
" for item in results:\n",
|
||||
" total_value += item.value\n",
|
||||
" assert_equal(total_value, expected_value)\n",
|
||||
" self.assertEqual(total_value, expected_value)\n",
|
||||
" print('Success: test_knapsack_bottom_up')\n",
|
||||
"\n",
|
||||
" def test_knapsack_top_down(self):\n",
|
||||
" knapsack = KnapsackTopDown()\n",
|
||||
" assert_raises(TypeError, knapsack.fill_knapsack, None, None)\n",
|
||||
" assert_equal(knapsack.fill_knapsack(0, 0), 0)\n",
|
||||
" self.assertRaises(TypeError, knapsack.fill_knapsack, None, None)\n",
|
||||
" self.assertEqual(knapsack.fill_knapsack(0, 0), 0)\n",
|
||||
" items = []\n",
|
||||
" items.append(Item(label='a', value=2, weight=2))\n",
|
||||
" items.append(Item(label='b', value=4, weight=2))\n",
|
||||
@@ -378,7 +368,7 @@
|
||||
" items.append(Item(label='d', value=9, weight=5))\n",
|
||||
" total_weight = 8\n",
|
||||
" expected_value = 13\n",
|
||||
" assert_equal(knapsack.fill_knapsack(items, total_weight), expected_value)\n",
|
||||
" self.assertEqual(knapsack.fill_knapsack(items, total_weight), expected_value)\n",
|
||||
" print('Success: test_knapsack_top_down')\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
@@ -394,9 +384,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -428,9 +416,9 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.4.3"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user