mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Update hanoi challenge (#121)
Update constraints and test cases. Move implementation to a class. Update tests.
This commit is contained in:
parent
14de5e4233
commit
430cca2a01
|
@ -35,6 +35,10 @@
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Can we assume we already have a stack class that can be used for this problem?\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"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -44,8 +48,8 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* None tower(s)\n",
|
"* None tower(s) -> Exception\n",
|
||||||
"* 0 disks\n",
|
"* 0 disks -> None\n",
|
||||||
"* 1 disk\n",
|
"* 1 disk\n",
|
||||||
"* 2 or more disks"
|
"* 2 or more disks"
|
||||||
]
|
]
|
||||||
|
@ -86,7 +90,9 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def hanoi(num_disks, src, dest, buff):\n",
|
"class Hanoi(object):\n",
|
||||||
|
"\n",
|
||||||
|
" def move_disks(self, num_disks, src, dest, buff):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass"
|
" pass"
|
||||||
]
|
]
|
||||||
|
@ -111,35 +117,36 @@
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_hanoi.py\n",
|
"# %load test_hanoi.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"from nose.tools import assert_equal, assert_raises\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestHanoi(object):\n",
|
"class TestHanoi(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_hanoi(self):\n",
|
" def test_hanoi(self):\n",
|
||||||
|
" hanoi = Hanoi()\n",
|
||||||
" num_disks = 3\n",
|
" num_disks = 3\n",
|
||||||
" src = Stack()\n",
|
" src = Stack()\n",
|
||||||
" buff = Stack()\n",
|
" buff = Stack()\n",
|
||||||
" dest = Stack()\n",
|
" dest = Stack()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: None towers')\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",
|
"\n",
|
||||||
" print('Test: 0 disks')\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",
|
" assert_equal(dest.pop(), None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: 1 disk')\n",
|
" print('Test: 1 disk')\n",
|
||||||
" src.push(5)\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",
|
" assert_equal(dest.pop(), 5)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: 2 or more disks')\n",
|
" print('Test: 2 or more disks')\n",
|
||||||
" for i in range(num_disks, -1, -1):\n",
|
" for disk_index in range(num_disks, -1, -1):\n",
|
||||||
" src.push(i)\n",
|
" src.push(disk_index)\n",
|
||||||
" hanoi(num_disks, src, dest, buff)\n",
|
" hanoi.move_disks(num_disks, src, dest, buff)\n",
|
||||||
" for i in range(0, num_disks):\n",
|
" for disk_index in range(0, num_disks):\n",
|
||||||
" assert_equal(dest.pop(), i)\n",
|
" assert_equal(dest.pop(), disk_index)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_hanoi')\n",
|
" print('Success: test_hanoi')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -165,21 +172,21 @@
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
|
@ -34,6 +34,10 @@
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Can we assume we already have a stack class that can be used for this problem?\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"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -43,8 +47,8 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* None tower(s)\n",
|
"* None tower(s) -> Exception\n",
|
||||||
"* 0 disks\n",
|
"* 0 disks -> None\n",
|
||||||
"* 1 disk\n",
|
"* 1 disk\n",
|
||||||
"* 2 or more disks"
|
"* 2 or more disks"
|
||||||
]
|
]
|
||||||
|
@ -93,14 +97,19 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def hanoi(num_disks, src, dest, buff):\n",
|
"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",
|
" 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",
|
" return\n",
|
||||||
" if num_disks > 0:\n",
|
" self.move_disks(num_disks-1, src, buff, dest)\n",
|
||||||
" hanoi(num_disks-1, src, buff, dest)\n",
|
" dest.push(src.pop())\n",
|
||||||
" data = src.pop()\n",
|
" self.move_disks(num_disks-1, buff, dest, src)"
|
||||||
" dest.push(data)\n",
|
|
||||||
" hanoi(num_disks-1, buff, dest, src)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -128,35 +137,36 @@
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_hanoi.py\n",
|
"%%writefile test_hanoi.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"from nose.tools import assert_equal, assert_raises\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestHanoi(object):\n",
|
"class TestHanoi(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_hanoi(self):\n",
|
" def test_hanoi(self):\n",
|
||||||
|
" hanoi = Hanoi()\n",
|
||||||
" num_disks = 3\n",
|
" num_disks = 3\n",
|
||||||
" src = Stack()\n",
|
" src = Stack()\n",
|
||||||
" buff = Stack()\n",
|
" buff = Stack()\n",
|
||||||
" dest = Stack()\n",
|
" dest = Stack()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: None towers')\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",
|
"\n",
|
||||||
" print('Test: 0 disks')\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",
|
" assert_equal(dest.pop(), None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: 1 disk')\n",
|
" print('Test: 1 disk')\n",
|
||||||
" src.push(5)\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",
|
" assert_equal(dest.pop(), 5)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: 2 or more disks')\n",
|
" print('Test: 2 or more disks')\n",
|
||||||
" for i in range(num_disks, -1, -1):\n",
|
" for disk_index in range(num_disks, -1, -1):\n",
|
||||||
" src.push(i)\n",
|
" src.push(disk_index)\n",
|
||||||
" hanoi(num_disks, src, dest, buff)\n",
|
" hanoi.move_disks(num_disks, src, dest, buff)\n",
|
||||||
" for i in range(0, num_disks):\n",
|
" for disk_index in range(0, num_disks):\n",
|
||||||
" assert_equal(dest.pop(), i)\n",
|
" assert_equal(dest.pop(), disk_index)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_hanoi')\n",
|
" print('Success: test_hanoi')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -196,21 +206,21 @@
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
|
@ -1,32 +1,33 @@
|
||||||
from nose.tools import assert_equal
|
from nose.tools import assert_equal, assert_raises
|
||||||
|
|
||||||
|
|
||||||
class TestHanoi(object):
|
class TestHanoi(object):
|
||||||
|
|
||||||
def test_hanoi(self):
|
def test_hanoi(self):
|
||||||
|
hanoi = Hanoi()
|
||||||
num_disks = 3
|
num_disks = 3
|
||||||
src = Stack()
|
src = Stack()
|
||||||
buff = Stack()
|
buff = Stack()
|
||||||
dest = Stack()
|
dest = Stack()
|
||||||
|
|
||||||
print('Test: None towers')
|
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')
|
print('Test: 0 disks')
|
||||||
hanoi(num_disks, src, dest, buff)
|
hanoi.move_disks(num_disks, src, dest, buff)
|
||||||
assert_equal(dest.pop(), None)
|
assert_equal(dest.pop(), None)
|
||||||
|
|
||||||
print('Test: 1 disk')
|
print('Test: 1 disk')
|
||||||
src.push(5)
|
src.push(5)
|
||||||
hanoi(num_disks, src, dest, buff)
|
hanoi.move_disks(num_disks, src, dest, buff)
|
||||||
assert_equal(dest.pop(), 5)
|
assert_equal(dest.pop(), 5)
|
||||||
|
|
||||||
print('Test: 2 or more disks')
|
print('Test: 2 or more disks')
|
||||||
for i in range(num_disks, -1, -1):
|
for disk_index in range(num_disks, -1, -1):
|
||||||
src.push(i)
|
src.push(disk_index)
|
||||||
hanoi(num_disks, src, dest, buff)
|
hanoi.move_disks(num_disks, src, dest, buff)
|
||||||
for i in range(0, num_disks):
|
for disk_index in range(0, num_disks):
|
||||||
assert_equal(dest.pop(), i)
|
assert_equal(dest.pop(), disk_index)
|
||||||
|
|
||||||
print('Success: test_hanoi')
|
print('Success: test_hanoi')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user