mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-07 18:08:01 +00:00
@@ -40,4 +40,4 @@ class Bst(object):
|
|||||||
node.right.parent = node
|
node.right.parent = node
|
||||||
return node.right
|
return node.right
|
||||||
else:
|
else:
|
||||||
return self._insert(node.right, data)
|
return self._insert(node.right, data)
|
||||||
|
|||||||
@@ -84,9 +84,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class Node(object):\n",
|
"class Node(object):\n",
|
||||||
@@ -142,18 +140,17 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_bst.py\n",
|
"# %load test_bst.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestTree(object):\n",
|
"class TestTree(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self, *args, **kwargs):\n",
|
||||||
|
" super(TestTree, self).__init__()\n",
|
||||||
" self.results = Results()\n",
|
" self.results = Results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_tree_one(self):\n",
|
" def test_tree_one(self):\n",
|
||||||
@@ -164,7 +161,7 @@
|
|||||||
" bst.insert(1)\n",
|
" bst.insert(1)\n",
|
||||||
" bst.insert(3)\n",
|
" bst.insert(3)\n",
|
||||||
" in_order_traversal(bst.root, self.results.add_result)\n",
|
" in_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
|
" self.assertEqual(str(self.results), '[1, 2, 3, 5, 8]')\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_tree_two(self):\n",
|
" def test_tree_two(self):\n",
|
||||||
@@ -175,7 +172,7 @@
|
|||||||
" bst.insert(4)\n",
|
" bst.insert(4)\n",
|
||||||
" bst.insert(5)\n",
|
" bst.insert(5)\n",
|
||||||
" in_order_traversal(bst.root, self.results.add_result)\n",
|
" in_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n",
|
" self.assertEqual(str(self.results), '[1, 2, 3, 4, 5]')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_tree')\n",
|
" print('Success: test_tree')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -216,9 +213,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,9 +99,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -161,9 +159,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run bst.py"
|
"%run bst.py"
|
||||||
@@ -179,9 +175,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run dfs.py"
|
"%run dfs.py"
|
||||||
@@ -190,9 +184,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../utils/results.py"
|
"%run ../utils/results.py"
|
||||||
@@ -201,9 +193,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -215,12 +205,13 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_bst.py\n",
|
"%%writefile test_bst.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestTree(object):\n",
|
"class TestTree(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self, *args, **kwargs):\n",
|
||||||
|
" super(TestTree, self).__init__()\n",
|
||||||
" self.results = Results()\n",
|
" self.results = Results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_tree_one(self):\n",
|
" def test_tree_one(self):\n",
|
||||||
@@ -231,7 +222,7 @@
|
|||||||
" bst.insert(1)\n",
|
" bst.insert(1)\n",
|
||||||
" bst.insert(3)\n",
|
" bst.insert(3)\n",
|
||||||
" in_order_traversal(bst.root, self.results.add_result)\n",
|
" in_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
|
" self.assertEqual(str(self.results), '[1, 2, 3, 5, 8]')\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_tree_two(self):\n",
|
" def test_tree_two(self):\n",
|
||||||
@@ -242,7 +233,7 @@
|
|||||||
" bst.insert(4)\n",
|
" bst.insert(4)\n",
|
||||||
" bst.insert(5)\n",
|
" bst.insert(5)\n",
|
||||||
" in_order_traversal(bst.root, self.results.add_result)\n",
|
" in_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n",
|
" self.assertEqual(str(self.results), '[1, 2, 3, 4, 5]')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_tree')\n",
|
" print('Success: test_tree')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -260,9 +251,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": 6,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -293,9 +282,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestTree(object):
|
class TestTree(unittest.TestCase):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(TestTree, self).__init__()
|
||||||
self.results = Results()
|
self.results = Results()
|
||||||
|
|
||||||
def test_tree_one(self):
|
def test_tree_one(self):
|
||||||
@@ -14,7 +15,7 @@ class TestTree(object):
|
|||||||
bst.insert(1)
|
bst.insert(1)
|
||||||
bst.insert(3)
|
bst.insert(3)
|
||||||
in_order_traversal(bst.root, self.results.add_result)
|
in_order_traversal(bst.root, self.results.add_result)
|
||||||
assert_equal(str(self.results), '[1, 2, 3, 5, 8]')
|
self.assertEqual(str(self.results), '[1, 2, 3, 5, 8]')
|
||||||
self.results.clear_results()
|
self.results.clear_results()
|
||||||
|
|
||||||
def test_tree_two(self):
|
def test_tree_two(self):
|
||||||
@@ -25,7 +26,7 @@ class TestTree(object):
|
|||||||
bst.insert(4)
|
bst.insert(4)
|
||||||
bst.insert(5)
|
bst.insert(5)
|
||||||
in_order_traversal(bst.root, self.results.add_result)
|
in_order_traversal(bst.root, self.results.add_result)
|
||||||
assert_equal(str(self.results), '[1, 2, 3, 4, 5]')
|
self.assertEqual(str(self.results), '[1, 2, 3, 4, 5]')
|
||||||
|
|
||||||
print('Success: test_tree')
|
print('Success: test_tree')
|
||||||
|
|
||||||
@@ -37,4 +38,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -85,9 +85,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class MinBst(object):\n",
|
"class MinBst(object):\n",
|
||||||
@@ -113,13 +111,11 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_bst_min.py\n",
|
"# %load test_bst_min.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def height(node):\n",
|
"def height(node):\n",
|
||||||
@@ -129,18 +125,18 @@
|
|||||||
" height(node.right))\n",
|
" height(node.right))\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestBstMin(object):\n",
|
"class TestBstMin(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bst_min(self):\n",
|
" def test_bst_min(self):\n",
|
||||||
" min_bst = MinBst()\n",
|
" min_bst = MinBst()\n",
|
||||||
" array = [0, 1, 2, 3, 4, 5, 6]\n",
|
" array = [0, 1, 2, 3, 4, 5, 6]\n",
|
||||||
" root = min_bst.create_min_bst(array)\n",
|
" root = min_bst.create_min_bst(array)\n",
|
||||||
" assert_equal(height(root), 3)\n",
|
" self.assertEqual(height(root), 3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" min_bst = MinBst()\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 = min_bst.create_min_bst(array)\n",
|
" root = min_bst.create_min_bst(array)\n",
|
||||||
" assert_equal(height(root), 4)\n",
|
" self.assertEqual(height(root), 4)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bst_min')\n",
|
" print('Success: test_bst_min')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -180,9 +176,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,9 +83,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../bst/bst.py"
|
"%run ../bst/bst.py"
|
||||||
@@ -93,10 +91,8 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from __future__ import division\n",
|
"from __future__ import division\n",
|
||||||
@@ -129,9 +125,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -143,7 +137,7 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_bst_min.py\n",
|
"%%writefile test_bst_min.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def height(node):\n",
|
"def height(node):\n",
|
||||||
@@ -153,18 +147,18 @@
|
|||||||
" height(node.right))\n",
|
" height(node.right))\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestBstMin(object):\n",
|
"class TestBstMin(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bst_min(self):\n",
|
" def test_bst_min(self):\n",
|
||||||
" min_bst = MinBst()\n",
|
" min_bst = MinBst()\n",
|
||||||
" array = [0, 1, 2, 3, 4, 5, 6]\n",
|
" array = [0, 1, 2, 3, 4, 5, 6]\n",
|
||||||
" root = min_bst.create_min_bst(array)\n",
|
" root = min_bst.create_min_bst(array)\n",
|
||||||
" assert_equal(height(root), 3)\n",
|
" self.assertEqual(height(root), 3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" min_bst = MinBst()\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 = min_bst.create_min_bst(array)\n",
|
" root = min_bst.create_min_bst(array)\n",
|
||||||
" assert_equal(height(root), 4)\n",
|
" self.assertEqual(height(root), 4)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bst_min')\n",
|
" print('Success: test_bst_min')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -181,9 +175,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -214,9 +206,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
def height(node):
|
def height(node):
|
||||||
@@ -8,18 +8,18 @@ def height(node):
|
|||||||
height(node.right))
|
height(node.right))
|
||||||
|
|
||||||
|
|
||||||
class TestBstMin(object):
|
class TestBstMin(unittest.TestCase):
|
||||||
|
|
||||||
def test_bst_min(self):
|
def test_bst_min(self):
|
||||||
min_bst = MinBst()
|
min_bst = MinBst()
|
||||||
array = [0, 1, 2, 3, 4, 5, 6]
|
array = [0, 1, 2, 3, 4, 5, 6]
|
||||||
root = min_bst.create_min_bst(array)
|
root = min_bst.create_min_bst(array)
|
||||||
assert_equal(height(root), 3)
|
self.assertEqual(height(root), 3)
|
||||||
|
|
||||||
min_bst = MinBst()
|
min_bst = MinBst()
|
||||||
array = [0, 1, 2, 3, 4, 5, 6, 7]
|
array = [0, 1, 2, 3, 4, 5, 6, 7]
|
||||||
root = min_bst.create_min_bst(array)
|
root = min_bst.create_min_bst(array)
|
||||||
assert_equal(height(root), 4)
|
self.assertEqual(height(root), 4)
|
||||||
|
|
||||||
print('Success: test_bst_min')
|
print('Success: test_bst_min')
|
||||||
|
|
||||||
@@ -30,4 +30,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -136,14 +136,14 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_bst_second_largest.py\n",
|
"# %load test_bst_second_largest.py\n",
|
||||||
"from nose.tools import assert_equal, assert_raises\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestBstSecondLargest(object):\n",
|
"class TestBstSecondLargest(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bst_second_largest(self):\n",
|
" def test_bst_second_largest(self):\n",
|
||||||
" bst = Solution(None)\n",
|
" bst = Solution(None)\n",
|
||||||
" assert_raises(TypeError, bst.find_second_largest)\n",
|
" self.assertRaises(TypeError, bst.find_second_largest)\n",
|
||||||
" root = Node(10)\n",
|
" root = Node(10)\n",
|
||||||
" bst = Solution(root)\n",
|
" bst = Solution(root)\n",
|
||||||
" node5 = bst.insert(5)\n",
|
" node5 = bst.insert(5)\n",
|
||||||
@@ -155,13 +155,13 @@
|
|||||||
" node2 = bst.insert(2)\n",
|
" node2 = bst.insert(2)\n",
|
||||||
" node4 = bst.insert(4)\n",
|
" node4 = bst.insert(4)\n",
|
||||||
" node30 = bst.insert(30)\n",
|
" node30 = bst.insert(30)\n",
|
||||||
" assert_equal(bst.find_second_largest(), node20)\n",
|
" self.assertEqual(bst.find_second_largest(), node20)\n",
|
||||||
" root = Node(10)\n",
|
" root = Node(10)\n",
|
||||||
" bst = Solution(root)\n",
|
" bst = Solution(root)\n",
|
||||||
" node5 = bst.insert(5)\n",
|
" node5 = bst.insert(5)\n",
|
||||||
" node3 = bst.insert(3)\n",
|
" node3 = bst.insert(3)\n",
|
||||||
" node7 = bst.insert(7)\n",
|
" node7 = bst.insert(7)\n",
|
||||||
" assert_equal(bst.find_second_largest(), node7)\n",
|
" self.assertEqual(bst.find_second_largest(), node7)\n",
|
||||||
" print('Success: test_bst_second_largest')\n",
|
" print('Success: test_bst_second_largest')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -200,7 +200,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.6.4"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -124,9 +124,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../bst/bst.py"
|
"%run ../bst/bst.py"
|
||||||
@@ -135,9 +133,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class Solution(Bst):\n",
|
"class Solution(Bst):\n",
|
||||||
@@ -175,9 +171,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -189,14 +183,14 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_bst_second_largest.py\n",
|
"%%writefile test_bst_second_largest.py\n",
|
||||||
"from nose.tools import assert_equal, assert_raises\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestBstSecondLargest(object):\n",
|
"class TestBstSecondLargest(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bst_second_largest(self):\n",
|
" def test_bst_second_largest(self):\n",
|
||||||
" bst = Solution(None)\n",
|
" bst = Solution(None)\n",
|
||||||
" assert_raises(TypeError, bst.find_second_largest)\n",
|
" self.assertRaises(TypeError, bst.find_second_largest)\n",
|
||||||
" root = Node(10)\n",
|
" root = Node(10)\n",
|
||||||
" bst = Solution(root)\n",
|
" bst = Solution(root)\n",
|
||||||
" node5 = bst.insert(5)\n",
|
" node5 = bst.insert(5)\n",
|
||||||
@@ -208,13 +202,13 @@
|
|||||||
" node2 = bst.insert(2)\n",
|
" node2 = bst.insert(2)\n",
|
||||||
" node4 = bst.insert(4)\n",
|
" node4 = bst.insert(4)\n",
|
||||||
" node30 = bst.insert(30)\n",
|
" node30 = bst.insert(30)\n",
|
||||||
" assert_equal(bst.find_second_largest(), node20)\n",
|
" self.assertEqual(bst.find_second_largest(), node20)\n",
|
||||||
" root = Node(10)\n",
|
" root = Node(10)\n",
|
||||||
" bst = Solution(root)\n",
|
" bst = Solution(root)\n",
|
||||||
" node5 = bst.insert(5)\n",
|
" node5 = bst.insert(5)\n",
|
||||||
" node3 = bst.insert(3)\n",
|
" node3 = bst.insert(3)\n",
|
||||||
" node7 = bst.insert(7)\n",
|
" node7 = bst.insert(7)\n",
|
||||||
" assert_equal(bst.find_second_largest(), node7)\n",
|
" self.assertEqual(bst.find_second_largest(), node7)\n",
|
||||||
" print('Success: test_bst_second_largest')\n",
|
" print('Success: test_bst_second_largest')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -230,9 +224,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -263,9 +255,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
from nose.tools import assert_equal, assert_raises
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestBstSecondLargest(object):
|
class TestBstSecondLargest(unittest.TestCase):
|
||||||
|
|
||||||
def test_bst_second_largest(self):
|
def test_bst_second_largest(self):
|
||||||
bst = Solution(None)
|
bst = Solution(None)
|
||||||
assert_raises(TypeError, bst.find_second_largest)
|
self.assertRaises(TypeError, bst.find_second_largest)
|
||||||
root = Node(10)
|
root = Node(10)
|
||||||
bst = Solution(root)
|
bst = Solution(root)
|
||||||
node5 = bst.insert(5)
|
node5 = bst.insert(5)
|
||||||
@@ -17,13 +17,13 @@ class TestBstSecondLargest(object):
|
|||||||
node2 = bst.insert(2)
|
node2 = bst.insert(2)
|
||||||
node4 = bst.insert(4)
|
node4 = bst.insert(4)
|
||||||
node30 = bst.insert(30)
|
node30 = bst.insert(30)
|
||||||
assert_equal(bst.find_second_largest(), node20)
|
self.assertEqual(bst.find_second_largest(), node20)
|
||||||
root = Node(10)
|
root = Node(10)
|
||||||
bst = Solution(root)
|
bst = Solution(root)
|
||||||
node5 = bst.insert(5)
|
node5 = bst.insert(5)
|
||||||
node3 = bst.insert(3)
|
node3 = bst.insert(3)
|
||||||
node7 = bst.insert(7)
|
node7 = bst.insert(7)
|
||||||
assert_equal(bst.find_second_largest(), node7)
|
self.assertEqual(bst.find_second_largest(), node7)
|
||||||
print('Success: test_bst_second_largest')
|
print('Success: test_bst_second_largest')
|
||||||
|
|
||||||
|
|
||||||
@@ -33,4 +33,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -100,9 +100,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class BstSuccessor(object):\n",
|
"class BstSuccessor(object):\n",
|
||||||
@@ -129,19 +127,15 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_bst_successor.py\n",
|
"# %load test_bst_successor.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"from nose.tools import raises\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestBstSuccessor(object):\n",
|
"class TestBstSuccessor(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" @raises(Exception)\n",
|
|
||||||
" def test_bst_successor_empty(self):\n",
|
" def test_bst_successor_empty(self):\n",
|
||||||
" bst_successor = BstSuccessor()\n",
|
" bst_successor = BstSuccessor()\n",
|
||||||
" bst_successor.get_next(None)\n",
|
" bst_successor.get_next(None)\n",
|
||||||
@@ -164,10 +158,10 @@
|
|||||||
" nodes[9] = bst.insert(9)\n",
|
" nodes[9] = bst.insert(9)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst_successor = BstSuccessor()\n",
|
" bst_successor = BstSuccessor()\n",
|
||||||
" assert_equal(bst_successor.get_next(nodes[4]), 5)\n",
|
" self.assertEqual(bst_successor.get_next(nodes[4]), 5)\n",
|
||||||
" assert_equal(bst_successor.get_next(nodes[5]), 6)\n",
|
" self.assertEqual(bst_successor.get_next(nodes[5]), 6)\n",
|
||||||
" assert_equal(bst_successor.get_next(nodes[8]), 9)\n",
|
" self.assertEqual(bst_successor.get_next(nodes[8]), 9)\n",
|
||||||
" assert_equal(bst_successor.get_next(nodes[15]), None)\n",
|
" self.assertEqual(bst_successor.get_next(nodes[15]), None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bst_successor')\n",
|
" print('Success: test_bst_successor')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -175,7 +169,7 @@
|
|||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestBstSuccessor()\n",
|
" test = TestBstSuccessor()\n",
|
||||||
" test.test_bst_successor()\n",
|
" test.test_bst_successor()\n",
|
||||||
" test.test_bst_successor_empty()\n",
|
" test.assertRaises(TypeError, test.test_bst_successor_empty)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
@@ -208,9 +202,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,9 +95,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../bst/bst.py"
|
"%run ../bst/bst.py"
|
||||||
@@ -106,9 +104,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class BstSuccessor(object):\n",
|
"class BstSuccessor(object):\n",
|
||||||
@@ -148,9 +144,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -162,13 +156,11 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_bst_successor.py\n",
|
"%%writefile test_bst_successor.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"from nose.tools import raises\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestBstSuccessor(object):\n",
|
"class TestBstSuccessor(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" @raises(Exception)\n",
|
|
||||||
" def test_bst_successor_empty(self):\n",
|
" def test_bst_successor_empty(self):\n",
|
||||||
" bst_successor = BstSuccessor()\n",
|
" bst_successor = BstSuccessor()\n",
|
||||||
" bst_successor.get_next(None)\n",
|
" bst_successor.get_next(None)\n",
|
||||||
@@ -191,10 +183,10 @@
|
|||||||
" nodes[9] = bst.insert(9)\n",
|
" nodes[9] = bst.insert(9)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst_successor = BstSuccessor()\n",
|
" bst_successor = BstSuccessor()\n",
|
||||||
" assert_equal(bst_successor.get_next(nodes[4]), 5)\n",
|
" self.assertEqual(bst_successor.get_next(nodes[4]), 5)\n",
|
||||||
" assert_equal(bst_successor.get_next(nodes[5]), 6)\n",
|
" self.assertEqual(bst_successor.get_next(nodes[5]), 6)\n",
|
||||||
" assert_equal(bst_successor.get_next(nodes[8]), 9)\n",
|
" self.assertEqual(bst_successor.get_next(nodes[8]), 9)\n",
|
||||||
" assert_equal(bst_successor.get_next(nodes[15]), None)\n",
|
" self.assertEqual(bst_successor.get_next(nodes[15]), None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bst_successor')\n",
|
" print('Success: test_bst_successor')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -202,7 +194,7 @@
|
|||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestBstSuccessor()\n",
|
" test = TestBstSuccessor()\n",
|
||||||
" test.test_bst_successor()\n",
|
" test.test_bst_successor()\n",
|
||||||
" test.test_bst_successor_empty()\n",
|
" test.assertRaises(TypeError, test.test_bst_successor_empty)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
@@ -212,9 +204,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -245,9 +235,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
from nose.tools import raises
|
|
||||||
|
|
||||||
|
|
||||||
class TestBstSuccessor(object):
|
class TestBstSuccessor(unittest.TestCase):
|
||||||
|
|
||||||
@raises(Exception)
|
|
||||||
def test_bst_successor_empty(self):
|
def test_bst_successor_empty(self):
|
||||||
bst_successor = BstSuccessor()
|
bst_successor = BstSuccessor()
|
||||||
bst_successor.get_next(None)
|
bst_successor.get_next(None)
|
||||||
@@ -27,10 +25,10 @@ class TestBstSuccessor(object):
|
|||||||
nodes[9] = bst.insert(9)
|
nodes[9] = bst.insert(9)
|
||||||
|
|
||||||
bst_successor = BstSuccessor()
|
bst_successor = BstSuccessor()
|
||||||
assert_equal(bst_successor.get_next(nodes[4]), 5)
|
self.assertEqual(bst_successor.get_next(nodes[4]), 5)
|
||||||
assert_equal(bst_successor.get_next(nodes[5]), 6)
|
self.assertEqual(bst_successor.get_next(nodes[5]), 6)
|
||||||
assert_equal(bst_successor.get_next(nodes[8]), 9)
|
self.assertEqual(bst_successor.get_next(nodes[8]), 9)
|
||||||
assert_equal(bst_successor.get_next(nodes[15]), None)
|
self.assertEqual(bst_successor.get_next(nodes[15]), None)
|
||||||
|
|
||||||
print('Success: test_bst_successor')
|
print('Success: test_bst_successor')
|
||||||
|
|
||||||
@@ -38,8 +36,8 @@ class TestBstSuccessor(object):
|
|||||||
def main():
|
def main():
|
||||||
test = TestBstSuccessor()
|
test = TestBstSuccessor()
|
||||||
test.test_bst_successor()
|
test.test_bst_successor()
|
||||||
test.test_bst_successor_empty()
|
test.assertRaises(TypeError, test.test_bst_successor_empty)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -88,21 +88,59 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../bst/bst.py\n",
|
"# %load ../bst/bst.py\n",
|
||||||
"%load ../bst/bst.py"
|
"class Node(object):\n",
|
||||||
|
"\n",
|
||||||
|
" def __init__(self, data):\n",
|
||||||
|
" self.data = data\n",
|
||||||
|
" self.left = None\n",
|
||||||
|
" self.right = None\n",
|
||||||
|
" self.parent = None\n",
|
||||||
|
"\n",
|
||||||
|
" def __repr__(self):\n",
|
||||||
|
" return str(self.data)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class Bst(object):\n",
|
||||||
|
"\n",
|
||||||
|
" def __init__(self, root=None):\n",
|
||||||
|
" self.root = root\n",
|
||||||
|
"\n",
|
||||||
|
" def insert(self, data):\n",
|
||||||
|
" if data is None:\n",
|
||||||
|
" raise TypeError('data cannot be None')\n",
|
||||||
|
" if self.root is None:\n",
|
||||||
|
" self.root = Node(data)\n",
|
||||||
|
" return self.root\n",
|
||||||
|
" else:\n",
|
||||||
|
" return self._insert(self.root, data)\n",
|
||||||
|
"\n",
|
||||||
|
" def _insert(self, node, data):\n",
|
||||||
|
" if node is None:\n",
|
||||||
|
" return Node(data)\n",
|
||||||
|
" if data <= node.data:\n",
|
||||||
|
" if node.left is None:\n",
|
||||||
|
" node.left = self._insert(node.left, data)\n",
|
||||||
|
" node.left.parent = node\n",
|
||||||
|
" return node.left\n",
|
||||||
|
" else:\n",
|
||||||
|
" return self._insert(node.left, data)\n",
|
||||||
|
" else:\n",
|
||||||
|
" if node.right is None:\n",
|
||||||
|
" node.right = self._insert(node.right, data)\n",
|
||||||
|
" node.right.parent = node\n",
|
||||||
|
" return node.right\n",
|
||||||
|
" else:\n",
|
||||||
|
" return self._insert(node.right, data)\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class BstValidate(Bst):\n",
|
"class BstValidate(Bst):\n",
|
||||||
@@ -129,21 +167,18 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_bst_validate.py\n",
|
"# %load test_bst_validate.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"from nose.tools import raises\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestBstValidate(object):\n",
|
"class TestBstValidate(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" @raises(Exception)\n",
|
|
||||||
" def test_bst_validate_empty(self):\n",
|
" def test_bst_validate_empty(self):\n",
|
||||||
" validate_bst(None)\n",
|
" bst = BstValidate(None)\n",
|
||||||
|
" bst.validate()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bst_validate(self):\n",
|
" def test_bst_validate(self):\n",
|
||||||
" bst = BstValidate(Node(5))\n",
|
" bst = BstValidate(Node(5))\n",
|
||||||
@@ -152,7 +187,7 @@
|
|||||||
" bst.insert(6)\n",
|
" bst.insert(6)\n",
|
||||||
" bst.insert(4)\n",
|
" bst.insert(4)\n",
|
||||||
" bst.insert(7)\n",
|
" bst.insert(7)\n",
|
||||||
" assert_equal(bst.validate(), True)\n",
|
" self.assertEqual(bst.validate(), True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst = BstValidate(Node(5))\n",
|
" bst = BstValidate(Node(5))\n",
|
||||||
" left = Node(5)\n",
|
" left = Node(5)\n",
|
||||||
@@ -161,14 +196,14 @@
|
|||||||
" bst.root.left = left\n",
|
" bst.root.left = left\n",
|
||||||
" bst.root.right = right\n",
|
" bst.root.right = right\n",
|
||||||
" bst.root.left.right = invalid\n",
|
" bst.root.left.right = invalid\n",
|
||||||
" assert_equal(bst.validate(), False)\n",
|
" self.assertEqual(bst.validate(), False)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bst_validate')\n",
|
" print('Success: test_bst_validate')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestBstValidate()\n",
|
" test = TestBstValidate()\n",
|
||||||
" test.test_bst_validate_empty()\n",
|
" test.assertRaises(TypeError, test.test_bst_validate_empty)\n",
|
||||||
" test.test_bst_validate()\n",
|
" test.test_bst_validate()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -202,9 +237,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,9 +99,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../bst/bst.py"
|
"%run ../bst/bst.py"
|
||||||
@@ -110,9 +108,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import sys\n",
|
"import sys\n",
|
||||||
@@ -147,9 +143,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -161,15 +155,14 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_bst_validate.py\n",
|
"%%writefile test_bst_validate.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"from nose.tools import raises\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestBstValidate(object):\n",
|
"class TestBstValidate(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" @raises(Exception)\n",
|
|
||||||
" def test_bst_validate_empty(self):\n",
|
" def test_bst_validate_empty(self):\n",
|
||||||
" validate_bst(None)\n",
|
" bst = BstValidate(None)\n",
|
||||||
|
" bst.validate()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bst_validate(self):\n",
|
" def test_bst_validate(self):\n",
|
||||||
" bst = BstValidate(Node(5))\n",
|
" bst = BstValidate(Node(5))\n",
|
||||||
@@ -178,7 +171,7 @@
|
|||||||
" bst.insert(6)\n",
|
" bst.insert(6)\n",
|
||||||
" bst.insert(4)\n",
|
" bst.insert(4)\n",
|
||||||
" bst.insert(7)\n",
|
" bst.insert(7)\n",
|
||||||
" assert_equal(bst.validate(), True)\n",
|
" self.assertEqual(bst.validate(), True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst = BstValidate(Node(5))\n",
|
" bst = BstValidate(Node(5))\n",
|
||||||
" left = Node(5)\n",
|
" left = Node(5)\n",
|
||||||
@@ -187,14 +180,14 @@
|
|||||||
" bst.root.left = left\n",
|
" bst.root.left = left\n",
|
||||||
" bst.root.right = right\n",
|
" bst.root.right = right\n",
|
||||||
" bst.root.left.right = invalid\n",
|
" bst.root.left.right = invalid\n",
|
||||||
" assert_equal(bst.validate(), False)\n",
|
" self.assertEqual(bst.validate(), False)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bst_validate')\n",
|
" print('Success: test_bst_validate')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestBstValidate()\n",
|
" test = TestBstValidate()\n",
|
||||||
" test.test_bst_validate_empty()\n",
|
" test.assertRaises(TypeError, test.test_bst_validate_empty)\n",
|
||||||
" test.test_bst_validate()\n",
|
" test.test_bst_validate()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -205,9 +198,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -238,9 +229,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
from nose.tools import raises
|
|
||||||
|
|
||||||
|
|
||||||
class TestBstValidate(object):
|
class TestBstValidate(unittest.TestCase):
|
||||||
|
|
||||||
@raises(Exception)
|
|
||||||
def test_bst_validate_empty(self):
|
def test_bst_validate_empty(self):
|
||||||
validate_bst(None)
|
bst = BstValidate(None)
|
||||||
|
bst.validate()
|
||||||
|
|
||||||
def test_bst_validate(self):
|
def test_bst_validate(self):
|
||||||
bst = BstValidate(Node(5))
|
bst = BstValidate(Node(5))
|
||||||
@@ -15,7 +14,7 @@ class TestBstValidate(object):
|
|||||||
bst.insert(6)
|
bst.insert(6)
|
||||||
bst.insert(4)
|
bst.insert(4)
|
||||||
bst.insert(7)
|
bst.insert(7)
|
||||||
assert_equal(bst.validate(), True)
|
self.assertEqual(bst.validate(), True)
|
||||||
|
|
||||||
bst = BstValidate(Node(5))
|
bst = BstValidate(Node(5))
|
||||||
left = Node(5)
|
left = Node(5)
|
||||||
@@ -24,16 +23,16 @@ class TestBstValidate(object):
|
|||||||
bst.root.left = left
|
bst.root.left = left
|
||||||
bst.root.right = right
|
bst.root.right = right
|
||||||
bst.root.left.right = invalid
|
bst.root.left.right = invalid
|
||||||
assert_equal(bst.validate(), False)
|
self.assertEqual(bst.validate(), False)
|
||||||
|
|
||||||
print('Success: test_bst_validate')
|
print('Success: test_bst_validate')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test = TestBstValidate()
|
test = TestBstValidate()
|
||||||
test.test_bst_validate_empty()
|
test.assertRaises(TypeError, test.test_bst_validate_empty)
|
||||||
test.test_bst_validate()
|
test.test_bst_validate()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -87,9 +87,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class BstBalance(Bst):\n",
|
"class BstBalance(Bst):\n",
|
||||||
@@ -116,39 +114,35 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_check_balance.py\n",
|
"# %load test_check_balance.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"from nose.tools import raises\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestCheckBalance(object):\n",
|
"class TestCheckBalance(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" @raises(TypeError)\n",
|
|
||||||
" def test_check_balance_empty(self):\n",
|
" def test_check_balance_empty(self):\n",
|
||||||
" bst = BstBalance(None)\n",
|
" bst = BstBalance(None)\n",
|
||||||
" bst.check_balance()\n",
|
" bst.check_balance()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_check_balance(self):\n",
|
" def test_check_balance(self):\n",
|
||||||
" bst = BstBalance(Node(5))\n",
|
" bst = BstBalance(Node(5))\n",
|
||||||
" assert_equal(bst.check_balance(), True)\n",
|
" self.assertEqual(bst.check_balance(), True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst.insert(3)\n",
|
" bst.insert(3)\n",
|
||||||
" bst.insert(8)\n",
|
" bst.insert(8)\n",
|
||||||
" bst.insert(1)\n",
|
" bst.insert(1)\n",
|
||||||
" bst.insert(4)\n",
|
" bst.insert(4)\n",
|
||||||
" assert_equal(bst.check_balance(), True)\n",
|
" self.assertEqual(bst.check_balance(), True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst = BstBalance(Node(5))\n",
|
" bst = BstBalance(Node(5))\n",
|
||||||
" bst.insert(3)\n",
|
" bst.insert(3)\n",
|
||||||
" bst.insert(8)\n",
|
" bst.insert(8)\n",
|
||||||
" bst.insert(9)\n",
|
" bst.insert(9)\n",
|
||||||
" bst.insert(10)\n",
|
" bst.insert(10)\n",
|
||||||
" assert_equal(bst.check_balance(), False)\n",
|
" self.assertEqual(bst.check_balance(), False)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst = BstBalance(Node(3))\n",
|
" bst = BstBalance(Node(3))\n",
|
||||||
" bst.insert(2)\n",
|
" bst.insert(2)\n",
|
||||||
@@ -157,14 +151,14 @@
|
|||||||
" bst.insert(4)\n",
|
" bst.insert(4)\n",
|
||||||
" bst.insert(6)\n",
|
" bst.insert(6)\n",
|
||||||
" bst.insert(7)\n",
|
" bst.insert(7)\n",
|
||||||
" assert_equal(bst.check_balance(), True)\n",
|
" self.assertEqual(bst.check_balance(), True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_check_balance')\n",
|
" print('Success: test_check_balance')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestCheckBalance()\n",
|
" test = TestCheckBalance()\n",
|
||||||
" test.test_check_balance_empty()\n",
|
" test.assertRaises(TypeError, test.test_check_balance_empty)\n",
|
||||||
" test.test_check_balance()\n",
|
" test.test_check_balance()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -198,9 +192,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,9 +86,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../bst/bst.py"
|
"%run ../bst/bst.py"
|
||||||
@@ -97,9 +95,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class BstBalance(Bst):\n",
|
"class BstBalance(Bst):\n",
|
||||||
@@ -135,9 +131,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -149,33 +143,31 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_check_balance.py\n",
|
"%%writefile test_check_balance.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"from nose.tools import raises\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestCheckBalance(object):\n",
|
"class TestCheckBalance(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" @raises(TypeError)\n",
|
|
||||||
" def test_check_balance_empty(self):\n",
|
" def test_check_balance_empty(self):\n",
|
||||||
" bst = BstBalance(None)\n",
|
" bst = BstBalance(None)\n",
|
||||||
" bst.check_balance()\n",
|
" bst.check_balance()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_check_balance(self):\n",
|
" def test_check_balance(self):\n",
|
||||||
" bst = BstBalance(Node(5))\n",
|
" bst = BstBalance(Node(5))\n",
|
||||||
" assert_equal(bst.check_balance(), True)\n",
|
" self.assertEqual(bst.check_balance(), True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst.insert(3)\n",
|
" bst.insert(3)\n",
|
||||||
" bst.insert(8)\n",
|
" bst.insert(8)\n",
|
||||||
" bst.insert(1)\n",
|
" bst.insert(1)\n",
|
||||||
" bst.insert(4)\n",
|
" bst.insert(4)\n",
|
||||||
" assert_equal(bst.check_balance(), True)\n",
|
" self.assertEqual(bst.check_balance(), True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst = BstBalance(Node(5))\n",
|
" bst = BstBalance(Node(5))\n",
|
||||||
" bst.insert(3)\n",
|
" bst.insert(3)\n",
|
||||||
" bst.insert(8)\n",
|
" bst.insert(8)\n",
|
||||||
" bst.insert(9)\n",
|
" bst.insert(9)\n",
|
||||||
" bst.insert(10)\n",
|
" bst.insert(10)\n",
|
||||||
" assert_equal(bst.check_balance(), False)\n",
|
" self.assertEqual(bst.check_balance(), False)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst = BstBalance(Node(3))\n",
|
" bst = BstBalance(Node(3))\n",
|
||||||
" bst.insert(2)\n",
|
" bst.insert(2)\n",
|
||||||
@@ -184,14 +176,14 @@
|
|||||||
" bst.insert(4)\n",
|
" bst.insert(4)\n",
|
||||||
" bst.insert(6)\n",
|
" bst.insert(6)\n",
|
||||||
" bst.insert(7)\n",
|
" bst.insert(7)\n",
|
||||||
" assert_equal(bst.check_balance(), True)\n",
|
" self.assertEqual(bst.check_balance(), True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_check_balance')\n",
|
" print('Success: test_check_balance')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestCheckBalance()\n",
|
" test = TestCheckBalance()\n",
|
||||||
" test.test_check_balance_empty()\n",
|
" test.assertRaises(TypeError, test.test_check_balance_empty)\n",
|
||||||
" test.test_check_balance()\n",
|
" test.test_check_balance()\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -202,9 +194,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -235,9 +225,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,28 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
from nose.tools import raises
|
|
||||||
|
|
||||||
|
|
||||||
class TestCheckBalance(object):
|
class TestCheckBalance(unittest.TestCase):
|
||||||
|
|
||||||
@raises(TypeError)
|
|
||||||
def test_check_balance_empty(self):
|
def test_check_balance_empty(self):
|
||||||
bst = BstBalance(None)
|
bst = BstBalance(None)
|
||||||
bst.check_balance()
|
bst.check_balance()
|
||||||
|
|
||||||
def test_check_balance(self):
|
def test_check_balance(self):
|
||||||
bst = BstBalance(Node(5))
|
bst = BstBalance(Node(5))
|
||||||
assert_equal(bst.check_balance(), True)
|
self.assertEqual(bst.check_balance(), True)
|
||||||
|
|
||||||
bst.insert(3)
|
bst.insert(3)
|
||||||
bst.insert(8)
|
bst.insert(8)
|
||||||
bst.insert(1)
|
bst.insert(1)
|
||||||
bst.insert(4)
|
bst.insert(4)
|
||||||
assert_equal(bst.check_balance(), True)
|
self.assertEqual(bst.check_balance(), True)
|
||||||
|
|
||||||
bst = BstBalance(Node(5))
|
bst = BstBalance(Node(5))
|
||||||
bst.insert(3)
|
bst.insert(3)
|
||||||
bst.insert(8)
|
bst.insert(8)
|
||||||
bst.insert(9)
|
bst.insert(9)
|
||||||
bst.insert(10)
|
bst.insert(10)
|
||||||
assert_equal(bst.check_balance(), False)
|
self.assertEqual(bst.check_balance(), False)
|
||||||
|
|
||||||
bst = BstBalance(Node(3))
|
bst = BstBalance(Node(3))
|
||||||
bst.insert(2)
|
bst.insert(2)
|
||||||
@@ -33,16 +31,16 @@ class TestCheckBalance(object):
|
|||||||
bst.insert(4)
|
bst.insert(4)
|
||||||
bst.insert(6)
|
bst.insert(6)
|
||||||
bst.insert(7)
|
bst.insert(7)
|
||||||
assert_equal(bst.check_balance(), True)
|
self.assertEqual(bst.check_balance(), True)
|
||||||
|
|
||||||
print('Success: test_check_balance')
|
print('Success: test_check_balance')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test = TestCheckBalance()
|
test = TestCheckBalance()
|
||||||
test.test_check_balance_empty()
|
test.assertRaises(TypeError, test.test_check_balance_empty)
|
||||||
test.test_check_balance()
|
test.test_check_balance()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ from enum import Enum # Python 2 users: Run pip install enum34
|
|||||||
|
|
||||||
|
|
||||||
class State(Enum):
|
class State(Enum):
|
||||||
|
|
||||||
unvisited = 0
|
unvisited = 0
|
||||||
visiting = 1
|
visiting = 1
|
||||||
visited = 2
|
visited = 2
|
||||||
@@ -64,4 +65,4 @@ class Graph:
|
|||||||
if src_key is None or dst_key is None:
|
if src_key is None or dst_key is None:
|
||||||
raise TypeError('key cannot be None')
|
raise TypeError('key cannot be None')
|
||||||
self.add_edge(src_key, dst_key, weight)
|
self.add_edge(src_key, dst_key, weight)
|
||||||
self.add_edge(dst_key, src_key, weight)
|
self.add_edge(dst_key, src_key, weight)
|
||||||
|
|||||||
@@ -175,10 +175,10 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_graph.py\n",
|
"# %load test_graph.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestGraph(object):\n",
|
"class TestGraph(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def create_graph(self):\n",
|
" def create_graph(self):\n",
|
||||||
" graph = Graph()\n",
|
" graph = Graph()\n",
|
||||||
@@ -198,29 +198,29 @@
|
|||||||
" graph.add_edge(5, 4, weight=8)\n",
|
" graph.add_edge(5, 4, weight=8)\n",
|
||||||
" graph.add_edge(5, 2, weight=9)\n",
|
" graph.add_edge(5, 2, weight=9)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)\n",
|
" self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)\n",
|
||||||
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n",
|
" self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n",
|
||||||
" assert_equal(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n",
|
" self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n",
|
||||||
" assert_equal(graph.nodes[2].adj_weights[graph.nodes[3].key], 4)\n",
|
" self.assertEqual(graph.nodes[2].adj_weights[graph.nodes[3].key], 4)\n",
|
||||||
" assert_equal(graph.nodes[3].adj_weights[graph.nodes[4].key], 5)\n",
|
" self.assertEqual(graph.nodes[3].adj_weights[graph.nodes[4].key], 5)\n",
|
||||||
" assert_equal(graph.nodes[3].adj_weights[graph.nodes[5].key], 6)\n",
|
" self.assertEqual(graph.nodes[3].adj_weights[graph.nodes[5].key], 6)\n",
|
||||||
" assert_equal(graph.nodes[4].adj_weights[graph.nodes[0].key], 7)\n",
|
" self.assertEqual(graph.nodes[4].adj_weights[graph.nodes[0].key], 7)\n",
|
||||||
" assert_equal(graph.nodes[5].adj_weights[graph.nodes[4].key], 8)\n",
|
" self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[4].key], 8)\n",
|
||||||
" assert_equal(graph.nodes[5].adj_weights[graph.nodes[2].key], 9)\n",
|
" self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[2].key], 9)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" assert_equal(graph.nodes[0].incoming_edges, 1)\n",
|
" self.assertEqual(graph.nodes[0].incoming_edges, 1)\n",
|
||||||
" assert_equal(graph.nodes[1].incoming_edges, 1)\n",
|
" self.assertEqual(graph.nodes[1].incoming_edges, 1)\n",
|
||||||
" assert_equal(graph.nodes[2].incoming_edges, 2)\n",
|
" self.assertEqual(graph.nodes[2].incoming_edges, 2)\n",
|
||||||
" assert_equal(graph.nodes[3].incoming_edges, 1)\n",
|
" self.assertEqual(graph.nodes[3].incoming_edges, 1)\n",
|
||||||
" assert_equal(graph.nodes[4].incoming_edges, 2)\n",
|
" self.assertEqual(graph.nodes[4].incoming_edges, 2)\n",
|
||||||
" assert_equal(graph.nodes[5].incoming_edges, 2)\n",
|
" self.assertEqual(graph.nodes[5].incoming_edges, 2)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" graph.nodes[0].remove_neighbor(graph.nodes[1])\n",
|
" graph.nodes[0].remove_neighbor(graph.nodes[1])\n",
|
||||||
" assert_equal(graph.nodes[1].incoming_edges, 0)\n",
|
" self.assertEqual(graph.nodes[1].incoming_edges, 0)\n",
|
||||||
" graph.nodes[3].remove_neighbor(graph.nodes[4])\n",
|
" graph.nodes[3].remove_neighbor(graph.nodes[4])\n",
|
||||||
" assert_equal(graph.nodes[4].incoming_edges, 1)\n",
|
" self.assertEqual(graph.nodes[4].incoming_edges, 1)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" assert_equal(graph.nodes[0] < graph.nodes[1], True)\n",
|
" self.assertEqual(graph.nodes[0] < graph.nodes[1], True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_graph')\n",
|
" print('Success: test_graph')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -230,12 +230,12 @@
|
|||||||
" graph.add_undirected_edge(0, 5, weight=2)\n",
|
" graph.add_undirected_edge(0, 5, weight=2)\n",
|
||||||
" graph.add_undirected_edge(1, 2, weight=3)\n",
|
" graph.add_undirected_edge(1, 2, weight=3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)\n",
|
" self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)\n",
|
||||||
" assert_equal(graph.nodes[1].adj_weights[graph.nodes[0].key], 5)\n",
|
" self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[0].key], 5)\n",
|
||||||
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n",
|
" self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n",
|
||||||
" assert_equal(graph.nodes[5].adj_weights[graph.nodes[0].key], 2)\n",
|
" self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[0].key], 2)\n",
|
||||||
" assert_equal(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n",
|
" self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n",
|
||||||
" assert_equal(graph.nodes[2].adj_weights[graph.nodes[1].key], 3)\n",
|
" self.assertEqual(graph.nodes[2].adj_weights[graph.nodes[1].key], 3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_graph_undirected')\n",
|
" print('Success: test_graph_undirected')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -276,7 +276,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.6.4"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -154,9 +154,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -241,9 +239,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run graph.py"
|
"%run graph.py"
|
||||||
@@ -259,9 +255,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -273,10 +267,10 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_graph.py\n",
|
"%%writefile test_graph.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestGraph(object):\n",
|
"class TestGraph(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def create_graph(self):\n",
|
" def create_graph(self):\n",
|
||||||
" graph = Graph()\n",
|
" graph = Graph()\n",
|
||||||
@@ -296,29 +290,29 @@
|
|||||||
" graph.add_edge(5, 4, weight=8)\n",
|
" graph.add_edge(5, 4, weight=8)\n",
|
||||||
" graph.add_edge(5, 2, weight=9)\n",
|
" graph.add_edge(5, 2, weight=9)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)\n",
|
" self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)\n",
|
||||||
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n",
|
" self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n",
|
||||||
" assert_equal(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n",
|
" self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n",
|
||||||
" assert_equal(graph.nodes[2].adj_weights[graph.nodes[3].key], 4)\n",
|
" self.assertEqual(graph.nodes[2].adj_weights[graph.nodes[3].key], 4)\n",
|
||||||
" assert_equal(graph.nodes[3].adj_weights[graph.nodes[4].key], 5)\n",
|
" self.assertEqual(graph.nodes[3].adj_weights[graph.nodes[4].key], 5)\n",
|
||||||
" assert_equal(graph.nodes[3].adj_weights[graph.nodes[5].key], 6)\n",
|
" self.assertEqual(graph.nodes[3].adj_weights[graph.nodes[5].key], 6)\n",
|
||||||
" assert_equal(graph.nodes[4].adj_weights[graph.nodes[0].key], 7)\n",
|
" self.assertEqual(graph.nodes[4].adj_weights[graph.nodes[0].key], 7)\n",
|
||||||
" assert_equal(graph.nodes[5].adj_weights[graph.nodes[4].key], 8)\n",
|
" self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[4].key], 8)\n",
|
||||||
" assert_equal(graph.nodes[5].adj_weights[graph.nodes[2].key], 9)\n",
|
" self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[2].key], 9)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" assert_equal(graph.nodes[0].incoming_edges, 1)\n",
|
" self.assertEqual(graph.nodes[0].incoming_edges, 1)\n",
|
||||||
" assert_equal(graph.nodes[1].incoming_edges, 1)\n",
|
" self.assertEqual(graph.nodes[1].incoming_edges, 1)\n",
|
||||||
" assert_equal(graph.nodes[2].incoming_edges, 2)\n",
|
" self.assertEqual(graph.nodes[2].incoming_edges, 2)\n",
|
||||||
" assert_equal(graph.nodes[3].incoming_edges, 1)\n",
|
" self.assertEqual(graph.nodes[3].incoming_edges, 1)\n",
|
||||||
" assert_equal(graph.nodes[4].incoming_edges, 2)\n",
|
" self.assertEqual(graph.nodes[4].incoming_edges, 2)\n",
|
||||||
" assert_equal(graph.nodes[5].incoming_edges, 2)\n",
|
" self.assertEqual(graph.nodes[5].incoming_edges, 2)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" graph.nodes[0].remove_neighbor(graph.nodes[1])\n",
|
" graph.nodes[0].remove_neighbor(graph.nodes[1])\n",
|
||||||
" assert_equal(graph.nodes[1].incoming_edges, 0)\n",
|
" self.assertEqual(graph.nodes[1].incoming_edges, 0)\n",
|
||||||
" graph.nodes[3].remove_neighbor(graph.nodes[4])\n",
|
" graph.nodes[3].remove_neighbor(graph.nodes[4])\n",
|
||||||
" assert_equal(graph.nodes[4].incoming_edges, 1)\n",
|
" self.assertEqual(graph.nodes[4].incoming_edges, 1)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" assert_equal(graph.nodes[0] < graph.nodes[1], True)\n",
|
" self.assertEqual(graph.nodes[0] < graph.nodes[1], True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_graph')\n",
|
" print('Success: test_graph')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -328,12 +322,12 @@
|
|||||||
" graph.add_undirected_edge(0, 5, weight=2)\n",
|
" graph.add_undirected_edge(0, 5, weight=2)\n",
|
||||||
" graph.add_undirected_edge(1, 2, weight=3)\n",
|
" graph.add_undirected_edge(1, 2, weight=3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)\n",
|
" self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)\n",
|
||||||
" assert_equal(graph.nodes[1].adj_weights[graph.nodes[0].key], 5)\n",
|
" self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[0].key], 5)\n",
|
||||||
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n",
|
" self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n",
|
||||||
" assert_equal(graph.nodes[5].adj_weights[graph.nodes[0].key], 2)\n",
|
" self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[0].key], 2)\n",
|
||||||
" assert_equal(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n",
|
" self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n",
|
||||||
" assert_equal(graph.nodes[2].adj_weights[graph.nodes[1].key], 3)\n",
|
" self.assertEqual(graph.nodes[2].adj_weights[graph.nodes[1].key], 3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_graph_undirected')\n",
|
" print('Success: test_graph_undirected')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -351,9 +345,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -385,9 +377,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestGraph(object):
|
class TestGraph(unittest.TestCase):
|
||||||
|
|
||||||
def create_graph(self):
|
def create_graph(self):
|
||||||
graph = Graph()
|
graph = Graph()
|
||||||
@@ -21,29 +21,29 @@ class TestGraph(object):
|
|||||||
graph.add_edge(5, 4, weight=8)
|
graph.add_edge(5, 4, weight=8)
|
||||||
graph.add_edge(5, 2, weight=9)
|
graph.add_edge(5, 2, weight=9)
|
||||||
|
|
||||||
assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)
|
self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)
|
||||||
assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)
|
self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)
|
||||||
assert_equal(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)
|
self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)
|
||||||
assert_equal(graph.nodes[2].adj_weights[graph.nodes[3].key], 4)
|
self.assertEqual(graph.nodes[2].adj_weights[graph.nodes[3].key], 4)
|
||||||
assert_equal(graph.nodes[3].adj_weights[graph.nodes[4].key], 5)
|
self.assertEqual(graph.nodes[3].adj_weights[graph.nodes[4].key], 5)
|
||||||
assert_equal(graph.nodes[3].adj_weights[graph.nodes[5].key], 6)
|
self.assertEqual(graph.nodes[3].adj_weights[graph.nodes[5].key], 6)
|
||||||
assert_equal(graph.nodes[4].adj_weights[graph.nodes[0].key], 7)
|
self.assertEqual(graph.nodes[4].adj_weights[graph.nodes[0].key], 7)
|
||||||
assert_equal(graph.nodes[5].adj_weights[graph.nodes[4].key], 8)
|
self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[4].key], 8)
|
||||||
assert_equal(graph.nodes[5].adj_weights[graph.nodes[2].key], 9)
|
self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[2].key], 9)
|
||||||
|
|
||||||
assert_equal(graph.nodes[0].incoming_edges, 1)
|
self.assertEqual(graph.nodes[0].incoming_edges, 1)
|
||||||
assert_equal(graph.nodes[1].incoming_edges, 1)
|
self.assertEqual(graph.nodes[1].incoming_edges, 1)
|
||||||
assert_equal(graph.nodes[2].incoming_edges, 2)
|
self.assertEqual(graph.nodes[2].incoming_edges, 2)
|
||||||
assert_equal(graph.nodes[3].incoming_edges, 1)
|
self.assertEqual(graph.nodes[3].incoming_edges, 1)
|
||||||
assert_equal(graph.nodes[4].incoming_edges, 2)
|
self.assertEqual(graph.nodes[4].incoming_edges, 2)
|
||||||
assert_equal(graph.nodes[5].incoming_edges, 2)
|
self.assertEqual(graph.nodes[5].incoming_edges, 2)
|
||||||
|
|
||||||
graph.nodes[0].remove_neighbor(graph.nodes[1])
|
graph.nodes[0].remove_neighbor(graph.nodes[1])
|
||||||
assert_equal(graph.nodes[1].incoming_edges, 0)
|
self.assertEqual(graph.nodes[1].incoming_edges, 0)
|
||||||
graph.nodes[3].remove_neighbor(graph.nodes[4])
|
graph.nodes[3].remove_neighbor(graph.nodes[4])
|
||||||
assert_equal(graph.nodes[4].incoming_edges, 1)
|
self.assertEqual(graph.nodes[4].incoming_edges, 1)
|
||||||
|
|
||||||
assert_equal(graph.nodes[0] < graph.nodes[1], True)
|
self.assertEqual(graph.nodes[0] < graph.nodes[1], True)
|
||||||
|
|
||||||
print('Success: test_graph')
|
print('Success: test_graph')
|
||||||
|
|
||||||
@@ -53,12 +53,12 @@ class TestGraph(object):
|
|||||||
graph.add_undirected_edge(0, 5, weight=2)
|
graph.add_undirected_edge(0, 5, weight=2)
|
||||||
graph.add_undirected_edge(1, 2, weight=3)
|
graph.add_undirected_edge(1, 2, weight=3)
|
||||||
|
|
||||||
assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)
|
self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)
|
||||||
assert_equal(graph.nodes[1].adj_weights[graph.nodes[0].key], 5)
|
self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[0].key], 5)
|
||||||
assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)
|
self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)
|
||||||
assert_equal(graph.nodes[5].adj_weights[graph.nodes[0].key], 2)
|
self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[0].key], 2)
|
||||||
assert_equal(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)
|
self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)
|
||||||
assert_equal(graph.nodes[2].adj_weights[graph.nodes[1].key], 3)
|
self.assertEqual(graph.nodes[2].adj_weights[graph.nodes[1].key], 3)
|
||||||
|
|
||||||
print('Success: test_graph_undirected')
|
print('Success: test_graph_undirected')
|
||||||
|
|
||||||
@@ -70,4 +70,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -142,12 +142,13 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_bfs.py\n",
|
"# %load test_bfs.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestBfs(object):\n",
|
"class TestBfs(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self, *args, **kwargs):\n",
|
||||||
|
" super(TestBfs, self).__init__()\n",
|
||||||
" self.results = Results()\n",
|
" self.results = Results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bfs(self):\n",
|
" def test_bfs(self):\n",
|
||||||
@@ -164,7 +165,7 @@
|
|||||||
" graph.add_edge(3, 2, 7)\n",
|
" graph.add_edge(3, 2, 7)\n",
|
||||||
" graph.add_edge(3, 4, 8)\n",
|
" graph.add_edge(3, 4, 8)\n",
|
||||||
" graph.bfs(nodes[0], self.results.add_result)\n",
|
" graph.bfs(nodes[0], self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[0, 1, 4, 5, 3, 2]\")\n",
|
" self.assertEqual(str(self.results), \"[0, 1, 4, 5, 3, 2]\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bfs')\n",
|
" print('Success: test_bfs')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -204,7 +205,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.6.4"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -103,9 +103,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../graph/graph.py"
|
"%run ../graph/graph.py"
|
||||||
@@ -114,9 +112,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from collections import deque\n",
|
"from collections import deque\n",
|
||||||
@@ -149,9 +145,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../utils/results.py"
|
"%run ../utils/results.py"
|
||||||
@@ -160,9 +154,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -174,12 +166,13 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_bfs.py\n",
|
"%%writefile test_bfs.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestBfs(object):\n",
|
"class TestBfs(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self, *args, **kwargs):\n",
|
||||||
|
" super(TestBfs, self).__init__()\n",
|
||||||
" self.results = Results()\n",
|
" self.results = Results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bfs(self):\n",
|
" def test_bfs(self):\n",
|
||||||
@@ -196,7 +189,7 @@
|
|||||||
" graph.add_edge(3, 2, 7)\n",
|
" graph.add_edge(3, 2, 7)\n",
|
||||||
" graph.add_edge(3, 4, 8)\n",
|
" graph.add_edge(3, 4, 8)\n",
|
||||||
" graph.bfs(nodes[0], self.results.add_result)\n",
|
" graph.bfs(nodes[0], self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[0, 1, 4, 5, 3, 2]\")\n",
|
" self.assertEqual(str(self.results), \"[0, 1, 4, 5, 3, 2]\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bfs')\n",
|
" print('Success: test_bfs')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -213,9 +206,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -246,9 +237,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestBfs(object):
|
class TestBfs(unittest.TestCase):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(TestBfs, self).__init__()
|
||||||
self.results = Results()
|
self.results = Results()
|
||||||
|
|
||||||
def test_bfs(self):
|
def test_bfs(self):
|
||||||
@@ -20,7 +21,7 @@ class TestBfs(object):
|
|||||||
graph.add_edge(3, 2, 7)
|
graph.add_edge(3, 2, 7)
|
||||||
graph.add_edge(3, 4, 8)
|
graph.add_edge(3, 4, 8)
|
||||||
graph.bfs(nodes[0], self.results.add_result)
|
graph.bfs(nodes[0], self.results.add_result)
|
||||||
assert_equal(str(self.results), "[0, 1, 4, 5, 3, 2]")
|
self.assertEqual(str(self.results), "[0, 1, 4, 5, 3, 2]")
|
||||||
|
|
||||||
print('Success: test_bfs')
|
print('Success: test_bfs')
|
||||||
|
|
||||||
@@ -31,4 +32,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -151,13 +151,13 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_build_order.py\n",
|
"# %load test_build_order.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"from nose.tools import assert_true\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestBuildOrder(object):\n",
|
"class TestBuildOrder(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self, *args, **kwargs):\n",
|
||||||
|
" super(TestBuildOrder, self).__init__()\n",
|
||||||
" self.dependencies = [\n",
|
" self.dependencies = [\n",
|
||||||
" Dependency('d', 'g'),\n",
|
" Dependency('d', 'g'),\n",
|
||||||
" Dependency('f', 'c'),\n",
|
" Dependency('f', 'c'),\n",
|
||||||
@@ -175,13 +175,13 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" expected_result0 = ('d', 'f')\n",
|
" expected_result0 = ('d', 'f')\n",
|
||||||
" expected_result1 = ('c', 'b', 'g')\n",
|
" expected_result1 = ('c', 'b', 'g')\n",
|
||||||
" assert_true(processed_nodes[0].key in expected_result0)\n",
|
" self.assertTrue(processed_nodes[0].key in expected_result0)\n",
|
||||||
" assert_true(processed_nodes[1].key in expected_result0)\n",
|
" self.assertTrue(processed_nodes[1].key in expected_result0)\n",
|
||||||
" assert_true(processed_nodes[2].key in expected_result1)\n",
|
" self.assertTrue(processed_nodes[2].key in expected_result1)\n",
|
||||||
" assert_true(processed_nodes[3].key in expected_result1)\n",
|
" self.assertTrue(processed_nodes[3].key in expected_result1)\n",
|
||||||
" assert_true(processed_nodes[4].key in expected_result1)\n",
|
" self.assertTrue(processed_nodes[4].key in expected_result1)\n",
|
||||||
" assert_true(processed_nodes[5].key is 'a')\n",
|
" self.assertTrue(processed_nodes[5].key is 'a')\n",
|
||||||
" assert_true(processed_nodes[6].key is 'e')\n",
|
" self.assertTrue(processed_nodes[6].key is 'e')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_build_order')\n",
|
" print('Success: test_build_order')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -189,7 +189,7 @@
|
|||||||
" self.dependencies.append(Dependency('e', 'f'))\n",
|
" self.dependencies.append(Dependency('e', 'f'))\n",
|
||||||
" build_order = BuildOrder(self.dependencies)\n",
|
" build_order = BuildOrder(self.dependencies)\n",
|
||||||
" processed_nodes = build_order.find_build_order()\n",
|
" processed_nodes = build_order.find_build_order()\n",
|
||||||
" assert_true(processed_nodes is None)\n",
|
" self.assertTrue(processed_nodes is None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_build_order_circular')\n",
|
" print('Success: test_build_order_circular')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -230,7 +230,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.6.4"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -101,9 +101,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from collections import deque\n",
|
"from collections import deque\n",
|
||||||
@@ -119,9 +117,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../graph/graph.py"
|
"%run ../graph/graph.py"
|
||||||
@@ -130,9 +126,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class BuildOrder(object):\n",
|
"class BuildOrder(object):\n",
|
||||||
@@ -187,9 +181,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../utils/results.py"
|
"%run ../utils/results.py"
|
||||||
@@ -198,9 +190,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -212,13 +202,13 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_build_order.py\n",
|
"%%writefile test_build_order.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"from nose.tools import assert_true\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestBuildOrder(object):\n",
|
"class TestBuildOrder(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self, *args, **kwargs):\n",
|
||||||
|
" super(TestBuildOrder, self).__init__()\n",
|
||||||
" self.dependencies = [\n",
|
" self.dependencies = [\n",
|
||||||
" Dependency('d', 'g'),\n",
|
" Dependency('d', 'g'),\n",
|
||||||
" Dependency('f', 'c'),\n",
|
" Dependency('f', 'c'),\n",
|
||||||
@@ -236,13 +226,13 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" expected_result0 = ('d', 'f')\n",
|
" expected_result0 = ('d', 'f')\n",
|
||||||
" expected_result1 = ('c', 'b', 'g')\n",
|
" expected_result1 = ('c', 'b', 'g')\n",
|
||||||
" assert_true(processed_nodes[0].key in expected_result0)\n",
|
" self.assertTrue(processed_nodes[0].key in expected_result0)\n",
|
||||||
" assert_true(processed_nodes[1].key in expected_result0)\n",
|
" self.assertTrue(processed_nodes[1].key in expected_result0)\n",
|
||||||
" assert_true(processed_nodes[2].key in expected_result1)\n",
|
" self.assertTrue(processed_nodes[2].key in expected_result1)\n",
|
||||||
" assert_true(processed_nodes[3].key in expected_result1)\n",
|
" self.assertTrue(processed_nodes[3].key in expected_result1)\n",
|
||||||
" assert_true(processed_nodes[4].key in expected_result1)\n",
|
" self.assertTrue(processed_nodes[4].key in expected_result1)\n",
|
||||||
" assert_true(processed_nodes[5].key is 'a')\n",
|
" self.assertTrue(processed_nodes[5].key is 'a')\n",
|
||||||
" assert_true(processed_nodes[6].key is 'e')\n",
|
" self.assertTrue(processed_nodes[6].key is 'e')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_build_order')\n",
|
" print('Success: test_build_order')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -250,7 +240,7 @@
|
|||||||
" self.dependencies.append(Dependency('e', 'f'))\n",
|
" self.dependencies.append(Dependency('e', 'f'))\n",
|
||||||
" build_order = BuildOrder(self.dependencies)\n",
|
" build_order = BuildOrder(self.dependencies)\n",
|
||||||
" processed_nodes = build_order.find_build_order()\n",
|
" processed_nodes = build_order.find_build_order()\n",
|
||||||
" assert_true(processed_nodes is None)\n",
|
" self.assertTrue(processed_nodes is None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_build_order_circular')\n",
|
" print('Success: test_build_order_circular')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -268,9 +258,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": 6,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -302,9 +290,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
from nose.tools import assert_true
|
|
||||||
|
|
||||||
|
|
||||||
class TestBuildOrder(object):
|
class TestBuildOrder(unittest.TestCase):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(TestBuildOrder, self).__init__()
|
||||||
self.dependencies = [
|
self.dependencies = [
|
||||||
Dependency('d', 'g'),
|
Dependency('d', 'g'),
|
||||||
Dependency('f', 'c'),
|
Dependency('f', 'c'),
|
||||||
@@ -22,13 +22,13 @@ class TestBuildOrder(object):
|
|||||||
|
|
||||||
expected_result0 = ('d', 'f')
|
expected_result0 = ('d', 'f')
|
||||||
expected_result1 = ('c', 'b', 'g')
|
expected_result1 = ('c', 'b', 'g')
|
||||||
assert_true(processed_nodes[0].key in expected_result0)
|
self.assertTrue(processed_nodes[0].key in expected_result0)
|
||||||
assert_true(processed_nodes[1].key in expected_result0)
|
self.assertTrue(processed_nodes[1].key in expected_result0)
|
||||||
assert_true(processed_nodes[2].key in expected_result1)
|
self.assertTrue(processed_nodes[2].key in expected_result1)
|
||||||
assert_true(processed_nodes[3].key in expected_result1)
|
self.assertTrue(processed_nodes[3].key in expected_result1)
|
||||||
assert_true(processed_nodes[4].key in expected_result1)
|
self.assertTrue(processed_nodes[4].key in expected_result1)
|
||||||
assert_true(processed_nodes[5].key is 'a')
|
self.assertTrue(processed_nodes[5].key is 'a')
|
||||||
assert_true(processed_nodes[6].key is 'e')
|
self.assertTrue(processed_nodes[6].key is 'e')
|
||||||
|
|
||||||
print('Success: test_build_order')
|
print('Success: test_build_order')
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ class TestBuildOrder(object):
|
|||||||
self.dependencies.append(Dependency('e', 'f'))
|
self.dependencies.append(Dependency('e', 'f'))
|
||||||
build_order = BuildOrder(self.dependencies)
|
build_order = BuildOrder(self.dependencies)
|
||||||
processed_nodes = build_order.find_build_order()
|
processed_nodes = build_order.find_build_order()
|
||||||
assert_true(processed_nodes is None)
|
self.assertTrue(processed_nodes is None)
|
||||||
|
|
||||||
print('Success: test_build_order_circular')
|
print('Success: test_build_order_circular')
|
||||||
|
|
||||||
@@ -48,4 +48,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -101,9 +101,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class GraphDfs(Graph):\n",
|
"class GraphDfs(Graph):\n",
|
||||||
@@ -141,18 +139,17 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_dfs.py\n",
|
"# %load test_dfs.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestDfs(object):\n",
|
"class TestDfs(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self, *args, **kwargs):\n",
|
||||||
|
" super(TestDfs, self).__init__()\n",
|
||||||
" self.results = Results()\n",
|
" self.results = Results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_dfs(self):\n",
|
" def test_dfs(self):\n",
|
||||||
@@ -169,7 +166,7 @@
|
|||||||
" graph.add_edge(3, 2, 7)\n",
|
" graph.add_edge(3, 2, 7)\n",
|
||||||
" graph.add_edge(3, 4, 8)\n",
|
" graph.add_edge(3, 4, 8)\n",
|
||||||
" graph.dfs(nodes[0], self.results.add_result)\n",
|
" graph.dfs(nodes[0], self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[0, 1, 3, 2, 4, 5]\")\n",
|
" self.assertEqual(str(self.results), \"[0, 1, 3, 2, 4, 5]\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_dfs')\n",
|
" print('Success: test_dfs')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -209,9 +206,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,9 +96,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../graph/graph.py"
|
"%run ../graph/graph.py"
|
||||||
@@ -107,9 +105,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class GraphDfs(Graph):\n",
|
"class GraphDfs(Graph):\n",
|
||||||
@@ -134,9 +130,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../utils/results.py"
|
"%run ../utils/results.py"
|
||||||
@@ -145,9 +139,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -159,12 +151,13 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_dfs.py\n",
|
"%%writefile test_dfs.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestDfs(object):\n",
|
"class TestDfs(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self, *args, **kwargs):\n",
|
||||||
|
" super(TestDfs, self).__init__()\n",
|
||||||
" self.results = Results()\n",
|
" self.results = Results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_dfs(self):\n",
|
" def test_dfs(self):\n",
|
||||||
@@ -181,7 +174,7 @@
|
|||||||
" graph.add_edge(3, 2, 7)\n",
|
" graph.add_edge(3, 2, 7)\n",
|
||||||
" graph.add_edge(3, 4, 8)\n",
|
" graph.add_edge(3, 4, 8)\n",
|
||||||
" graph.dfs(nodes[0], self.results.add_result)\n",
|
" graph.dfs(nodes[0], self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[0, 1, 3, 2, 4, 5]\")\n",
|
" self.assertEqual(str(self.results), \"[0, 1, 3, 2, 4, 5]\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_dfs')\n",
|
" print('Success: test_dfs')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -198,9 +191,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -231,9 +222,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestDfs(object):
|
class TestDfs(unittest.TestCase):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(TestDfs, self).__init__()
|
||||||
self.results = Results()
|
self.results = Results()
|
||||||
|
|
||||||
def test_dfs(self):
|
def test_dfs(self):
|
||||||
@@ -20,7 +21,7 @@ class TestDfs(object):
|
|||||||
graph.add_edge(3, 2, 7)
|
graph.add_edge(3, 2, 7)
|
||||||
graph.add_edge(3, 4, 8)
|
graph.add_edge(3, 4, 8)
|
||||||
graph.dfs(nodes[0], self.results.add_result)
|
graph.dfs(nodes[0], self.results.add_result)
|
||||||
assert_equal(str(self.results), "[0, 1, 3, 2, 4, 5]")
|
self.assertEqual(str(self.results), "[0, 1, 3, 2, 4, 5]")
|
||||||
|
|
||||||
print('Success: test_dfs')
|
print('Success: test_dfs')
|
||||||
|
|
||||||
@@ -31,4 +32,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -103,9 +103,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class GraphPathExists(Graph):\n",
|
"class GraphPathExists(Graph):\n",
|
||||||
@@ -132,16 +130,14 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_path_exists.py\n",
|
"# %load test_path_exists.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestPathExists(object):\n",
|
"class TestPathExists(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_path_exists(self):\n",
|
" def test_path_exists(self):\n",
|
||||||
" nodes = []\n",
|
" nodes = []\n",
|
||||||
@@ -157,9 +153,9 @@
|
|||||||
" graph.add_edge(3, 2, 7)\n",
|
" graph.add_edge(3, 2, 7)\n",
|
||||||
" graph.add_edge(3, 4, 8)\n",
|
" graph.add_edge(3, 4, 8)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" assert_equal(graph.path_exists(nodes[0], nodes[2]), True)\n",
|
" self.assertEqual(graph.path_exists(nodes[0], nodes[2]), True)\n",
|
||||||
" assert_equal(graph.path_exists(nodes[0], nodes[0]), True)\n",
|
" self.assertEqual(graph.path_exists(nodes[0], nodes[0]), True)\n",
|
||||||
" assert_equal(graph.path_exists(nodes[4], nodes[5]), False)\n",
|
" self.assertEqual(graph.path_exists(nodes[4], nodes[5]), False)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_path_exists')\n",
|
" print('Success: test_path_exists')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -199,9 +195,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,9 +107,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../graph/graph.py"
|
"%run ../graph/graph.py"
|
||||||
@@ -118,9 +116,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from collections import deque\n",
|
"from collections import deque\n",
|
||||||
@@ -157,9 +153,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -171,10 +165,10 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_path_exists.py\n",
|
"%%writefile test_path_exists.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestPathExists(object):\n",
|
"class TestPathExists(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_path_exists(self):\n",
|
" def test_path_exists(self):\n",
|
||||||
" nodes = []\n",
|
" nodes = []\n",
|
||||||
@@ -190,9 +184,9 @@
|
|||||||
" graph.add_edge(3, 2, 7)\n",
|
" graph.add_edge(3, 2, 7)\n",
|
||||||
" graph.add_edge(3, 4, 8)\n",
|
" graph.add_edge(3, 4, 8)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" assert_equal(graph.path_exists(nodes[0], nodes[2]), True)\n",
|
" self.assertEqual(graph.path_exists(nodes[0], nodes[2]), True)\n",
|
||||||
" assert_equal(graph.path_exists(nodes[0], nodes[0]), True)\n",
|
" self.assertEqual(graph.path_exists(nodes[0], nodes[0]), True)\n",
|
||||||
" assert_equal(graph.path_exists(nodes[4], nodes[5]), False)\n",
|
" self.assertEqual(graph.path_exists(nodes[4], nodes[5]), False)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_path_exists')\n",
|
" print('Success: test_path_exists')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -209,9 +203,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -242,9 +234,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestPathExists(object):
|
class TestPathExists(unittest.TestCase):
|
||||||
|
|
||||||
def test_path_exists(self):
|
def test_path_exists(self):
|
||||||
nodes = []
|
nodes = []
|
||||||
@@ -17,9 +17,9 @@ class TestPathExists(object):
|
|||||||
graph.add_edge(3, 2, 7)
|
graph.add_edge(3, 2, 7)
|
||||||
graph.add_edge(3, 4, 8)
|
graph.add_edge(3, 4, 8)
|
||||||
|
|
||||||
assert_equal(graph.path_exists(nodes[0], nodes[2]), True)
|
self.assertEqual(graph.path_exists(nodes[0], nodes[2]), True)
|
||||||
assert_equal(graph.path_exists(nodes[0], nodes[0]), True)
|
self.assertEqual(graph.path_exists(nodes[0], nodes[0]), True)
|
||||||
assert_equal(graph.path_exists(nodes[4], nodes[5]), False)
|
self.assertEqual(graph.path_exists(nodes[4], nodes[5]), False)
|
||||||
|
|
||||||
print('Success: test_path_exists')
|
print('Success: test_path_exists')
|
||||||
|
|
||||||
@@ -30,4 +30,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -91,8 +91,8 @@
|
|||||||
"graph.add_edge('h', 'g', weight=2)\n",
|
"graph.add_edge('h', 'g', weight=2)\n",
|
||||||
"shortest_path = ShortestPath(graph)\n",
|
"shortest_path = ShortestPath(graph)\n",
|
||||||
"result = shortest_path.find_shortest_path('a', 'i')\n",
|
"result = shortest_path.find_shortest_path('a', 'i')\n",
|
||||||
"assert_equal(result, ['a', 'c', 'd', 'g', 'i'])\n",
|
"self.assertEqual(result, ['a', 'c', 'd', 'g', 'i'])\n",
|
||||||
"assert_equal(shortest_path.path_weight['i'], 8)\n",
|
"self.assertEqual(shortest_path.path_weight['i'], 8)\n",
|
||||||
"</pre>"
|
"</pre>"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -174,10 +174,10 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_shortest_path.py\n",
|
"# %load test_shortest_path.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestShortestPath(object):\n",
|
"class TestShortestPath(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_shortest_path(self):\n",
|
" def test_shortest_path(self):\n",
|
||||||
" graph = Graph()\n",
|
" graph = Graph()\n",
|
||||||
@@ -202,8 +202,8 @@
|
|||||||
" graph.add_edge('h', 'g', weight=2)\n",
|
" graph.add_edge('h', 'g', weight=2)\n",
|
||||||
" shortest_path = ShortestPath(graph)\n",
|
" shortest_path = ShortestPath(graph)\n",
|
||||||
" result = shortest_path.find_shortest_path('a', 'i')\n",
|
" result = shortest_path.find_shortest_path('a', 'i')\n",
|
||||||
" assert_equal(result, ['a', 'c', 'd', 'g', 'i'])\n",
|
" self.assertEqual(result, ['a', 'c', 'd', 'g', 'i'])\n",
|
||||||
" assert_equal(shortest_path.path_weight['i'], 8)\n",
|
" self.assertEqual(shortest_path.path_weight['i'], 8)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_shortest_path')\n",
|
" print('Success: test_shortest_path')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -243,7 +243,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.6.4"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -90,8 +90,8 @@
|
|||||||
"graph.add_edge('h', 'g', weight=2)\n",
|
"graph.add_edge('h', 'g', weight=2)\n",
|
||||||
"shortest_path = ShortestPath(graph)\n",
|
"shortest_path = ShortestPath(graph)\n",
|
||||||
"result = shortest_path.find_shortest_path('a', 'i')\n",
|
"result = shortest_path.find_shortest_path('a', 'i')\n",
|
||||||
"assert_equal(result, ['a', 'c', 'd', 'g', 'i'])\n",
|
"self.assertEqual(result, ['a', 'c', 'd', 'g', 'i'])\n",
|
||||||
"assert_equal(shortest_path.path_weight['i'], 8)\n",
|
"self.assertEqual(shortest_path.path_weight['i'], 8)\n",
|
||||||
"</pre>"
|
"</pre>"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -154,9 +154,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../../arrays_strings/priority_queue/priority_queue.py"
|
"%run ../../arrays_strings/priority_queue/priority_queue.py"
|
||||||
@@ -165,9 +163,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../graph/graph.py"
|
"%run ../graph/graph.py"
|
||||||
@@ -176,9 +172,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import sys\n",
|
"import sys\n",
|
||||||
@@ -254,9 +248,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -268,10 +260,10 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_shortest_path.py\n",
|
"%%writefile test_shortest_path.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestShortestPath(object):\n",
|
"class TestShortestPath(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_shortest_path(self):\n",
|
" def test_shortest_path(self):\n",
|
||||||
" graph = Graph()\n",
|
" graph = Graph()\n",
|
||||||
@@ -296,8 +288,8 @@
|
|||||||
" graph.add_edge('h', 'g', weight=2)\n",
|
" graph.add_edge('h', 'g', weight=2)\n",
|
||||||
" shortest_path = ShortestPath(graph)\n",
|
" shortest_path = ShortestPath(graph)\n",
|
||||||
" result = shortest_path.find_shortest_path('a', 'i')\n",
|
" result = shortest_path.find_shortest_path('a', 'i')\n",
|
||||||
" assert_equal(result, ['a', 'c', 'd', 'g', 'i'])\n",
|
" self.assertEqual(result, ['a', 'c', 'd', 'g', 'i'])\n",
|
||||||
" assert_equal(shortest_path.path_weight['i'], 8)\n",
|
" self.assertEqual(shortest_path.path_weight['i'], 8)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_shortest_path')\n",
|
" print('Success: test_shortest_path')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -314,9 +306,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -347,9 +337,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestShortestPath(object):
|
class TestShortestPath(unittest.TestCase):
|
||||||
|
|
||||||
def test_shortest_path(self):
|
def test_shortest_path(self):
|
||||||
graph = Graph()
|
graph = Graph()
|
||||||
@@ -26,8 +26,8 @@ class TestShortestPath(object):
|
|||||||
graph.add_edge('h', 'g', weight=2)
|
graph.add_edge('h', 'g', weight=2)
|
||||||
shortest_path = ShortestPath(graph)
|
shortest_path = ShortestPath(graph)
|
||||||
result = shortest_path.find_shortest_path('a', 'i')
|
result = shortest_path.find_shortest_path('a', 'i')
|
||||||
assert_equal(result, ['a', 'c', 'd', 'g', 'i'])
|
self.assertEqual(result, ['a', 'c', 'd', 'g', 'i'])
|
||||||
assert_equal(shortest_path.path_weight['i'], 8)
|
self.assertEqual(shortest_path.path_weight['i'], 8)
|
||||||
|
|
||||||
print('Success: test_shortest_path')
|
print('Success: test_shortest_path')
|
||||||
|
|
||||||
@@ -38,4 +38,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -111,9 +111,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class GraphShortestPath(Graph):\n",
|
"class GraphShortestPath(Graph):\n",
|
||||||
@@ -140,16 +138,14 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_shortest_path.py\n",
|
"# %load test_shortest_path.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestShortestPath(object):\n",
|
"class TestShortestPath(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_shortest_path(self):\n",
|
" def test_shortest_path(self):\n",
|
||||||
" nodes = []\n",
|
" nodes = []\n",
|
||||||
@@ -165,9 +161,9 @@
|
|||||||
" graph.add_edge(3, 2)\n",
|
" graph.add_edge(3, 2)\n",
|
||||||
" graph.add_edge(3, 4)\n",
|
" graph.add_edge(3, 4)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" assert_equal(graph.shortest_path(nodes[0].key, nodes[2].key), [0, 1, 3, 2])\n",
|
" self.assertEqual(graph.shortest_path(nodes[0].key, nodes[2].key), [0, 1, 3, 2])\n",
|
||||||
" assert_equal(graph.shortest_path(nodes[0].key, nodes[0].key), [0])\n",
|
" self.assertEqual(graph.shortest_path(nodes[0].key, nodes[0].key), [0])\n",
|
||||||
" assert_equal(graph.shortest_path(nodes[4].key, nodes[5].key), None)\n",
|
" self.assertEqual(graph.shortest_path(nodes[4].key, nodes[5].key), None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_shortest_path')\n",
|
" print('Success: test_shortest_path')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -207,9 +203,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,9 +116,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../graph/graph.py"
|
"%run ../graph/graph.py"
|
||||||
@@ -127,9 +125,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from collections import deque\n",
|
"from collections import deque\n",
|
||||||
@@ -181,9 +177,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -195,10 +189,10 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_shortest_path.py\n",
|
"%%writefile test_shortest_path.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestShortestPath(object):\n",
|
"class TestShortestPath(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_shortest_path(self):\n",
|
" def test_shortest_path(self):\n",
|
||||||
" nodes = []\n",
|
" nodes = []\n",
|
||||||
@@ -214,9 +208,9 @@
|
|||||||
" graph.add_edge(3, 2)\n",
|
" graph.add_edge(3, 2)\n",
|
||||||
" graph.add_edge(3, 4)\n",
|
" graph.add_edge(3, 4)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" assert_equal(graph.shortest_path(nodes[0].key, nodes[2].key), [0, 1, 3, 2])\n",
|
" self.assertEqual(graph.shortest_path(nodes[0].key, nodes[2].key), [0, 1, 3, 2])\n",
|
||||||
" assert_equal(graph.shortest_path(nodes[0].key, nodes[0].key), [0])\n",
|
" self.assertEqual(graph.shortest_path(nodes[0].key, nodes[0].key), [0])\n",
|
||||||
" assert_equal(graph.shortest_path(nodes[4].key, nodes[5].key), None)\n",
|
" self.assertEqual(graph.shortest_path(nodes[4].key, nodes[5].key), None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_shortest_path')\n",
|
" print('Success: test_shortest_path')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -233,9 +227,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -266,9 +258,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestShortestPath(object):
|
class TestShortestPath(unittest.TestCase):
|
||||||
|
|
||||||
def test_shortest_path(self):
|
def test_shortest_path(self):
|
||||||
nodes = []
|
nodes = []
|
||||||
@@ -17,9 +17,9 @@ class TestShortestPath(object):
|
|||||||
graph.add_edge(3, 2)
|
graph.add_edge(3, 2)
|
||||||
graph.add_edge(3, 4)
|
graph.add_edge(3, 4)
|
||||||
|
|
||||||
assert_equal(graph.shortest_path(nodes[0].key, nodes[2].key), [0, 1, 3, 2])
|
self.assertEqual(graph.shortest_path(nodes[0].key, nodes[2].key), [0, 1, 3, 2])
|
||||||
assert_equal(graph.shortest_path(nodes[0].key, nodes[0].key), [0])
|
self.assertEqual(graph.shortest_path(nodes[0].key, nodes[0].key), [0])
|
||||||
assert_equal(graph.shortest_path(nodes[4].key, nodes[5].key), None)
|
self.assertEqual(graph.shortest_path(nodes[4].key, nodes[5].key), None)
|
||||||
|
|
||||||
print('Success: test_shortest_path')
|
print('Success: test_shortest_path')
|
||||||
|
|
||||||
@@ -30,4 +30,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -128,10 +128,10 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_invert_tree.py\n",
|
"# %load test_invert_tree.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestInvertTree(object):\n",
|
"class TestInvertTree(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_invert_tree(self):\n",
|
" def test_invert_tree(self):\n",
|
||||||
" root = Node(5)\n",
|
" root = Node(5)\n",
|
||||||
@@ -143,13 +143,13 @@
|
|||||||
" node6 = bst.insert(6)\n",
|
" node6 = bst.insert(6)\n",
|
||||||
" node9 = bst.insert(9)\n",
|
" node9 = bst.insert(9)\n",
|
||||||
" result = bst.invert_tree()\n",
|
" result = bst.invert_tree()\n",
|
||||||
" assert_equal(result, root)\n",
|
" self.assertEqual(result, root)\n",
|
||||||
" assert_equal(result.left, node7)\n",
|
" self.assertEqual(result.left, node7)\n",
|
||||||
" assert_equal(result.right, node2)\n",
|
" self.assertEqual(result.right, node2)\n",
|
||||||
" assert_equal(result.left.left, node9)\n",
|
" self.assertEqual(result.left.left, node9)\n",
|
||||||
" assert_equal(result.left.right, node6)\n",
|
" self.assertEqual(result.left.right, node6)\n",
|
||||||
" assert_equal(result.right.left, node3)\n",
|
" self.assertEqual(result.right.left, node3)\n",
|
||||||
" assert_equal(result.right.right, node1)\n",
|
" self.assertEqual(result.right.right, node1)\n",
|
||||||
" print('Success: test_invert_tree')\n",
|
" print('Success: test_invert_tree')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -188,7 +188,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.6.4"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -95,9 +95,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../bst/bst.py"
|
"%run ../bst/bst.py"
|
||||||
@@ -106,9 +104,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class InverseBst(Bst):\n",
|
"class InverseBst(Bst):\n",
|
||||||
@@ -137,9 +133,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -151,10 +145,10 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_invert_tree.py\n",
|
"%%writefile test_invert_tree.py\n",
|
||||||
"from nose.tools import assert_equal, assert_raises\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestInvertTree(object):\n",
|
"class TestInvertTree(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_invert_tree(self):\n",
|
" def test_invert_tree(self):\n",
|
||||||
" root = Node(5)\n",
|
" root = Node(5)\n",
|
||||||
@@ -166,13 +160,13 @@
|
|||||||
" node6 = bst.insert(6)\n",
|
" node6 = bst.insert(6)\n",
|
||||||
" node9 = bst.insert(9)\n",
|
" node9 = bst.insert(9)\n",
|
||||||
" result = bst.invert_tree()\n",
|
" result = bst.invert_tree()\n",
|
||||||
" assert_equal(result, root)\n",
|
" self.assertEqual(result, root)\n",
|
||||||
" assert_equal(result.left, node7)\n",
|
" self.assertEqual(result.left, node7)\n",
|
||||||
" assert_equal(result.right, node2)\n",
|
" self.assertEqual(result.right, node2)\n",
|
||||||
" assert_equal(result.left.left, node9)\n",
|
" self.assertEqual(result.left.left, node9)\n",
|
||||||
" assert_equal(result.left.right, node6)\n",
|
" self.assertEqual(result.left.right, node6)\n",
|
||||||
" assert_equal(result.right.left, node3)\n",
|
" self.assertEqual(result.right.left, node3)\n",
|
||||||
" assert_equal(result.right.right, node1)\n",
|
" self.assertEqual(result.right.right, node1)\n",
|
||||||
" print('Success: test_invert_tree')\n",
|
" print('Success: test_invert_tree')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -188,9 +182,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -221,9 +213,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from nose.tools import assert_equal, assert_raises
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestInvertTree(object):
|
class TestInvertTree(unittest.TestCase):
|
||||||
|
|
||||||
def test_invert_tree(self):
|
def test_invert_tree(self):
|
||||||
root = Node(5)
|
root = Node(5)
|
||||||
@@ -13,13 +13,13 @@ class TestInvertTree(object):
|
|||||||
node6 = bst.insert(6)
|
node6 = bst.insert(6)
|
||||||
node9 = bst.insert(9)
|
node9 = bst.insert(9)
|
||||||
result = bst.invert_tree()
|
result = bst.invert_tree()
|
||||||
assert_equal(result, root)
|
self.assertEqual(result, root)
|
||||||
assert_equal(result.left, node7)
|
self.assertEqual(result.left, node7)
|
||||||
assert_equal(result.right, node2)
|
self.assertEqual(result.right, node2)
|
||||||
assert_equal(result.left.left, node9)
|
self.assertEqual(result.left.left, node9)
|
||||||
assert_equal(result.left.right, node6)
|
self.assertEqual(result.left.right, node6)
|
||||||
assert_equal(result.right.left, node3)
|
self.assertEqual(result.right.left, node3)
|
||||||
assert_equal(result.right.right, node1)
|
self.assertEqual(result.right.right, node1)
|
||||||
print('Success: test_invert_tree')
|
print('Success: test_invert_tree')
|
||||||
|
|
||||||
|
|
||||||
@@ -29,4 +29,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -29,12 +29,12 @@ class MinHeap(object):
|
|||||||
if key is None:
|
if key is None:
|
||||||
raise TypeError('key cannot be None')
|
raise TypeError('key cannot be None')
|
||||||
self.array.append(key)
|
self.array.append(key)
|
||||||
self._bubble_up(index=len(self.array)-1)
|
self._bubble_up(index=len(self.array) - 1)
|
||||||
|
|
||||||
def _bubble_up(self, index):
|
def _bubble_up(self, index):
|
||||||
if index == 0:
|
if index == 0:
|
||||||
return
|
return
|
||||||
index_parent = (index-1) // 2
|
index_parent = (index - 1) // 2
|
||||||
if self.array[index] < self.array[index_parent]:
|
if self.array[index] < self.array[index_parent]:
|
||||||
# Swap the indices and recurse
|
# Swap the indices and recurse
|
||||||
self.array[index], self.array[index_parent] = \
|
self.array[index], self.array[index_parent] = \
|
||||||
@@ -54,13 +54,17 @@ class MinHeap(object):
|
|||||||
def _find_smaller_child(self, index):
|
def _find_smaller_child(self, index):
|
||||||
left_child_index = 2 * index + 1
|
left_child_index = 2 * index + 1
|
||||||
right_child_index = 2 * index + 2
|
right_child_index = 2 * index + 2
|
||||||
|
# No right child
|
||||||
if right_child_index >= len(self.array):
|
if right_child_index >= len(self.array):
|
||||||
|
# No left child
|
||||||
if left_child_index >= len(self.array):
|
if left_child_index >= len(self.array):
|
||||||
return -1
|
return -1
|
||||||
|
# Left child only
|
||||||
else:
|
else:
|
||||||
return left_child_index
|
return left_child_index
|
||||||
else:
|
else:
|
||||||
|
# Compare left and right children
|
||||||
if self.array[left_child_index] < self.array[right_child_index]:
|
if self.array[left_child_index] < self.array[right_child_index]:
|
||||||
return left_child_index
|
return left_child_index
|
||||||
else:
|
else:
|
||||||
return right_child_index
|
return right_child_index
|
||||||
|
|||||||
@@ -95,9 +95,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class MinHeap(object):\n",
|
"class MinHeap(object):\n",
|
||||||
@@ -140,34 +138,32 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_min_heap.py\n",
|
"# %load test_min_heap.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestMinHeap(object):\n",
|
"class TestMinHeap(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_min_heap(self):\n",
|
" def test_min_heap(self):\n",
|
||||||
" heap = MinHeap()\n",
|
" heap = MinHeap()\n",
|
||||||
" assert_equal(heap.peek_min(), None)\n",
|
" self.assertEqual(heap.peek_min(), None)\n",
|
||||||
" assert_equal(heap.extract_min(), None)\n",
|
" self.assertEqual(heap.extract_min(), None)\n",
|
||||||
" heap.insert(20)\n",
|
" heap.insert(20)\n",
|
||||||
" assert_equal(heap.peek_min(), 20)\n",
|
" self.assertEqual(heap.peek_min(), 20)\n",
|
||||||
" heap.insert(5)\n",
|
" heap.insert(5)\n",
|
||||||
" assert_equal(heap.peek_min(), 5)\n",
|
" self.assertEqual(heap.peek_min(), 5)\n",
|
||||||
" heap.insert(15)\n",
|
" heap.insert(15)\n",
|
||||||
" heap.insert(22)\n",
|
" heap.insert(22)\n",
|
||||||
" heap.insert(40)\n",
|
" heap.insert(40)\n",
|
||||||
" heap.insert(5)\n",
|
" heap.insert(5)\n",
|
||||||
" assert_equal(heap.peek_min(), 5)\n",
|
" self.assertEqual(heap.peek_min(), 5)\n",
|
||||||
" heap.insert(3)\n",
|
" heap.insert(3)\n",
|
||||||
" assert_equal(heap.peek_min(), 3)\n",
|
" self.assertEqual(heap.peek_min(), 3)\n",
|
||||||
" assert_equal(heap.extract_min(), 3)\n",
|
" self.assertEqual(heap.extract_min(), 3)\n",
|
||||||
" assert_equal(heap.peek_min(), 5)\n",
|
" self.assertEqual(heap.peek_min(), 5)\n",
|
||||||
" print('Success: test_min_heap')\n",
|
" print('Success: test_min_heap')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" \n",
|
" \n",
|
||||||
@@ -206,9 +202,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -194,9 +194,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -283,9 +281,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run min_heap.py"
|
"%run min_heap.py"
|
||||||
@@ -301,9 +297,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -315,46 +309,46 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_min_heap.py\n",
|
"%%writefile test_min_heap.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestMinHeap(object):\n",
|
"class TestMinHeap(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_min_heap(self):\n",
|
" def test_min_heap(self):\n",
|
||||||
" heap = MinHeap()\n",
|
" heap = MinHeap()\n",
|
||||||
" assert_equal(heap.peek_min(), None)\n",
|
" self.assertEqual(heap.peek_min(), None)\n",
|
||||||
" assert_equal(heap.extract_min(), None)\n",
|
" self.assertEqual(heap.extract_min(), None)\n",
|
||||||
" heap.insert(20)\n",
|
" heap.insert(20)\n",
|
||||||
" assert_equal(heap.array[0], 20)\n",
|
" self.assertEqual(heap.array[0], 20)\n",
|
||||||
" heap.insert(5)\n",
|
" heap.insert(5)\n",
|
||||||
" assert_equal(heap.array[0], 5)\n",
|
" self.assertEqual(heap.array[0], 5)\n",
|
||||||
" assert_equal(heap.array[1], 20)\n",
|
" self.assertEqual(heap.array[1], 20)\n",
|
||||||
" heap.insert(15)\n",
|
" heap.insert(15)\n",
|
||||||
" assert_equal(heap.array[0], 5)\n",
|
" self.assertEqual(heap.array[0], 5)\n",
|
||||||
" assert_equal(heap.array[1], 20)\n",
|
" self.assertEqual(heap.array[1], 20)\n",
|
||||||
" assert_equal(heap.array[2], 15)\n",
|
" self.assertEqual(heap.array[2], 15)\n",
|
||||||
" heap.insert(22)\n",
|
" heap.insert(22)\n",
|
||||||
" assert_equal(heap.array[0], 5)\n",
|
" self.assertEqual(heap.array[0], 5)\n",
|
||||||
" assert_equal(heap.array[1], 20)\n",
|
" self.assertEqual(heap.array[1], 20)\n",
|
||||||
" assert_equal(heap.array[2], 15)\n",
|
" self.assertEqual(heap.array[2], 15)\n",
|
||||||
" assert_equal(heap.array[3], 22)\n",
|
" self.assertEqual(heap.array[3], 22)\n",
|
||||||
" heap.insert(40)\n",
|
" heap.insert(40)\n",
|
||||||
" assert_equal(heap.array[0], 5)\n",
|
" self.assertEqual(heap.array[0], 5)\n",
|
||||||
" assert_equal(heap.array[1], 20)\n",
|
" self.assertEqual(heap.array[1], 20)\n",
|
||||||
" assert_equal(heap.array[2], 15)\n",
|
" self.assertEqual(heap.array[2], 15)\n",
|
||||||
" assert_equal(heap.array[3], 22)\n",
|
" self.assertEqual(heap.array[3], 22)\n",
|
||||||
" assert_equal(heap.array[4], 40)\n",
|
" self.assertEqual(heap.array[4], 40)\n",
|
||||||
" heap.insert(3)\n",
|
" heap.insert(3)\n",
|
||||||
" assert_equal(heap.array[0], 3)\n",
|
" self.assertEqual(heap.array[0], 3)\n",
|
||||||
" assert_equal(heap.array[1], 20)\n",
|
" self.assertEqual(heap.array[1], 20)\n",
|
||||||
" assert_equal(heap.array[2], 5)\n",
|
" self.assertEqual(heap.array[2], 5)\n",
|
||||||
" assert_equal(heap.array[3], 22)\n",
|
" self.assertEqual(heap.array[3], 22)\n",
|
||||||
" assert_equal(heap.array[4], 40)\n",
|
" self.assertEqual(heap.array[4], 40)\n",
|
||||||
" assert_equal(heap.array[5], 15)\n",
|
" self.assertEqual(heap.array[5], 15)\n",
|
||||||
" mins = []\n",
|
" mins = []\n",
|
||||||
" while heap:\n",
|
" while heap:\n",
|
||||||
" mins.append(heap.extract_min())\n",
|
" mins.append(heap.extract_min())\n",
|
||||||
" assert_equal(mins, [3, 5, 15, 20, 22, 40])\n",
|
" self.assertEqual(mins, [3, 5, 15, 20, 22, 40])\n",
|
||||||
" print('Success: test_min_heap')\n",
|
" print('Success: test_min_heap')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" \n",
|
" \n",
|
||||||
@@ -370,9 +364,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -403,9 +395,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,43 +1,43 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestMinHeap(object):
|
class TestMinHeap(unittest.TestCase):
|
||||||
|
|
||||||
def test_min_heap(self):
|
def test_min_heap(self):
|
||||||
heap = MinHeap()
|
heap = MinHeap()
|
||||||
assert_equal(heap.peek_min(), None)
|
self.assertEqual(heap.peek_min(), None)
|
||||||
assert_equal(heap.extract_min(), None)
|
self.assertEqual(heap.extract_min(), None)
|
||||||
heap.insert(20)
|
heap.insert(20)
|
||||||
assert_equal(heap.array[0], 20)
|
self.assertEqual(heap.array[0], 20)
|
||||||
heap.insert(5)
|
heap.insert(5)
|
||||||
assert_equal(heap.array[0], 5)
|
self.assertEqual(heap.array[0], 5)
|
||||||
assert_equal(heap.array[1], 20)
|
self.assertEqual(heap.array[1], 20)
|
||||||
heap.insert(15)
|
heap.insert(15)
|
||||||
assert_equal(heap.array[0], 5)
|
self.assertEqual(heap.array[0], 5)
|
||||||
assert_equal(heap.array[1], 20)
|
self.assertEqual(heap.array[1], 20)
|
||||||
assert_equal(heap.array[2], 15)
|
self.assertEqual(heap.array[2], 15)
|
||||||
heap.insert(22)
|
heap.insert(22)
|
||||||
assert_equal(heap.array[0], 5)
|
self.assertEqual(heap.array[0], 5)
|
||||||
assert_equal(heap.array[1], 20)
|
self.assertEqual(heap.array[1], 20)
|
||||||
assert_equal(heap.array[2], 15)
|
self.assertEqual(heap.array[2], 15)
|
||||||
assert_equal(heap.array[3], 22)
|
self.assertEqual(heap.array[3], 22)
|
||||||
heap.insert(40)
|
heap.insert(40)
|
||||||
assert_equal(heap.array[0], 5)
|
self.assertEqual(heap.array[0], 5)
|
||||||
assert_equal(heap.array[1], 20)
|
self.assertEqual(heap.array[1], 20)
|
||||||
assert_equal(heap.array[2], 15)
|
self.assertEqual(heap.array[2], 15)
|
||||||
assert_equal(heap.array[3], 22)
|
self.assertEqual(heap.array[3], 22)
|
||||||
assert_equal(heap.array[4], 40)
|
self.assertEqual(heap.array[4], 40)
|
||||||
heap.insert(3)
|
heap.insert(3)
|
||||||
assert_equal(heap.array[0], 3)
|
self.assertEqual(heap.array[0], 3)
|
||||||
assert_equal(heap.array[1], 20)
|
self.assertEqual(heap.array[1], 20)
|
||||||
assert_equal(heap.array[2], 5)
|
self.assertEqual(heap.array[2], 5)
|
||||||
assert_equal(heap.array[3], 22)
|
self.assertEqual(heap.array[3], 22)
|
||||||
assert_equal(heap.array[4], 40)
|
self.assertEqual(heap.array[4], 40)
|
||||||
assert_equal(heap.array[5], 15)
|
self.assertEqual(heap.array[5], 15)
|
||||||
mins = []
|
mins = []
|
||||||
while heap:
|
while heap:
|
||||||
mins.append(heap.extract_min())
|
mins.append(heap.extract_min())
|
||||||
assert_equal(mins, [3, 5, 15, 20, 22, 40])
|
self.assertEqual(mins, [3, 5, 15, 20, 22, 40])
|
||||||
print('Success: test_min_heap')
|
print('Success: test_min_heap')
|
||||||
|
|
||||||
|
|
||||||
@@ -47,4 +47,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -82,9 +82,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class BstBfs(Bst):\n",
|
"class BstBfs(Bst):\n",
|
||||||
@@ -115,18 +113,17 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_bfs.py\n",
|
"# %load test_bfs.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestBfs(object):\n",
|
"class TestBfs(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self, *args, **kwargs):\n",
|
||||||
|
" super(TestBfs, self).__init__()\n",
|
||||||
" self.results = Results()\n",
|
" self.results = Results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bfs(self):\n",
|
" def test_bfs(self):\n",
|
||||||
@@ -136,7 +133,7 @@
|
|||||||
" bst.insert(1)\n",
|
" bst.insert(1)\n",
|
||||||
" bst.insert(3)\n",
|
" bst.insert(3)\n",
|
||||||
" bst.bfs(self.results.add_result)\n",
|
" bst.bfs(self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), '[5, 2, 8, 1, 3]')\n",
|
" self.assertEqual(str(self.results), '[5, 2, 8, 1, 3]')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bfs')\n",
|
" print('Success: test_bfs')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -176,9 +173,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,9 +78,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../bst/bst.py"
|
"%run ../bst/bst.py"
|
||||||
@@ -89,9 +87,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from collections import deque\n",
|
"from collections import deque\n",
|
||||||
@@ -123,9 +119,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../utils/results.py"
|
"%run ../utils/results.py"
|
||||||
@@ -134,9 +128,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -148,12 +140,13 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_bfs.py\n",
|
"%%writefile test_bfs.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestBfs(object):\n",
|
"class TestBfs(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self, *args, **kwargs):\n",
|
||||||
|
" super(TestBfs, self).__init__()\n",
|
||||||
" self.results = Results()\n",
|
" self.results = Results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bfs(self):\n",
|
" def test_bfs(self):\n",
|
||||||
@@ -163,7 +156,7 @@
|
|||||||
" bst.insert(1)\n",
|
" bst.insert(1)\n",
|
||||||
" bst.insert(3)\n",
|
" bst.insert(3)\n",
|
||||||
" bst.bfs(self.results.add_result)\n",
|
" bst.bfs(self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), '[5, 2, 8, 1, 3]')\n",
|
" self.assertEqual(str(self.results), '[5, 2, 8, 1, 3]')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bfs')\n",
|
" print('Success: test_bfs')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -180,9 +173,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -213,9 +204,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestBfs(object):
|
class TestBfs(unittest.TestCase):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(TestBfs, self).__init__()
|
||||||
self.results = Results()
|
self.results = Results()
|
||||||
|
|
||||||
def test_bfs(self):
|
def test_bfs(self):
|
||||||
@@ -13,7 +14,7 @@ class TestBfs(object):
|
|||||||
bst.insert(1)
|
bst.insert(1)
|
||||||
bst.insert(3)
|
bst.insert(3)
|
||||||
bst.bfs(self.results.add_result)
|
bst.bfs(self.results.add_result)
|
||||||
assert_equal(str(self.results), '[5, 2, 8, 1, 3]')
|
self.assertEqual(str(self.results), '[5, 2, 8, 1, 3]')
|
||||||
|
|
||||||
print('Success: test_bfs')
|
print('Success: test_bfs')
|
||||||
|
|
||||||
@@ -24,4 +25,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -82,21 +82,59 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../bst/bst.py\n",
|
"# %load ../bst/bst.py\n",
|
||||||
"%load ../bst/bst.py"
|
"class Node(object):\n",
|
||||||
|
"\n",
|
||||||
|
" def __init__(self, data):\n",
|
||||||
|
" self.data = data\n",
|
||||||
|
" self.left = None\n",
|
||||||
|
" self.right = None\n",
|
||||||
|
" self.parent = None\n",
|
||||||
|
"\n",
|
||||||
|
" def __repr__(self):\n",
|
||||||
|
" return str(self.data)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class Bst(object):\n",
|
||||||
|
"\n",
|
||||||
|
" def __init__(self, root=None):\n",
|
||||||
|
" self.root = root\n",
|
||||||
|
"\n",
|
||||||
|
" def insert(self, data):\n",
|
||||||
|
" if data is None:\n",
|
||||||
|
" raise TypeError('data cannot be None')\n",
|
||||||
|
" if self.root is None:\n",
|
||||||
|
" self.root = Node(data)\n",
|
||||||
|
" return self.root\n",
|
||||||
|
" else:\n",
|
||||||
|
" return self._insert(self.root, data)\n",
|
||||||
|
"\n",
|
||||||
|
" def _insert(self, node, data):\n",
|
||||||
|
" if node is None:\n",
|
||||||
|
" return Node(data)\n",
|
||||||
|
" if data <= node.data:\n",
|
||||||
|
" if node.left is None:\n",
|
||||||
|
" node.left = self._insert(node.left, data)\n",
|
||||||
|
" node.left.parent = node\n",
|
||||||
|
" return node.left\n",
|
||||||
|
" else:\n",
|
||||||
|
" return self._insert(node.left, data)\n",
|
||||||
|
" else:\n",
|
||||||
|
" if node.right is None:\n",
|
||||||
|
" node.right = self._insert(node.right, data)\n",
|
||||||
|
" node.right.parent = node\n",
|
||||||
|
" return node.right\n",
|
||||||
|
" else:\n",
|
||||||
|
" return self._insert(node.right, data)\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class BstDfs(Bst):\n",
|
"class BstDfs(Bst):\n",
|
||||||
@@ -124,9 +162,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../utils/results.py"
|
"%run ../utils/results.py"
|
||||||
@@ -135,18 +171,17 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_dfs.py\n",
|
"# %load test_dfs.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestDfs(object):\n",
|
"class TestDfs(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self, *args, **kwargs):\n",
|
||||||
|
" super(TestDfs, self).__init__()\n",
|
||||||
" self.results = Results()\n",
|
" self.results = Results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_dfs(self):\n",
|
" def test_dfs(self):\n",
|
||||||
@@ -157,15 +192,15 @@
|
|||||||
" bst.insert(3)\n",
|
" bst.insert(3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
|
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[1, 2, 3, 5, 8]\")\n",
|
" self.assertEqual(str(self.results), \"[1, 2, 3, 5, 8]\")\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
|
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[5, 2, 1, 3, 8]\")\n",
|
" self.assertEqual(str(self.results), \"[5, 2, 1, 3, 8]\")\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
|
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[1, 3, 2, 8, 5]\")\n",
|
" self.assertEqual(str(self.results), \"[1, 3, 2, 8, 5]\")\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst = BstDfs(Node(1))\n",
|
" bst = BstDfs(Node(1))\n",
|
||||||
@@ -175,15 +210,15 @@
|
|||||||
" bst.insert(5)\n",
|
" bst.insert(5)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
|
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
|
" self.assertEqual(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
|
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
|
" self.assertEqual(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
|
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[5, 4, 3, 2, 1]\")\n",
|
" self.assertEqual(str(self.results), \"[5, 4, 3, 2, 1]\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_dfs')\n",
|
" print('Success: test_dfs')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -223,9 +258,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,9 +116,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../bst/bst.py"
|
"%run ../bst/bst.py"
|
||||||
@@ -127,9 +125,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class BstDfs(Bst):\n",
|
"class BstDfs(Bst):\n",
|
||||||
@@ -163,9 +159,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../utils/results.py"
|
"%run ../utils/results.py"
|
||||||
@@ -174,9 +168,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -188,12 +180,13 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_dfs.py\n",
|
"%%writefile test_dfs.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestDfs(object):\n",
|
"class TestDfs(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self, *args, **kwargs):\n",
|
||||||
|
" super(TestDfs, self).__init__()\n",
|
||||||
" self.results = Results()\n",
|
" self.results = Results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_dfs(self):\n",
|
" def test_dfs(self):\n",
|
||||||
@@ -204,15 +197,15 @@
|
|||||||
" bst.insert(3)\n",
|
" bst.insert(3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
|
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[1, 2, 3, 5, 8]\")\n",
|
" self.assertEqual(str(self.results), \"[1, 2, 3, 5, 8]\")\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
|
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[5, 2, 1, 3, 8]\")\n",
|
" self.assertEqual(str(self.results), \"[5, 2, 1, 3, 8]\")\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
|
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[1, 3, 2, 8, 5]\")\n",
|
" self.assertEqual(str(self.results), \"[1, 3, 2, 8, 5]\")\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst = BstDfs(Node(1))\n",
|
" bst = BstDfs(Node(1))\n",
|
||||||
@@ -222,15 +215,15 @@
|
|||||||
" bst.insert(5)\n",
|
" bst.insert(5)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
|
" bst.in_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
|
" self.assertEqual(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
|
" bst.pre_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
|
" self.assertEqual(str(self.results), \"[1, 2, 3, 4, 5]\")\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
|
" bst.post_order_traversal(bst.root, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), \"[5, 4, 3, 2, 1]\")\n",
|
" self.assertEqual(str(self.results), \"[5, 4, 3, 2, 1]\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_dfs')\n",
|
" print('Success: test_dfs')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -247,9 +240,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -280,9 +271,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestDfs(object):
|
class TestDfs(unittest.TestCase):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(TestDfs, self).__init__()
|
||||||
self.results = Results()
|
self.results = Results()
|
||||||
|
|
||||||
def test_dfs(self):
|
def test_dfs(self):
|
||||||
@@ -14,15 +15,15 @@ class TestDfs(object):
|
|||||||
bst.insert(3)
|
bst.insert(3)
|
||||||
|
|
||||||
bst.in_order_traversal(bst.root, self.results.add_result)
|
bst.in_order_traversal(bst.root, self.results.add_result)
|
||||||
assert_equal(str(self.results), "[1, 2, 3, 5, 8]")
|
self.assertEqual(str(self.results), "[1, 2, 3, 5, 8]")
|
||||||
self.results.clear_results()
|
self.results.clear_results()
|
||||||
|
|
||||||
bst.pre_order_traversal(bst.root, self.results.add_result)
|
bst.pre_order_traversal(bst.root, self.results.add_result)
|
||||||
assert_equal(str(self.results), "[5, 2, 1, 3, 8]")
|
self.assertEqual(str(self.results), "[5, 2, 1, 3, 8]")
|
||||||
self.results.clear_results()
|
self.results.clear_results()
|
||||||
|
|
||||||
bst.post_order_traversal(bst.root, self.results.add_result)
|
bst.post_order_traversal(bst.root, self.results.add_result)
|
||||||
assert_equal(str(self.results), "[1, 3, 2, 8, 5]")
|
self.assertEqual(str(self.results), "[1, 3, 2, 8, 5]")
|
||||||
self.results.clear_results()
|
self.results.clear_results()
|
||||||
|
|
||||||
bst = BstDfs(Node(1))
|
bst = BstDfs(Node(1))
|
||||||
@@ -32,15 +33,15 @@ class TestDfs(object):
|
|||||||
bst.insert(5)
|
bst.insert(5)
|
||||||
|
|
||||||
bst.in_order_traversal(bst.root, self.results.add_result)
|
bst.in_order_traversal(bst.root, self.results.add_result)
|
||||||
assert_equal(str(self.results), "[1, 2, 3, 4, 5]")
|
self.assertEqual(str(self.results), "[1, 2, 3, 4, 5]")
|
||||||
self.results.clear_results()
|
self.results.clear_results()
|
||||||
|
|
||||||
bst.pre_order_traversal(bst.root, self.results.add_result)
|
bst.pre_order_traversal(bst.root, self.results.add_result)
|
||||||
assert_equal(str(self.results), "[1, 2, 3, 4, 5]")
|
self.assertEqual(str(self.results), "[1, 2, 3, 4, 5]")
|
||||||
self.results.clear_results()
|
self.results.clear_results()
|
||||||
|
|
||||||
bst.post_order_traversal(bst.root, self.results.add_result)
|
bst.post_order_traversal(bst.root, self.results.add_result)
|
||||||
assert_equal(str(self.results), "[5, 4, 3, 2, 1]")
|
self.assertEqual(str(self.results), "[5, 4, 3, 2, 1]")
|
||||||
|
|
||||||
print('Success: test_dfs')
|
print('Success: test_dfs')
|
||||||
|
|
||||||
@@ -51,4 +52,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -83,9 +83,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class BstHeight(Bst):\n",
|
"class BstHeight(Bst):\n",
|
||||||
@@ -112,25 +110,23 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_height.py\n",
|
"# %load test_height.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestHeight(object):\n",
|
"class TestHeight(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_height(self):\n",
|
" def test_height(self):\n",
|
||||||
" bst = BstHeight(Node(5))\n",
|
" bst = BstHeight(Node(5))\n",
|
||||||
" assert_equal(bst.height(bst.root), 1)\n",
|
" self.assertEqual(bst.height(bst.root), 1)\n",
|
||||||
" bst.insert(2)\n",
|
" bst.insert(2)\n",
|
||||||
" bst.insert(8)\n",
|
" bst.insert(8)\n",
|
||||||
" bst.insert(1)\n",
|
" bst.insert(1)\n",
|
||||||
" bst.insert(3)\n",
|
" bst.insert(3)\n",
|
||||||
" assert_equal(bst.height(bst.root), 3)\n",
|
" self.assertEqual(bst.height(bst.root), 3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_height')\n",
|
" print('Success: test_height')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -170,9 +166,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,9 +77,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../bst/bst.py"
|
"%run ../bst/bst.py"
|
||||||
@@ -88,9 +86,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class BstHeight(Bst):\n",
|
"class BstHeight(Bst):\n",
|
||||||
@@ -112,9 +108,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -126,19 +120,19 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_height.py\n",
|
"%%writefile test_height.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestHeight(object):\n",
|
"class TestHeight(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_height(self):\n",
|
" def test_height(self):\n",
|
||||||
" bst = BstHeight(Node(5))\n",
|
" bst = BstHeight(Node(5))\n",
|
||||||
" assert_equal(bst.height(bst.root), 1)\n",
|
" self.assertEqual(bst.height(bst.root), 1)\n",
|
||||||
" bst.insert(2)\n",
|
" bst.insert(2)\n",
|
||||||
" bst.insert(8)\n",
|
" bst.insert(8)\n",
|
||||||
" bst.insert(1)\n",
|
" bst.insert(1)\n",
|
||||||
" bst.insert(3)\n",
|
" bst.insert(3)\n",
|
||||||
" assert_equal(bst.height(bst.root), 3)\n",
|
" self.assertEqual(bst.height(bst.root), 3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_height')\n",
|
" print('Success: test_height')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -155,9 +149,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -188,9 +180,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestHeight(object):
|
class TestHeight(unittest.TestCase):
|
||||||
|
|
||||||
def test_height(self):
|
def test_height(self):
|
||||||
bst = BstHeight(Node(5))
|
bst = BstHeight(Node(5))
|
||||||
assert_equal(bst.height(bst.root), 1)
|
self.assertEqual(bst.height(bst.root), 1)
|
||||||
bst.insert(2)
|
bst.insert(2)
|
||||||
bst.insert(8)
|
bst.insert(8)
|
||||||
bst.insert(1)
|
bst.insert(1)
|
||||||
bst.insert(3)
|
bst.insert(3)
|
||||||
assert_equal(bst.height(bst.root), 3)
|
self.assertEqual(bst.height(bst.root), 3)
|
||||||
|
|
||||||
print('Success: test_height')
|
print('Success: test_height')
|
||||||
|
|
||||||
@@ -21,4 +21,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestLowestCommonAncestor(object):
|
class TestLowestCommonAncestor(unittest.TestCase):
|
||||||
|
|
||||||
def test_lca(self):
|
def test_lca(self):
|
||||||
node10 = Node(10)
|
node10 = Node(10)
|
||||||
@@ -26,13 +26,13 @@ class TestLowestCommonAncestor(object):
|
|||||||
root = node10
|
root = node10
|
||||||
node0 = Node(0)
|
node0 = Node(0)
|
||||||
binary_tree = BinaryTree()
|
binary_tree = BinaryTree()
|
||||||
assert_equal(binary_tree.lca(root, node0, node5), None)
|
self.assertEqual(binary_tree.lca(root, node0, node5), None)
|
||||||
assert_equal(binary_tree.lca(root, node5, node0), None)
|
self.assertEqual(binary_tree.lca(root, node5, node0), None)
|
||||||
assert_equal(binary_tree.lca(root, node1, node8), node3)
|
self.assertEqual(binary_tree.lca(root, node1, node8), node3)
|
||||||
assert_equal(binary_tree.lca(root, node12, node8), node5)
|
self.assertEqual(binary_tree.lca(root, node12, node8), node5)
|
||||||
assert_equal(binary_tree.lca(root, node12, node40), node10)
|
self.assertEqual(binary_tree.lca(root, node12, node40), node10)
|
||||||
assert_equal(binary_tree.lca(root, node9, node20), node9)
|
self.assertEqual(binary_tree.lca(root, node9, node20), node9)
|
||||||
assert_equal(binary_tree.lca(root, node3, node5), node5)
|
self.assertEqual(binary_tree.lca(root, node3, node5), node5)
|
||||||
print('Success: test_lca')
|
print('Success: test_lca')
|
||||||
|
|
||||||
|
|
||||||
@@ -42,4 +42,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -136,10 +136,10 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_lca.py\n",
|
"# %load test_lca.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestLowestCommonAncestor(object):\n",
|
"class TestLowestCommonAncestor(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_lca(self):\n",
|
" def test_lca(self):\n",
|
||||||
" node10 = Node(10)\n",
|
" node10 = Node(10)\n",
|
||||||
@@ -164,13 +164,13 @@
|
|||||||
" root = node10\n",
|
" root = node10\n",
|
||||||
" node0 = Node(0)\n",
|
" node0 = Node(0)\n",
|
||||||
" binary_tree = BinaryTree()\n",
|
" binary_tree = BinaryTree()\n",
|
||||||
" assert_equal(binary_tree.lca(root, node0, node5), None)\n",
|
" self.assertEqual(binary_tree.lca(root, node0, node5), None)\n",
|
||||||
" assert_equal(binary_tree.lca(root, node5, node0), None)\n",
|
" self.assertEqual(binary_tree.lca(root, node5, node0), None)\n",
|
||||||
" assert_equal(binary_tree.lca(root, node1, node8), node3)\n",
|
" self.assertEqual(binary_tree.lca(root, node1, node8), node3)\n",
|
||||||
" assert_equal(binary_tree.lca(root, node12, node8), node5)\n",
|
" self.assertEqual(binary_tree.lca(root, node12, node8), node5)\n",
|
||||||
" assert_equal(binary_tree.lca(root, node12, node40), node10)\n",
|
" self.assertEqual(binary_tree.lca(root, node12, node40), node10)\n",
|
||||||
" assert_equal(binary_tree.lca(root, node9, node20), node9)\n",
|
" self.assertEqual(binary_tree.lca(root, node9, node20), node9)\n",
|
||||||
" assert_equal(binary_tree.lca(root, node3, node5), node5)\n",
|
" self.assertEqual(binary_tree.lca(root, node3, node5), node5)\n",
|
||||||
" print('Success: test_lca')\n",
|
" print('Success: test_lca')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -209,7 +209,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.6.4"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -96,9 +96,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class Node(object):\n",
|
"class Node(object):\n",
|
||||||
@@ -115,9 +113,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class BinaryTree(object):\n",
|
"class BinaryTree(object):\n",
|
||||||
@@ -155,9 +151,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class LcaResult(object):\n",
|
"class LcaResult(object):\n",
|
||||||
@@ -209,9 +203,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -223,10 +215,10 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_lca.py\n",
|
"%%writefile test_lca.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestLowestCommonAncestor(object):\n",
|
"class TestLowestCommonAncestor(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_lca(self):\n",
|
" def test_lca(self):\n",
|
||||||
" node10 = Node(10)\n",
|
" node10 = Node(10)\n",
|
||||||
@@ -251,13 +243,13 @@
|
|||||||
" root = node10\n",
|
" root = node10\n",
|
||||||
" node0 = Node(0)\n",
|
" node0 = Node(0)\n",
|
||||||
" binary_tree = BinaryTree()\n",
|
" binary_tree = BinaryTree()\n",
|
||||||
" assert_equal(binary_tree.lca(root, node0, node5), None)\n",
|
" self.assertEqual(binary_tree.lca(root, node0, node5), None)\n",
|
||||||
" assert_equal(binary_tree.lca(root, node5, node0), None)\n",
|
" self.assertEqual(binary_tree.lca(root, node5, node0), None)\n",
|
||||||
" assert_equal(binary_tree.lca(root, node1, node8), node3)\n",
|
" self.assertEqual(binary_tree.lca(root, node1, node8), node3)\n",
|
||||||
" assert_equal(binary_tree.lca(root, node12, node8), node5)\n",
|
" self.assertEqual(binary_tree.lca(root, node12, node8), node5)\n",
|
||||||
" assert_equal(binary_tree.lca(root, node12, node40), node10)\n",
|
" self.assertEqual(binary_tree.lca(root, node12, node40), node10)\n",
|
||||||
" assert_equal(binary_tree.lca(root, node9, node20), node9)\n",
|
" self.assertEqual(binary_tree.lca(root, node9, node20), node9)\n",
|
||||||
" assert_equal(binary_tree.lca(root, node3, node5), node5)\n",
|
" self.assertEqual(binary_tree.lca(root, node3, node5), node5)\n",
|
||||||
" print('Success: test_lca')\n",
|
" print('Success: test_lca')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -273,9 +265,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -306,9 +296,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from nose.tools import assert_equal
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestTreeLevelLists(object):
|
class TestTreeLevelLists(unittest.TestCase):
|
||||||
|
|
||||||
def test_tree_level_lists(self):
|
def test_tree_level_lists(self):
|
||||||
bst = BstLevelLists(Node(5))
|
bst = BstLevelLists(Node(5))
|
||||||
@@ -24,11 +24,11 @@ class TestTreeLevelLists(object):
|
|||||||
results.add_result(node)
|
results.add_result(node)
|
||||||
results_list.append(results)
|
results_list.append(results)
|
||||||
|
|
||||||
assert_equal(str(results_list[0]), '[5]')
|
self.assertEqual(str(results_list[0]), '[5]')
|
||||||
assert_equal(str(results_list[1]), '[3, 8]')
|
self.assertEqual(str(results_list[1]), '[3, 8]')
|
||||||
assert_equal(str(results_list[2]), '[2, 4, 7, 9]')
|
self.assertEqual(str(results_list[2]), '[2, 4, 7, 9]')
|
||||||
assert_equal(str(results_list[3]), '[1, 6, 10]')
|
self.assertEqual(str(results_list[3]), '[1, 6, 10]')
|
||||||
assert_equal(str(results_list[4]), '[11]')
|
self.assertEqual(str(results_list[4]), '[11]')
|
||||||
|
|
||||||
print('Success: test_tree_level_lists')
|
print('Success: test_tree_level_lists')
|
||||||
|
|
||||||
@@ -39,4 +39,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -86,9 +86,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class BstLevelLists(Bst):\n",
|
"class BstLevelLists(Bst):\n",
|
||||||
@@ -126,16 +124,14 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_tree_level_lists.py\n",
|
"# %load test_tree_level_lists.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestTreeLevelLists(object):\n",
|
"class TestTreeLevelLists(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_tree_level_lists(self):\n",
|
" def test_tree_level_lists(self):\n",
|
||||||
" bst = BstLevelLists(Node(5))\n",
|
" bst = BstLevelLists(Node(5))\n",
|
||||||
@@ -158,11 +154,11 @@
|
|||||||
" results.add_result(node)\n",
|
" results.add_result(node)\n",
|
||||||
" results_list.append(results)\n",
|
" results_list.append(results)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" assert_equal(str(results_list[0]), '[5]')\n",
|
" self.assertEqual(str(results_list[0]), '[5]')\n",
|
||||||
" assert_equal(str(results_list[1]), '[3, 8]')\n",
|
" self.assertEqual(str(results_list[1]), '[3, 8]')\n",
|
||||||
" assert_equal(str(results_list[2]), '[2, 4, 7, 9]')\n",
|
" self.assertEqual(str(results_list[2]), '[2, 4, 7, 9]')\n",
|
||||||
" assert_equal(str(results_list[3]), '[1, 6, 10]')\n",
|
" self.assertEqual(str(results_list[3]), '[1, 6, 10]')\n",
|
||||||
" assert_equal(str(results_list[4]), '[11]')\n",
|
" self.assertEqual(str(results_list[4]), '[11]')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_tree_level_lists')\n",
|
" print('Success: test_tree_level_lists')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -202,9 +198,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,9 +87,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../bst/bst.py"
|
"%run ../bst/bst.py"
|
||||||
@@ -98,9 +96,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class BstLevelLists(Bst):\n",
|
"class BstLevelLists(Bst):\n",
|
||||||
@@ -134,9 +130,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": true
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run ../utils/results.py"
|
"%run ../utils/results.py"
|
||||||
@@ -145,9 +139,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -159,10 +151,10 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_tree_level_lists.py\n",
|
"%%writefile test_tree_level_lists.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestTreeLevelLists(object):\n",
|
"class TestTreeLevelLists(unittest.TestCase):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_tree_level_lists(self):\n",
|
" def test_tree_level_lists(self):\n",
|
||||||
" bst = BstLevelLists(Node(5))\n",
|
" bst = BstLevelLists(Node(5))\n",
|
||||||
@@ -185,11 +177,11 @@
|
|||||||
" results.add_result(node)\n",
|
" results.add_result(node)\n",
|
||||||
" results_list.append(results)\n",
|
" results_list.append(results)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" assert_equal(str(results_list[0]), '[5]')\n",
|
" self.assertEqual(str(results_list[0]), '[5]')\n",
|
||||||
" assert_equal(str(results_list[1]), '[3, 8]')\n",
|
" self.assertEqual(str(results_list[1]), '[3, 8]')\n",
|
||||||
" assert_equal(str(results_list[2]), '[2, 4, 7, 9]')\n",
|
" self.assertEqual(str(results_list[2]), '[2, 4, 7, 9]')\n",
|
||||||
" assert_equal(str(results_list[3]), '[1, 6, 10]')\n",
|
" self.assertEqual(str(results_list[3]), '[1, 6, 10]')\n",
|
||||||
" assert_equal(str(results_list[4]), '[11]')\n",
|
" self.assertEqual(str(results_list[4]), '[11]')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_tree_level_lists')\n",
|
" print('Success: test_tree_level_lists')\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -206,9 +198,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -239,9 +229,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
from nose.tools import assert_true
|
import unittest
|
||||||
from nose.tools import raises
|
|
||||||
|
|
||||||
|
|
||||||
class TestTrie(object):
|
class TestTrie(unittest.TestCase):
|
||||||
|
|
||||||
def test_trie(self):
|
def test_trie(self):
|
||||||
trie = Trie()
|
trie = Trie()
|
||||||
@@ -13,7 +12,7 @@ class TestTrie(object):
|
|||||||
for word in words:
|
for word in words:
|
||||||
trie.insert(word)
|
trie.insert(word)
|
||||||
for word in trie.list_words():
|
for word in trie.list_words():
|
||||||
assert_true(trie.find(word) is not None)
|
self.assertTrue(trie.find(word) is not None)
|
||||||
|
|
||||||
print('Test: Remove me')
|
print('Test: Remove me')
|
||||||
trie.remove('me')
|
trie.remove('me')
|
||||||
@@ -21,9 +20,9 @@ class TestTrie(object):
|
|||||||
words = ['a', 'at', 'has', 'hat', 'he',
|
words = ['a', 'at', 'has', 'hat', 'he',
|
||||||
'men', 'mens', 'met']
|
'men', 'mens', 'met']
|
||||||
for word in words:
|
for word in words:
|
||||||
assert_true(trie.find(word) is not None)
|
self.assertTrue(trie.find(word) is not None)
|
||||||
for word in words_removed:
|
for word in words_removed:
|
||||||
assert_true(trie.find(word) is None)
|
self.assertTrue(trie.find(word) is None)
|
||||||
|
|
||||||
print('Test: Remove mens')
|
print('Test: Remove mens')
|
||||||
trie.remove('mens')
|
trie.remove('mens')
|
||||||
@@ -31,9 +30,9 @@ class TestTrie(object):
|
|||||||
words = ['a', 'at', 'has', 'hat', 'he',
|
words = ['a', 'at', 'has', 'hat', 'he',
|
||||||
'men', 'met']
|
'men', 'met']
|
||||||
for word in words:
|
for word in words:
|
||||||
assert_true(trie.find(word) is not None)
|
self.assertTrue(trie.find(word) is not None)
|
||||||
for word in words_removed:
|
for word in words_removed:
|
||||||
assert_true(trie.find(word) is None)
|
self.assertTrue(trie.find(word) is None)
|
||||||
|
|
||||||
print('Test: Remove a')
|
print('Test: Remove a')
|
||||||
trie.remove('a')
|
trie.remove('a')
|
||||||
@@ -41,9 +40,9 @@ class TestTrie(object):
|
|||||||
words = ['at', 'has', 'hat', 'he',
|
words = ['at', 'has', 'hat', 'he',
|
||||||
'men', 'met']
|
'men', 'met']
|
||||||
for word in words:
|
for word in words:
|
||||||
assert_true(trie.find(word) is not None)
|
self.assertTrue(trie.find(word) is not None)
|
||||||
for word in words_removed:
|
for word in words_removed:
|
||||||
assert_true(trie.find(word) is None)
|
self.assertTrue(trie.find(word) is None)
|
||||||
|
|
||||||
print('Test: Remove has')
|
print('Test: Remove has')
|
||||||
trie.remove('has')
|
trie.remove('has')
|
||||||
@@ -51,24 +50,23 @@ class TestTrie(object):
|
|||||||
words = ['at', 'hat', 'he',
|
words = ['at', 'hat', 'he',
|
||||||
'men', 'met']
|
'men', 'met']
|
||||||
for word in words:
|
for word in words:
|
||||||
assert_true(trie.find(word) is not None)
|
self.assertTrue(trie.find(word) is not None)
|
||||||
for word in words_removed:
|
for word in words_removed:
|
||||||
assert_true(trie.find(word) is None)
|
self.assertTrue(trie.find(word) is None)
|
||||||
|
|
||||||
print('Success: test_trie')
|
print('Success: test_trie')
|
||||||
|
|
||||||
@raises(Exception)
|
|
||||||
def test_trie_remove_invalid(self):
|
def test_trie_remove_invalid(self):
|
||||||
print('Test: Remove from empty trie')
|
print('Test: Remove from empty trie')
|
||||||
trie = Trie()
|
trie = Trie()
|
||||||
assert_true(trie.remove('foo') is None)
|
self.assertTrue(trie.remove('foo') is None)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test = TestTrie()
|
test = TestTrie()
|
||||||
test.test_trie()
|
test.test_trie()
|
||||||
test.test_trie_remove_invalid()
|
test.assertRaises(KeyError, test.test_trie_remove_invalid)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -72,5 +72,5 @@ class Trie(object):
|
|||||||
return
|
return
|
||||||
for key, child in node.children.items():
|
for key, child in node.children.items():
|
||||||
if child.terminates:
|
if child.terminates:
|
||||||
result.append(curr_word+key)
|
result.append(curr_word + key)
|
||||||
self._list_words(child, curr_word+key, result)
|
self._list_words(child, curr_word + key, result)
|
||||||
|
|||||||
@@ -110,9 +110,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from collections import OrderedDict\n",
|
"from collections import OrderedDict\n",
|
||||||
@@ -165,21 +163,17 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %load test_trie.py\n",
|
"# %load test_trie.py\n",
|
||||||
"from nose.tools import assert_true\n",
|
"import unittest\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestTrie(object):\n",
|
"class TestTrie(unittest.TestCase): \n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_trie(self):\n",
|
" def test_trie(self):\n",
|
||||||
" print('Test: Remove from empty trie')\n",
|
|
||||||
" trie = Trie()\n",
|
" trie = Trie()\n",
|
||||||
" assert_true(trie.remove('foo') is None)\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: Insert')\n",
|
" print('Test: Insert')\n",
|
||||||
" words = ['a', 'at', 'has', 'hat', 'he',\n",
|
" words = ['a', 'at', 'has', 'hat', 'he',\n",
|
||||||
@@ -187,11 +181,7 @@
|
|||||||
" for word in words:\n",
|
" for word in words:\n",
|
||||||
" trie.insert(word)\n",
|
" trie.insert(word)\n",
|
||||||
" for word in trie.list_words():\n",
|
" for word in trie.list_words():\n",
|
||||||
" assert_true(trie.find(word) is not None)\n",
|
" self.assertTrue(trie.find(word) is not None)\n",
|
||||||
"\n",
|
|
||||||
" # Remove me\n",
|
|
||||||
" # Remove mens\n",
|
|
||||||
" # Remove a\n",
|
|
||||||
" \n",
|
" \n",
|
||||||
" print('Test: Remove me')\n",
|
" print('Test: Remove me')\n",
|
||||||
" trie.remove('me')\n",
|
" trie.remove('me')\n",
|
||||||
@@ -199,9 +189,9 @@
|
|||||||
" words = ['a', 'at', 'has', 'hat', 'he',\n",
|
" words = ['a', 'at', 'has', 'hat', 'he',\n",
|
||||||
" 'men', 'mens', 'met']\n",
|
" 'men', 'mens', 'met']\n",
|
||||||
" for word in words:\n",
|
" for word in words:\n",
|
||||||
" assert_true(trie.find(word) is not None)\n",
|
" self.assertTrue(trie.find(word) is not None)\n",
|
||||||
" for word in words_removed:\n",
|
" for word in words_removed:\n",
|
||||||
" assert_true(trie.find(word) is None)\n",
|
" self.assertTrue(trie.find(word) is None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: Remove mens')\n",
|
" print('Test: Remove mens')\n",
|
||||||
" trie.remove('mens')\n",
|
" trie.remove('mens')\n",
|
||||||
@@ -209,9 +199,9 @@
|
|||||||
" words = ['a', 'at', 'has', 'hat', 'he',\n",
|
" words = ['a', 'at', 'has', 'hat', 'he',\n",
|
||||||
" 'men', 'met']\n",
|
" 'men', 'met']\n",
|
||||||
" for word in words:\n",
|
" for word in words:\n",
|
||||||
" assert_true(trie.find(word) is not None)\n",
|
" self.assertTrue(trie.find(word) is not None)\n",
|
||||||
" for word in words_removed:\n",
|
" for word in words_removed:\n",
|
||||||
" assert_true(trie.find(word) is None)\n",
|
" self.assertTrue(trie.find(word) is None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: Remove a')\n",
|
" print('Test: Remove a')\n",
|
||||||
" trie.remove('a')\n",
|
" trie.remove('a')\n",
|
||||||
@@ -219,9 +209,9 @@
|
|||||||
" words = ['at', 'has', 'hat', 'he',\n",
|
" words = ['at', 'has', 'hat', 'he',\n",
|
||||||
" 'men', 'met']\n",
|
" 'men', 'met']\n",
|
||||||
" for word in words:\n",
|
" for word in words:\n",
|
||||||
" assert_true(trie.find(word) is not None)\n",
|
" self.assertTrue(trie.find(word) is not None)\n",
|
||||||
" for word in words_removed:\n",
|
" for word in words_removed:\n",
|
||||||
" assert_true(trie.find(word) is None)\n",
|
" self.assertTrue(trie.find(word) is None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: Remove has')\n",
|
" print('Test: Remove has')\n",
|
||||||
" trie.remove('has')\n",
|
" trie.remove('has')\n",
|
||||||
@@ -229,16 +219,22 @@
|
|||||||
" words = ['at', 'hat', 'he',\n",
|
" words = ['at', 'hat', 'he',\n",
|
||||||
" 'men', 'met']\n",
|
" 'men', 'met']\n",
|
||||||
" for word in words:\n",
|
" for word in words:\n",
|
||||||
" assert_true(trie.find(word) is not None)\n",
|
" self.assertTrue(trie.find(word) is not None)\n",
|
||||||
" for word in words_removed:\n",
|
" for word in words_removed:\n",
|
||||||
" assert_true(trie.find(word) is None)\n",
|
" self.assertTrue(trie.find(word) is None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_trie')\n",
|
" print('Success: test_trie')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
" def test_trie_remove_invalid(self):\n",
|
||||||
|
" print('Test: Remove from empty trie')\n",
|
||||||
|
" trie = Trie()\n",
|
||||||
|
" self.assertTrue(trie.remove('foo') is None) \n",
|
||||||
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestTrie()\n",
|
" test = TestTrie()\n",
|
||||||
" test.test_trie()\n",
|
" test.test_trie()\n",
|
||||||
|
" test.assertRaises(KeyError, test.test_trie_remove_invalid)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
@@ -271,9 +267,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -165,9 +165,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -260,9 +258,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run trie.py"
|
"%run trie.py"
|
||||||
@@ -278,9 +274,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -292,11 +286,10 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_trie.py\n",
|
"%%writefile test_trie.py\n",
|
||||||
"from nose.tools import assert_true\n",
|
"import unittest\n",
|
||||||
"from nose.tools import raises\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestTrie(object): \n",
|
"class TestTrie(unittest.TestCase): \n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_trie(self):\n",
|
" def test_trie(self):\n",
|
||||||
" trie = Trie()\n",
|
" trie = Trie()\n",
|
||||||
@@ -307,7 +300,7 @@
|
|||||||
" for word in words:\n",
|
" for word in words:\n",
|
||||||
" trie.insert(word)\n",
|
" trie.insert(word)\n",
|
||||||
" for word in trie.list_words():\n",
|
" for word in trie.list_words():\n",
|
||||||
" assert_true(trie.find(word) is not None)\n",
|
" self.assertTrue(trie.find(word) is not None)\n",
|
||||||
" \n",
|
" \n",
|
||||||
" print('Test: Remove me')\n",
|
" print('Test: Remove me')\n",
|
||||||
" trie.remove('me')\n",
|
" trie.remove('me')\n",
|
||||||
@@ -315,9 +308,9 @@
|
|||||||
" words = ['a', 'at', 'has', 'hat', 'he',\n",
|
" words = ['a', 'at', 'has', 'hat', 'he',\n",
|
||||||
" 'men', 'mens', 'met']\n",
|
" 'men', 'mens', 'met']\n",
|
||||||
" for word in words:\n",
|
" for word in words:\n",
|
||||||
" assert_true(trie.find(word) is not None)\n",
|
" self.assertTrue(trie.find(word) is not None)\n",
|
||||||
" for word in words_removed:\n",
|
" for word in words_removed:\n",
|
||||||
" assert_true(trie.find(word) is None)\n",
|
" self.assertTrue(trie.find(word) is None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: Remove mens')\n",
|
" print('Test: Remove mens')\n",
|
||||||
" trie.remove('mens')\n",
|
" trie.remove('mens')\n",
|
||||||
@@ -325,9 +318,9 @@
|
|||||||
" words = ['a', 'at', 'has', 'hat', 'he',\n",
|
" words = ['a', 'at', 'has', 'hat', 'he',\n",
|
||||||
" 'men', 'met']\n",
|
" 'men', 'met']\n",
|
||||||
" for word in words:\n",
|
" for word in words:\n",
|
||||||
" assert_true(trie.find(word) is not None)\n",
|
" self.assertTrue(trie.find(word) is not None)\n",
|
||||||
" for word in words_removed:\n",
|
" for word in words_removed:\n",
|
||||||
" assert_true(trie.find(word) is None)\n",
|
" self.assertTrue(trie.find(word) is None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: Remove a')\n",
|
" print('Test: Remove a')\n",
|
||||||
" trie.remove('a')\n",
|
" trie.remove('a')\n",
|
||||||
@@ -335,9 +328,9 @@
|
|||||||
" words = ['at', 'has', 'hat', 'he',\n",
|
" words = ['at', 'has', 'hat', 'he',\n",
|
||||||
" 'men', 'met']\n",
|
" 'men', 'met']\n",
|
||||||
" for word in words:\n",
|
" for word in words:\n",
|
||||||
" assert_true(trie.find(word) is not None)\n",
|
" self.assertTrue(trie.find(word) is not None)\n",
|
||||||
" for word in words_removed:\n",
|
" for word in words_removed:\n",
|
||||||
" assert_true(trie.find(word) is None)\n",
|
" self.assertTrue(trie.find(word) is None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: Remove has')\n",
|
" print('Test: Remove has')\n",
|
||||||
" trie.remove('has')\n",
|
" trie.remove('has')\n",
|
||||||
@@ -345,23 +338,22 @@
|
|||||||
" words = ['at', 'hat', 'he',\n",
|
" words = ['at', 'hat', 'he',\n",
|
||||||
" 'men', 'met']\n",
|
" 'men', 'met']\n",
|
||||||
" for word in words:\n",
|
" for word in words:\n",
|
||||||
" assert_true(trie.find(word) is not None)\n",
|
" self.assertTrue(trie.find(word) is not None)\n",
|
||||||
" for word in words_removed:\n",
|
" for word in words_removed:\n",
|
||||||
" assert_true(trie.find(word) is None)\n",
|
" self.assertTrue(trie.find(word) is None)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_trie')\n",
|
" print('Success: test_trie')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" @raises(Exception)\n",
|
|
||||||
" def test_trie_remove_invalid(self):\n",
|
" def test_trie_remove_invalid(self):\n",
|
||||||
" print('Test: Remove from empty trie')\n",
|
" print('Test: Remove from empty trie')\n",
|
||||||
" trie = Trie()\n",
|
" trie = Trie()\n",
|
||||||
" assert_true(trie.remove('foo') is None) \n",
|
" self.assertTrue(trie.remove('foo') is None) \n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestTrie()\n",
|
" test = TestTrie()\n",
|
||||||
" test.test_trie()\n",
|
" test.test_trie()\n",
|
||||||
" test.test_trie_remove_invalid()\n",
|
" test.assertRaises(KeyError, test.test_trie_remove_invalid)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
@@ -371,9 +363,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"collapsed": false
|
|
||||||
},
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@@ -410,9 +400,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.7.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 1
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user