diff --git a/graphs_trees/bst_validate/bst_validate_challenge.ipynb b/graphs_trees/bst_validate/bst_validate_challenge.ipynb index 270b384..e38dce6 100644 --- a/graphs_trees/bst_validate/bst_validate_challenge.ipynb +++ b/graphs_trees/bst_validate/bst_validate_challenge.ipynb @@ -36,7 +36,11 @@ "\n", "* Can the tree have duplicates?\n", " * Yes\n", + "* If this is called on a None input, should we raise an exception?\n", + " * Yes\n", "* Can we assume we already have a Node class?\n", + " * Yes\n", + "* Can we assume this fits in memory?\n", " * Yes" ] }, @@ -128,10 +132,15 @@ "source": [ "# %load test_bst_validate.py\n", "from nose.tools import assert_equal\n", + "from nose.tools import raises\n", "\n", "\n", "class TestBstValidate(object):\n", "\n", + " @raises(Exception)\n", + " def test_bst_validate_empty(self):\n", + " validate_bst(None)\n", + "\n", " def test_bst_validate(self):\n", " node = Node(5)\n", " insert(node, 8)\n", @@ -149,11 +158,13 @@ " root.right = right\n", " root.left.right = invalid\n", " assert_equal(validate_bst(root), False)\n", + "\n", " print('Success: test_bst_validate')\n", "\n", "\n", "def main():\n", " test = TestBstValidate()\n", + " test.test_bst_validate_empty()\n", " test.test_bst_validate()\n", "\n", "\n", @@ -173,21 +184,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/graphs_trees/bst_validate/bst_validate_solution.ipynb b/graphs_trees/bst_validate/bst_validate_solution.ipynb index 1ff8045..2195a5e 100644 --- a/graphs_trees/bst_validate/bst_validate_solution.ipynb +++ b/graphs_trees/bst_validate/bst_validate_solution.ipynb @@ -35,7 +35,11 @@ "\n", "* Can the tree have duplicates?\n", " * Yes\n", + "* If this is called on a None input, should we raise an exception?\n", + " * Yes\n", "* Can we assume we already have a Node class?\n", + " * Yes\n", + "* Can we assume this fits in memory?\n", " * Yes" ] }, @@ -45,6 +49,8 @@ "source": [ "## Test Cases\n", "\n", + "None -> exception\n", + "\n", "
\n", "Valid:\n", " 5\n", @@ -70,7 +76,7 @@ "\n", "We'll use a recursive solution that valides left <= current < right, passing down the min and max values as we do a depth-first traversal.\n", "\n", - "* If the node is None, return False\n", + "* If the node is None, return True\n", "* If min is set and the node's value <= min, return False\n", "* if max is set and the node's value > max, return False\n", "* Recursively call the validate function on node.left, updating max\n", @@ -108,19 +114,21 @@ "outputs": [], "source": [ "def validate_bst(node):\n", - " return __validate_bst__(node, None, None)\n", + " if node is None:\n", + " raise Exception('No root node')\n", + " return _validate_bst(node, None, None)\n", "\n", "\n", - "def __validate_bst__(node, mininum, maximum):\n", + "def _validate_bst(node, mininum, maximum):\n", " if node is None:\n", " return True\n", " if mininum is not None and node.data <= mininum:\n", " return False\n", " if maximum is not None and node.data > maximum:\n", " return False\n", - " if not __validate_bst__(node.left, mininum, node.data):\n", + " if not _validate_bst(node.left, mininum, node.data):\n", " return False\n", - " if not __validate_bst__(node.right, node.data, maximum):\n", + " if not _validate_bst(node.right, node.data, maximum):\n", " return False\n", " return True" ] @@ -150,10 +158,15 @@ "source": [ "%%writefile test_bst_validate.py\n", "from nose.tools import assert_equal\n", + "from nose.tools import raises\n", "\n", "\n", "class TestBstValidate(object):\n", "\n", + " @raises(Exception)\n", + " def test_bst_validate_empty(self):\n", + " validate_bst(None)\n", + "\n", " def test_bst_validate(self):\n", " node = Node(5)\n", " insert(node, 8)\n", @@ -177,6 +190,7 @@ "\n", "def main():\n", " test = TestBstValidate()\n", + " test.test_bst_validate_empty()\n", " test.test_bst_validate()\n", "\n", "\n", @@ -206,21 +220,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/graphs_trees/bst_validate/test_bst_validate.py b/graphs_trees/bst_validate/test_bst_validate.py index 059baed..815a03e 100644 --- a/graphs_trees/bst_validate/test_bst_validate.py +++ b/graphs_trees/bst_validate/test_bst_validate.py @@ -1,8 +1,13 @@ from nose.tools import assert_equal +from nose.tools import raises class TestBstValidate(object): + @raises(Exception) + def test_bst_validate_empty(self): + validate_bst(None) + def test_bst_validate(self): node = Node(5) insert(node, 8) @@ -26,6 +31,7 @@ class TestBstValidate(object): def main(): test = TestBstValidate() + test.test_bst_validate_empty() test.test_bst_validate()