mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Move bst min to a class
This commit is contained in:
parent
82c9725000
commit
857497115a
|
@ -90,15 +90,10 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def create_min_bst(array):\n",
|
"class MinBst(object):\n",
|
||||||
" # TODO: Implement me\n",
|
|
||||||
" # Calls _create_min_bst\n",
|
|
||||||
" pass \n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
" def create_min_bst(self, array):\n",
|
||||||
"def _create_min_bst(array, start, end):\n",
|
" # TODO: Implement me"
|
||||||
" # TODO: Implement me\n",
|
|
||||||
" pass"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -115,17 +110,6 @@
|
||||||
"**The following unit test is expected to fail until you solve the challenge.**"
|
"**The following unit test is expected to fail until you solve the challenge.**"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"%run ../tree_height/height.py"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
|
@ -138,15 +122,24 @@
|
||||||
"from nose.tools import assert_equal\n",
|
"from nose.tools import assert_equal\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"def height(node):\n",
|
||||||
|
" if node is None:\n",
|
||||||
|
" return 0\n",
|
||||||
|
" return 1 + max(height(node.left),\n",
|
||||||
|
" height(node.right))\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
"class TestBstMin(object):\n",
|
"class TestBstMin(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bst_min(self):\n",
|
" def test_bst_min(self):\n",
|
||||||
|
" min_bst = MinBst()\n",
|
||||||
" array = [0, 1, 2, 3, 4, 5, 6]\n",
|
" array = [0, 1, 2, 3, 4, 5, 6]\n",
|
||||||
" root = create_min_bst(array)\n",
|
" root = min_bst.create_min_bst(array)\n",
|
||||||
" assert_equal(height(root), 3)\n",
|
" assert_equal(height(root), 3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
" min_bst = MinBst()\n",
|
||||||
" array = [0, 1, 2, 3, 4, 5, 6, 7]\n",
|
" array = [0, 1, 2, 3, 4, 5, 6, 7]\n",
|
||||||
" root = create_min_bst(array)\n",
|
" root = min_bst.create_min_bst(array)\n",
|
||||||
" assert_equal(height(root), 4)\n",
|
" assert_equal(height(root), 4)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bst_min')\n",
|
" print('Success: test_bst_min')\n",
|
||||||
|
|
|
@ -102,20 +102,21 @@
|
||||||
"from __future__ import division\n",
|
"from __future__ import division\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def create_min_bst(array):\n",
|
"class MinBst(object):\n",
|
||||||
" if array is None:\n",
|
|
||||||
" return\n",
|
|
||||||
" return _create_min_bst(array, 0, len(array)-1)\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
|
" def create_min_bst(self, array):\n",
|
||||||
|
" if array is None:\n",
|
||||||
|
" return\n",
|
||||||
|
" return self._create_min_bst(array, 0, len(array)-1)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def _create_min_bst(array, start, end):\n",
|
" def _create_min_bst(self, array, start, end):\n",
|
||||||
" if end < start:\n",
|
" if end < start:\n",
|
||||||
" return None\n",
|
" return None\n",
|
||||||
" mid = (start + end) // 2\n",
|
" mid = (start + end) // 2\n",
|
||||||
" node = Node(array[mid])\n",
|
" node = Node(array[mid])\n",
|
||||||
" node.left = _create_min_bst(array, start, mid-1)\n",
|
" node.left = self._create_min_bst(array, start, mid-1)\n",
|
||||||
" node.right = _create_min_bst(array, mid+1, end)\n",
|
" node.right = self._create_min_bst(array, mid+1, end)\n",
|
||||||
" return node"
|
" return node"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -128,17 +129,6 @@
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"%run ../tree_height/height.py"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 4,
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
@ -156,15 +146,24 @@
|
||||||
"from nose.tools import assert_equal\n",
|
"from nose.tools import assert_equal\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"def height(node):\n",
|
||||||
|
" if node is None:\n",
|
||||||
|
" return 0\n",
|
||||||
|
" return 1 + max(height(node.left),\n",
|
||||||
|
" height(node.right))\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
"class TestBstMin(object):\n",
|
"class TestBstMin(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bst_min(self):\n",
|
" def test_bst_min(self):\n",
|
||||||
|
" min_bst = MinBst()\n",
|
||||||
" array = [0, 1, 2, 3, 4, 5, 6]\n",
|
" array = [0, 1, 2, 3, 4, 5, 6]\n",
|
||||||
" root = create_min_bst(array)\n",
|
" root = min_bst.create_min_bst(array)\n",
|
||||||
" assert_equal(height(root), 3)\n",
|
" assert_equal(height(root), 3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
" min_bst = MinBst()\n",
|
||||||
" array = [0, 1, 2, 3, 4, 5, 6, 7]\n",
|
" array = [0, 1, 2, 3, 4, 5, 6, 7]\n",
|
||||||
" root = create_min_bst(array)\n",
|
" root = min_bst.create_min_bst(array)\n",
|
||||||
" assert_equal(height(root), 4)\n",
|
" assert_equal(height(root), 4)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bst_min')\n",
|
" print('Success: test_bst_min')\n",
|
||||||
|
@ -181,7 +180,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,15 +1,24 @@
|
||||||
from nose.tools import assert_equal
|
from nose.tools import assert_equal
|
||||||
|
|
||||||
|
|
||||||
|
def height(node):
|
||||||
|
if node is None:
|
||||||
|
return 0
|
||||||
|
return 1 + max(height(node.left),
|
||||||
|
height(node.right))
|
||||||
|
|
||||||
|
|
||||||
class TestBstMin(object):
|
class TestBstMin(object):
|
||||||
|
|
||||||
def test_bst_min(self):
|
def test_bst_min(self):
|
||||||
|
min_bst = MinBst()
|
||||||
array = [0, 1, 2, 3, 4, 5, 6]
|
array = [0, 1, 2, 3, 4, 5, 6]
|
||||||
root = create_min_bst(array)
|
root = min_bst.create_min_bst(array)
|
||||||
assert_equal(height(root), 3)
|
assert_equal(height(root), 3)
|
||||||
|
|
||||||
|
min_bst = MinBst()
|
||||||
array = [0, 1, 2, 3, 4, 5, 6, 7]
|
array = [0, 1, 2, 3, 4, 5, 6, 7]
|
||||||
root = create_min_bst(array)
|
root = min_bst.create_min_bst(array)
|
||||||
assert_equal(height(root), 4)
|
assert_equal(height(root), 4)
|
||||||
|
|
||||||
print('Success: test_bst_min')
|
print('Success: test_bst_min')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user