diff --git a/recursion_dynamic/hanoi/hanoi_challenge.ipynb b/recursion_dynamic/hanoi/hanoi_challenge.ipynb index b03217d..624f168 100644 --- a/recursion_dynamic/hanoi/hanoi_challenge.ipynb +++ b/recursion_dynamic/hanoi/hanoi_challenge.ipynb @@ -35,6 +35,10 @@ "## 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" ] }, @@ -44,8 +48,8 @@ "source": [ "## Test Cases\n", "\n", - "* None tower(s)\n", - "* 0 disks\n", + "* None tower(s) -> Exception\n", + "* 0 disks -> None\n", "* 1 disk\n", "* 2 or more disks" ] @@ -86,9 +90,11 @@ }, "outputs": [], "source": [ - "def hanoi(num_disks, src, dest, buff):\n", - " # TODO: Implement me\n", - " pass" + "class Hanoi(object):\n", + "\n", + " def move_disks(self, num_disks, src, dest, buff):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -111,35 +117,36 @@ "outputs": [], "source": [ "# %load test_hanoi.py\n", - "from nose.tools import assert_equal\n", + "from nose.tools import assert_equal, assert_raises\n", "\n", "\n", "class TestHanoi(object):\n", "\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", - " hanoi(num_disks, None, None, None)\n", + " assert_raises(TypeError, hanoi.move_disks, num_disks, None, None, None)\n", "\n", " print('Test: 0 disks')\n", - " hanoi(num_disks, src, dest, buff)\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(num_disks, src, dest, buff)\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 i in range(num_disks, -1, -1):\n", - " src.push(i)\n", - " hanoi(num_disks, src, dest, buff)\n", - " for i in range(0, num_disks):\n", - " assert_equal(dest.pop(), i)\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", @@ -165,21 +172,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.5.0" } }, "nbformat": 4, diff --git a/recursion_dynamic/hanoi/hanoi_solution.ipynb b/recursion_dynamic/hanoi/hanoi_solution.ipynb index b974912..785a8fd 100644 --- a/recursion_dynamic/hanoi/hanoi_solution.ipynb +++ b/recursion_dynamic/hanoi/hanoi_solution.ipynb @@ -34,6 +34,10 @@ "## 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" ] }, @@ -43,8 +47,8 @@ "source": [ "## Test Cases\n", "\n", - "* None tower(s)\n", - "* 0 disks\n", + "* None tower(s) -> Exception\n", + "* 0 disks -> None\n", "* 1 disk\n", "* 2 or more disks" ] @@ -93,14 +97,19 @@ }, "outputs": [], "source": [ - "def hanoi(num_disks, src, dest, buff):\n", - " if src is None or dest is None or buff is None:\n", - " return\n", - " if num_disks > 0:\n", - " hanoi(num_disks-1, src, buff, dest)\n", - " data = src.pop()\n", - " dest.push(data)\n", - " hanoi(num_disks-1, buff, dest, src)" + "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)" ] }, { @@ -128,35 +137,36 @@ ], "source": [ "%%writefile test_hanoi.py\n", - "from nose.tools import assert_equal\n", + "from nose.tools import assert_equal, assert_raises\n", "\n", "\n", "class TestHanoi(object):\n", "\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", - " hanoi(num_disks, None, None, None)\n", + " assert_raises(TypeError, hanoi.move_disks, num_disks, None, None, None)\n", "\n", " print('Test: 0 disks')\n", - " hanoi(num_disks, src, dest, buff)\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(num_disks, src, dest, buff)\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 i in range(num_disks, -1, -1):\n", - " src.push(i)\n", - " hanoi(num_disks, src, dest, buff)\n", - " for i in range(0, num_disks):\n", - " assert_equal(dest.pop(), i)\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", @@ -196,21 +206,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.5.0" } }, "nbformat": 4, diff --git a/recursion_dynamic/hanoi/test_hanoi.py b/recursion_dynamic/hanoi/test_hanoi.py index 7fa113d..f100254 100644 --- a/recursion_dynamic/hanoi/test_hanoi.py +++ b/recursion_dynamic/hanoi/test_hanoi.py @@ -1,32 +1,33 @@ -from nose.tools import assert_equal +from nose.tools import assert_equal, assert_raises class TestHanoi(object): def test_hanoi(self): + hanoi = Hanoi() num_disks = 3 src = Stack() buff = Stack() dest = Stack() print('Test: None towers') - hanoi(num_disks, None, None, None) + assert_raises(TypeError, hanoi.move_disks, num_disks, None, None, None) print('Test: 0 disks') - hanoi(num_disks, src, dest, buff) + hanoi.move_disks(num_disks, src, dest, buff) assert_equal(dest.pop(), None) print('Test: 1 disk') src.push(5) - hanoi(num_disks, src, dest, buff) + hanoi.move_disks(num_disks, src, dest, buff) assert_equal(dest.pop(), 5) print('Test: 2 or more disks') - for i in range(num_disks, -1, -1): - src.push(i) - hanoi(num_disks, src, dest, buff) - for i in range(0, num_disks): - assert_equal(dest.pop(), i) + for disk_index in range(num_disks, -1, -1): + src.push(disk_index) + hanoi.move_disks(num_disks, src, dest, buff) + for disk_index in range(0, num_disks): + assert_equal(dest.pop(), disk_index) print('Success: test_hanoi')