Added hanoi challenge.

This commit is contained in:
Donne Martin
2015-07-02 23:03:04 -04:00
parent 129c7a889f
commit 88eb5a9895
4 changed files with 271 additions and 15 deletions

View File

@@ -4,7 +4,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/coding-challenges).</i></small>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution Notebook"
]
},
{
@@ -26,7 +33,7 @@
"source": [
"## Constraints\n",
"\n",
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
"*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
"\n",
"* Can we assume we already have a stack class that can be used for this problem?\n",
" * Yes"
@@ -38,7 +45,7 @@
"source": [
"## Test Cases\n",
"\n",
"* NULL towers\n",
"* NULL tower(s)\n",
"* 0 disks\n",
"* 1 disk\n",
"* 2 or more disks"
@@ -73,11 +80,11 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
"%run stack.py"
"%run ../stack/stack.py"
]
},
{
@@ -103,8 +110,7 @@
"metadata": {},
"source": [
"## Unit Test\n",
"\n",
"*It is important to identify and run through general and edge cases from the [Test Cases](#Test-Cases) section by hand. You generally will not be asked to write a unit test like what is shown below.*"
"\n"
]
},
{
@@ -118,18 +124,17 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Test: NULL towers\n",
"Test: 0 disks\n",
"Test: 1 disk\n",
"Test: 2 or more disks\n",
"Success: test_hanoi\n"
"Overwriting test_hanoi.py\n"
]
}
],
"source": [
"%%writefile test_hanoi.py\n",
"from nose.tools import assert_equal\n",
"\n",
"class Test(object):\n",
"\n",
"class TestHanoi(object):\n",
" \n",
" def test_hanoi(self):\n",
" num_disks = 3\n",
" src = Stack()\n",
@@ -157,9 +162,35 @@
"\n",
" print('Success: test_hanoi')\n",
"\n",
"def main():\n",
" test = TestHanoi()\n",
" test.test_hanoi()\n",
" \n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_hanoi()"
" main()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test: NULL towers\n",
"Test: 0 disks\n",
"Test: 1 disk\n",
"Test: 2 or more disks\n",
"Success: test_hanoi\n"
]
}
],
"source": [
"%run -i test_hanoi.py"
]
}
],