mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-05 00:48:03 +00:00
Update hanoi challenge (#121)
Update constraints and test cases. Move implementation to a class. Update tests.
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user