interactive-coding-challenges/recursion_dynamic/hanoi/hanoi_solution.ipynb

229 lines
5.3 KiB
Python
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
2015-07-03 11:03:04 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Implement the [Towers of Hanoi](http://en.wikipedia.org/wiki/Tower_of_Hanoi) with 3 towers and N disks.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"\n",
"* Can we assume we already have a stack class that can be used for this problem?\n",
" * Yes\n",
"* Can we assume the inputs are valid?\n",
" * No\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* None tower(s) -> Exception\n",
"* 0 disks -> None\n",
"* 1 disk\n",
"* 2 or more disks"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"* Create three stacks to represent each tower\n",
"* def hanoi(n, src, dest, buffer):\n",
" * If 0 disks return\n",
" * hanoi(n-1, src, buffer)\n",
" * Move remaining element from src to dest\n",
" * hanoi(n-1, buffer, dest) \n",
"\n",
"Complexity:\n",
"* Time: O(2^n)\n",
"* Space: O(m) where m is the number of recursion levels"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
2015-07-03 11:03:04 +08:00
"collapsed": false
},
"outputs": [],
"source": [
"%run ../../stacks_queues/stack/stack.py"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class Hanoi(object):\n",
"\n",
" def move_disks(self, num_disks, src, dest, buff):\n",
" if src is None or dest is None or buff is None:\n",
" raise TypeError('Cannot have a None input')\n",
" self._move_disks(num_disks, src, dest, buff)\n",
"\n",
" def _move_disks(self, num_disks, src, dest, buff):\n",
" if num_disks == 0:\n",
" return\n",
" self.move_disks(num_disks-1, src, buff, dest)\n",
" dest.push(src.pop())\n",
" self.move_disks(num_disks-1, buff, dest, src)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test\n",
2015-07-03 11:03:04 +08:00
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2015-07-03 11:03:04 +08:00
"Overwriting test_hanoi.py\n"
]
}
],
"source": [
2015-07-03 11:03:04 +08:00
"%%writefile test_hanoi.py\n",
"from nose.tools import assert_equal, assert_raises\n",
"\n",
2015-07-03 11:03:04 +08:00
"\n",
"class TestHanoi(object):\n",
2015-07-12 03:39:59 +08:00
"\n",
" def test_hanoi(self):\n",
" hanoi = Hanoi()\n",
" num_disks = 3\n",
" src = Stack()\n",
" buff = Stack()\n",
" dest = Stack()\n",
"\n",
" print('Test: None towers')\n",
" assert_raises(TypeError, hanoi.move_disks, num_disks, None, None, None)\n",
"\n",
" print('Test: 0 disks')\n",
" hanoi.move_disks(num_disks, src, dest, buff)\n",
" assert_equal(dest.pop(), None)\n",
"\n",
" print('Test: 1 disk')\n",
" src.push(5)\n",
" hanoi.move_disks(num_disks, src, dest, buff)\n",
" assert_equal(dest.pop(), 5)\n",
"\n",
" print('Test: 2 or more disks')\n",
" for disk_index in range(num_disks, -1, -1):\n",
" src.push(disk_index)\n",
" hanoi.move_disks(num_disks, src, dest, buff)\n",
" for disk_index in range(0, num_disks):\n",
" assert_equal(dest.pop(), disk_index)\n",
"\n",
" print('Success: test_hanoi')\n",
"\n",
2015-07-12 03:39:59 +08:00
"\n",
2015-07-03 11:03:04 +08:00
"def main():\n",
" test = TestHanoi()\n",
" test.test_hanoi()\n",
2015-07-12 03:39:59 +08:00
"\n",
"\n",
"if __name__ == '__main__':\n",
2015-07-03 11:03:04 +08:00
" main()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test: None towers\n",
2015-07-03 11:03:04 +08:00
"Test: 0 disks\n",
"Test: 1 disk\n",
"Test: 2 or more disks\n",
"Success: test_hanoi\n"
]
}
],
"source": [
"%run -i test_hanoi.py"
]
}
],
"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.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}