diff --git a/graphs_trees/bst/bst.py b/graphs_trees/bst/bst.py index 9b23e8e..e199d84 100644 --- a/graphs_trees/bst/bst.py +++ b/graphs_trees/bst/bst.py @@ -40,4 +40,4 @@ class Bst(object): node.right.parent = node return node.right else: - return self._insert(node.right, data) \ No newline at end of file + return self._insert(node.right, data) diff --git a/graphs_trees/bst/bst_challenge.ipynb b/graphs_trees/bst/bst_challenge.ipynb index e971ef7..093715a 100644 --- a/graphs_trees/bst/bst_challenge.ipynb +++ b/graphs_trees/bst/bst_challenge.ipynb @@ -84,9 +84,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class Node(object):\n", @@ -142,18 +140,17 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_bst.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestTree(object):\n", + "class TestTree(unittest.TestCase):\n", "\n", - " def __init__(self):\n", + " def __init__(self, *args, **kwargs):\n", + " super(TestTree, self).__init__()\n", " self.results = Results()\n", "\n", " def test_tree_one(self):\n", @@ -164,7 +161,7 @@ " bst.insert(1)\n", " bst.insert(3)\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", "\n", " def test_tree_two(self):\n", @@ -175,7 +172,7 @@ " bst.insert(4)\n", " bst.insert(5)\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", " print('Success: test_tree')\n", "\n", @@ -216,9 +213,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/bst/bst_solution.ipynb b/graphs_trees/bst/bst_solution.ipynb index fb3bc5a..d761dbd 100644 --- a/graphs_trees/bst/bst_solution.ipynb +++ b/graphs_trees/bst/bst_solution.ipynb @@ -99,9 +99,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -161,9 +159,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "%run bst.py" @@ -179,9 +175,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run dfs.py" @@ -190,9 +184,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "%run ../utils/results.py" @@ -201,9 +193,7 @@ { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -215,12 +205,13 @@ ], "source": [ "%%writefile test_bst.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestTree(object):\n", + "class TestTree(unittest.TestCase):\n", "\n", - " def __init__(self):\n", + " def __init__(self, *args, **kwargs):\n", + " super(TestTree, self).__init__()\n", " self.results = Results()\n", "\n", " def test_tree_one(self):\n", @@ -231,7 +222,7 @@ " bst.insert(1)\n", " bst.insert(3)\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", "\n", " def test_tree_two(self):\n", @@ -242,7 +233,7 @@ " bst.insert(4)\n", " bst.insert(5)\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", " print('Success: test_tree')\n", "\n", @@ -260,9 +251,7 @@ { "cell_type": "code", "execution_count": 6, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -293,9 +282,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/bst/test_bst.py b/graphs_trees/bst/test_bst.py index 4c775ab..ecb6dfd 100644 --- a/graphs_trees/bst/test_bst.py +++ b/graphs_trees/bst/test_bst.py @@ -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() def test_tree_one(self): @@ -14,7 +15,7 @@ class TestTree(object): bst.insert(1) bst.insert(3) 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() def test_tree_two(self): @@ -25,7 +26,7 @@ class TestTree(object): bst.insert(4) bst.insert(5) 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') @@ -37,4 +38,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/bst_min/bst_min_challenge.ipynb b/graphs_trees/bst_min/bst_min_challenge.ipynb index 7531c6e..9cce6b6 100644 --- a/graphs_trees/bst_min/bst_min_challenge.ipynb +++ b/graphs_trees/bst_min/bst_min_challenge.ipynb @@ -85,9 +85,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class MinBst(object):\n", @@ -113,13 +111,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_bst_min.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", "def height(node):\n", @@ -129,18 +125,18 @@ " height(node.right))\n", "\n", "\n", - "class TestBstMin(object):\n", + "class TestBstMin(unittest.TestCase):\n", "\n", " def test_bst_min(self):\n", " min_bst = MinBst()\n", " array = [0, 1, 2, 3, 4, 5, 6]\n", " root = min_bst.create_min_bst(array)\n", - " assert_equal(height(root), 3)\n", + " self.assertEqual(height(root), 3)\n", "\n", " min_bst = MinBst()\n", " array = [0, 1, 2, 3, 4, 5, 6, 7]\n", " root = min_bst.create_min_bst(array)\n", - " assert_equal(height(root), 4)\n", + " self.assertEqual(height(root), 4)\n", "\n", " print('Success: test_bst_min')\n", "\n", @@ -180,9 +176,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/bst_min/bst_min_solution.ipynb b/graphs_trees/bst_min/bst_min_solution.ipynb index 4869b26..f74f6cc 100644 --- a/graphs_trees/bst_min/bst_min_solution.ipynb +++ b/graphs_trees/bst_min/bst_min_solution.ipynb @@ -83,9 +83,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../bst/bst.py" @@ -93,10 +91,8 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, + "execution_count": 2, + "metadata": {}, "outputs": [], "source": [ "from __future__ import division\n", @@ -129,9 +125,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -143,7 +137,7 @@ ], "source": [ "%%writefile test_bst_min.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", "def height(node):\n", @@ -153,18 +147,18 @@ " height(node.right))\n", "\n", "\n", - "class TestBstMin(object):\n", + "class TestBstMin(unittest.TestCase):\n", "\n", " def test_bst_min(self):\n", " min_bst = MinBst()\n", " array = [0, 1, 2, 3, 4, 5, 6]\n", " root = min_bst.create_min_bst(array)\n", - " assert_equal(height(root), 3)\n", + " self.assertEqual(height(root), 3)\n", "\n", " min_bst = MinBst()\n", " array = [0, 1, 2, 3, 4, 5, 6, 7]\n", " root = min_bst.create_min_bst(array)\n", - " assert_equal(height(root), 4)\n", + " self.assertEqual(height(root), 4)\n", "\n", " print('Success: test_bst_min')\n", "\n", @@ -181,9 +175,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -214,9 +206,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/bst_min/test_bst_min.py b/graphs_trees/bst_min/test_bst_min.py index 963ae70..7f239d7 100644 --- a/graphs_trees/bst_min/test_bst_min.py +++ b/graphs_trees/bst_min/test_bst_min.py @@ -1,4 +1,4 @@ -from nose.tools import assert_equal +import unittest def height(node): @@ -8,18 +8,18 @@ def height(node): height(node.right)) -class TestBstMin(object): +class TestBstMin(unittest.TestCase): def test_bst_min(self): min_bst = MinBst() array = [0, 1, 2, 3, 4, 5, 6] root = min_bst.create_min_bst(array) - assert_equal(height(root), 3) + self.assertEqual(height(root), 3) min_bst = MinBst() array = [0, 1, 2, 3, 4, 5, 6, 7] root = min_bst.create_min_bst(array) - assert_equal(height(root), 4) + self.assertEqual(height(root), 4) print('Success: test_bst_min') @@ -30,4 +30,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/bst_second_largest/bst_second_largest_challenge.ipynb b/graphs_trees/bst_second_largest/bst_second_largest_challenge.ipynb index 253fc86..d270ce4 100644 --- a/graphs_trees/bst_second_largest/bst_second_largest_challenge.ipynb +++ b/graphs_trees/bst_second_largest/bst_second_largest_challenge.ipynb @@ -136,14 +136,14 @@ "outputs": [], "source": [ "# %load test_bst_second_largest.py\n", - "from nose.tools import assert_equal, assert_raises\n", + "import unittest\n", "\n", "\n", - "class TestBstSecondLargest(object):\n", + "class TestBstSecondLargest(unittest.TestCase):\n", "\n", " def test_bst_second_largest(self):\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", " bst = Solution(root)\n", " node5 = bst.insert(5)\n", @@ -155,13 +155,13 @@ " node2 = bst.insert(2)\n", " node4 = bst.insert(4)\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", " bst = Solution(root)\n", " node5 = bst.insert(5)\n", " node3 = bst.insert(3)\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", "\n", "\n", @@ -200,7 +200,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.7.2" } }, "nbformat": 4, diff --git a/graphs_trees/bst_second_largest/bst_second_largest_solution.ipynb b/graphs_trees/bst_second_largest/bst_second_largest_solution.ipynb index 6da1202..f23af1a 100644 --- a/graphs_trees/bst_second_largest/bst_second_largest_solution.ipynb +++ b/graphs_trees/bst_second_largest/bst_second_largest_solution.ipynb @@ -124,9 +124,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../bst/bst.py" @@ -135,9 +133,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class Solution(Bst):\n", @@ -175,9 +171,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -189,14 +183,14 @@ ], "source": [ "%%writefile test_bst_second_largest.py\n", - "from nose.tools import assert_equal, assert_raises\n", + "import unittest\n", "\n", "\n", - "class TestBstSecondLargest(object):\n", + "class TestBstSecondLargest(unittest.TestCase):\n", "\n", " def test_bst_second_largest(self):\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", " bst = Solution(root)\n", " node5 = bst.insert(5)\n", @@ -208,13 +202,13 @@ " node2 = bst.insert(2)\n", " node4 = bst.insert(4)\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", " bst = Solution(root)\n", " node5 = bst.insert(5)\n", " node3 = bst.insert(3)\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", "\n", "\n", @@ -230,9 +224,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -263,9 +255,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/bst_second_largest/test_bst_second_largest.py b/graphs_trees/bst_second_largest/test_bst_second_largest.py index a5a67db..6145349 100644 --- a/graphs_trees/bst_second_largest/test_bst_second_largest.py +++ b/graphs_trees/bst_second_largest/test_bst_second_largest.py @@ -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): bst = Solution(None) - assert_raises(TypeError, bst.find_second_largest) + self.assertRaises(TypeError, bst.find_second_largest) root = Node(10) bst = Solution(root) node5 = bst.insert(5) @@ -17,13 +17,13 @@ class TestBstSecondLargest(object): node2 = bst.insert(2) node4 = bst.insert(4) node30 = bst.insert(30) - assert_equal(bst.find_second_largest(), node20) + self.assertEqual(bst.find_second_largest(), node20) root = Node(10) bst = Solution(root) node5 = bst.insert(5) node3 = bst.insert(3) node7 = bst.insert(7) - assert_equal(bst.find_second_largest(), node7) + self.assertEqual(bst.find_second_largest(), node7) print('Success: test_bst_second_largest') @@ -33,4 +33,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/bst_successor/bst_successor_challenge.ipynb b/graphs_trees/bst_successor/bst_successor_challenge.ipynb index 0d589ef..bc9baec 100644 --- a/graphs_trees/bst_successor/bst_successor_challenge.ipynb +++ b/graphs_trees/bst_successor/bst_successor_challenge.ipynb @@ -100,9 +100,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class BstSuccessor(object):\n", @@ -129,19 +127,15 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_bst_successor.py\n", - "from nose.tools import assert_equal\n", - "from nose.tools import raises\n", + "import unittest\n", "\n", "\n", - "class TestBstSuccessor(object):\n", + "class TestBstSuccessor(unittest.TestCase):\n", "\n", - " @raises(Exception)\n", " def test_bst_successor_empty(self):\n", " bst_successor = BstSuccessor()\n", " bst_successor.get_next(None)\n", @@ -164,10 +158,10 @@ " nodes[9] = bst.insert(9)\n", "\n", " bst_successor = BstSuccessor()\n", - " assert_equal(bst_successor.get_next(nodes[4]), 5)\n", - " assert_equal(bst_successor.get_next(nodes[5]), 6)\n", - " assert_equal(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[4]), 5)\n", + " self.assertEqual(bst_successor.get_next(nodes[5]), 6)\n", + " self.assertEqual(bst_successor.get_next(nodes[8]), 9)\n", + " self.assertEqual(bst_successor.get_next(nodes[15]), None)\n", "\n", " print('Success: test_bst_successor')\n", "\n", @@ -175,7 +169,7 @@ "def main():\n", " test = TestBstSuccessor()\n", " test.test_bst_successor()\n", - " test.test_bst_successor_empty()\n", + " test.assertRaises(TypeError, test.test_bst_successor_empty)\n", "\n", "\n", "if __name__ == '__main__':\n", @@ -208,9 +202,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/bst_successor/bst_successor_solution.ipynb b/graphs_trees/bst_successor/bst_successor_solution.ipynb index 4ee62ff..e02770d 100644 --- a/graphs_trees/bst_successor/bst_successor_solution.ipynb +++ b/graphs_trees/bst_successor/bst_successor_solution.ipynb @@ -95,9 +95,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../bst/bst.py" @@ -106,9 +104,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class BstSuccessor(object):\n", @@ -148,9 +144,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -162,13 +156,11 @@ ], "source": [ "%%writefile test_bst_successor.py\n", - "from nose.tools import assert_equal\n", - "from nose.tools import raises\n", + "import unittest\n", "\n", "\n", - "class TestBstSuccessor(object):\n", + "class TestBstSuccessor(unittest.TestCase):\n", "\n", - " @raises(Exception)\n", " def test_bst_successor_empty(self):\n", " bst_successor = BstSuccessor()\n", " bst_successor.get_next(None)\n", @@ -191,10 +183,10 @@ " nodes[9] = bst.insert(9)\n", "\n", " bst_successor = BstSuccessor()\n", - " assert_equal(bst_successor.get_next(nodes[4]), 5)\n", - " assert_equal(bst_successor.get_next(nodes[5]), 6)\n", - " assert_equal(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[4]), 5)\n", + " self.assertEqual(bst_successor.get_next(nodes[5]), 6)\n", + " self.assertEqual(bst_successor.get_next(nodes[8]), 9)\n", + " self.assertEqual(bst_successor.get_next(nodes[15]), None)\n", "\n", " print('Success: test_bst_successor')\n", "\n", @@ -202,7 +194,7 @@ "def main():\n", " test = TestBstSuccessor()\n", " test.test_bst_successor()\n", - " test.test_bst_successor_empty()\n", + " test.assertRaises(TypeError, test.test_bst_successor_empty)\n", "\n", "\n", "if __name__ == '__main__':\n", @@ -212,9 +204,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -245,9 +235,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/bst_successor/test_bst_successor.py b/graphs_trees/bst_successor/test_bst_successor.py index 4331d82..2acee3a 100644 --- a/graphs_trees/bst_successor/test_bst_successor.py +++ b/graphs_trees/bst_successor/test_bst_successor.py @@ -1,10 +1,8 @@ -from nose.tools import assert_equal -from nose.tools import raises +import unittest -class TestBstSuccessor(object): +class TestBstSuccessor(unittest.TestCase): - @raises(Exception) def test_bst_successor_empty(self): bst_successor = BstSuccessor() bst_successor.get_next(None) @@ -27,10 +25,10 @@ class TestBstSuccessor(object): nodes[9] = bst.insert(9) bst_successor = BstSuccessor() - assert_equal(bst_successor.get_next(nodes[4]), 5) - assert_equal(bst_successor.get_next(nodes[5]), 6) - assert_equal(bst_successor.get_next(nodes[8]), 9) - assert_equal(bst_successor.get_next(nodes[15]), None) + self.assertEqual(bst_successor.get_next(nodes[4]), 5) + self.assertEqual(bst_successor.get_next(nodes[5]), 6) + self.assertEqual(bst_successor.get_next(nodes[8]), 9) + self.assertEqual(bst_successor.get_next(nodes[15]), None) print('Success: test_bst_successor') @@ -38,8 +36,8 @@ class TestBstSuccessor(object): def main(): test = TestBstSuccessor() test.test_bst_successor() - test.test_bst_successor_empty() + test.assertRaises(TypeError, test.test_bst_successor_empty) if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/bst_validate/bst_validate_challenge.ipynb b/graphs_trees/bst_validate/bst_validate_challenge.ipynb index b611560..2e385cb 100644 --- a/graphs_trees/bst_validate/bst_validate_challenge.ipynb +++ b/graphs_trees/bst_validate/bst_validate_challenge.ipynb @@ -88,21 +88,59 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "%run ../bst/bst.py\n", - "%load ../bst/bst.py" + "# %load ../bst/bst.py\n", + "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", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class BstValidate(Bst):\n", @@ -129,21 +167,18 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_bst_validate.py\n", - "from nose.tools import assert_equal\n", - "from nose.tools import raises\n", + "import unittest\n", "\n", "\n", - "class TestBstValidate(object):\n", + "class TestBstValidate(unittest.TestCase):\n", "\n", - " @raises(Exception)\n", " def test_bst_validate_empty(self):\n", - " validate_bst(None)\n", + " bst = BstValidate(None)\n", + " bst.validate()\n", "\n", " def test_bst_validate(self):\n", " bst = BstValidate(Node(5))\n", @@ -152,7 +187,7 @@ " bst.insert(6)\n", " bst.insert(4)\n", " bst.insert(7)\n", - " assert_equal(bst.validate(), True)\n", + " self.assertEqual(bst.validate(), True)\n", "\n", " bst = BstValidate(Node(5))\n", " left = Node(5)\n", @@ -161,14 +196,14 @@ " bst.root.left = left\n", " bst.root.right = right\n", " bst.root.left.right = invalid\n", - " assert_equal(bst.validate(), False)\n", + " self.assertEqual(bst.validate(), False)\n", "\n", " print('Success: test_bst_validate')\n", "\n", "\n", "def main():\n", " test = TestBstValidate()\n", - " test.test_bst_validate_empty()\n", + " test.assertRaises(TypeError, test.test_bst_validate_empty)\n", " test.test_bst_validate()\n", "\n", "\n", @@ -202,9 +237,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/bst_validate/bst_validate_solution.ipynb b/graphs_trees/bst_validate/bst_validate_solution.ipynb index 41839e2..f67e6bb 100644 --- a/graphs_trees/bst_validate/bst_validate_solution.ipynb +++ b/graphs_trees/bst_validate/bst_validate_solution.ipynb @@ -99,9 +99,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../bst/bst.py" @@ -110,9 +108,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "import sys\n", @@ -147,9 +143,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -161,15 +155,14 @@ ], "source": [ "%%writefile test_bst_validate.py\n", - "from nose.tools import assert_equal\n", - "from nose.tools import raises\n", + "import unittest\n", "\n", "\n", - "class TestBstValidate(object):\n", + "class TestBstValidate(unittest.TestCase):\n", "\n", - " @raises(Exception)\n", " def test_bst_validate_empty(self):\n", - " validate_bst(None)\n", + " bst = BstValidate(None)\n", + " bst.validate()\n", "\n", " def test_bst_validate(self):\n", " bst = BstValidate(Node(5))\n", @@ -178,7 +171,7 @@ " bst.insert(6)\n", " bst.insert(4)\n", " bst.insert(7)\n", - " assert_equal(bst.validate(), True)\n", + " self.assertEqual(bst.validate(), True)\n", "\n", " bst = BstValidate(Node(5))\n", " left = Node(5)\n", @@ -187,14 +180,14 @@ " bst.root.left = left\n", " bst.root.right = right\n", " bst.root.left.right = invalid\n", - " assert_equal(bst.validate(), False)\n", + " self.assertEqual(bst.validate(), False)\n", "\n", " print('Success: test_bst_validate')\n", "\n", "\n", "def main():\n", " test = TestBstValidate()\n", - " test.test_bst_validate_empty()\n", + " test.assertRaises(TypeError, test.test_bst_validate_empty)\n", " test.test_bst_validate()\n", "\n", "\n", @@ -205,9 +198,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -238,9 +229,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/bst_validate/test_bst_validate.py b/graphs_trees/bst_validate/test_bst_validate.py index 89a9dcb..736cfa0 100644 --- a/graphs_trees/bst_validate/test_bst_validate.py +++ b/graphs_trees/bst_validate/test_bst_validate.py @@ -1,12 +1,11 @@ -from nose.tools import assert_equal -from nose.tools import raises +import unittest -class TestBstValidate(object): +class TestBstValidate(unittest.TestCase): - @raises(Exception) def test_bst_validate_empty(self): - validate_bst(None) + bst = BstValidate(None) + bst.validate() def test_bst_validate(self): bst = BstValidate(Node(5)) @@ -15,7 +14,7 @@ class TestBstValidate(object): bst.insert(6) bst.insert(4) bst.insert(7) - assert_equal(bst.validate(), True) + self.assertEqual(bst.validate(), True) bst = BstValidate(Node(5)) left = Node(5) @@ -24,16 +23,16 @@ class TestBstValidate(object): bst.root.left = left bst.root.right = right bst.root.left.right = invalid - assert_equal(bst.validate(), False) + self.assertEqual(bst.validate(), False) print('Success: test_bst_validate') def main(): test = TestBstValidate() - test.test_bst_validate_empty() + test.assertRaises(TypeError, test.test_bst_validate_empty) test.test_bst_validate() if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/check_balance/check_balance_challenge.ipynb b/graphs_trees/check_balance/check_balance_challenge.ipynb index 9b87d0c..e813f2e 100644 --- a/graphs_trees/check_balance/check_balance_challenge.ipynb +++ b/graphs_trees/check_balance/check_balance_challenge.ipynb @@ -87,9 +87,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class BstBalance(Bst):\n", @@ -116,39 +114,35 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_check_balance.py\n", - "from nose.tools import assert_equal\n", - "from nose.tools import raises\n", + "import unittest\n", "\n", "\n", - "class TestCheckBalance(object):\n", + "class TestCheckBalance(unittest.TestCase):\n", "\n", - " @raises(TypeError)\n", " def test_check_balance_empty(self):\n", " bst = BstBalance(None)\n", " bst.check_balance()\n", "\n", " def test_check_balance(self):\n", " bst = BstBalance(Node(5))\n", - " assert_equal(bst.check_balance(), True)\n", + " self.assertEqual(bst.check_balance(), True)\n", "\n", " bst.insert(3)\n", " bst.insert(8)\n", " bst.insert(1)\n", " bst.insert(4)\n", - " assert_equal(bst.check_balance(), True)\n", + " self.assertEqual(bst.check_balance(), True)\n", "\n", " bst = BstBalance(Node(5))\n", " bst.insert(3)\n", " bst.insert(8)\n", " bst.insert(9)\n", " bst.insert(10)\n", - " assert_equal(bst.check_balance(), False)\n", + " self.assertEqual(bst.check_balance(), False)\n", "\n", " bst = BstBalance(Node(3))\n", " bst.insert(2)\n", @@ -157,14 +151,14 @@ " bst.insert(4)\n", " bst.insert(6)\n", " bst.insert(7)\n", - " assert_equal(bst.check_balance(), True)\n", + " self.assertEqual(bst.check_balance(), True)\n", "\n", " print('Success: test_check_balance')\n", "\n", "\n", "def main():\n", " test = TestCheckBalance()\n", - " test.test_check_balance_empty()\n", + " test.assertRaises(TypeError, test.test_check_balance_empty)\n", " test.test_check_balance()\n", "\n", "\n", @@ -198,9 +192,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/check_balance/check_balance_solution.ipynb b/graphs_trees/check_balance/check_balance_solution.ipynb index 5835927..11cc9bc 100644 --- a/graphs_trees/check_balance/check_balance_solution.ipynb +++ b/graphs_trees/check_balance/check_balance_solution.ipynb @@ -86,9 +86,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../bst/bst.py" @@ -97,9 +95,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "class BstBalance(Bst):\n", @@ -135,9 +131,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -149,33 +143,31 @@ ], "source": [ "%%writefile test_check_balance.py\n", - "from nose.tools import assert_equal\n", - "from nose.tools import raises\n", + "import unittest\n", "\n", "\n", - "class TestCheckBalance(object):\n", + "class TestCheckBalance(unittest.TestCase):\n", "\n", - " @raises(TypeError)\n", " def test_check_balance_empty(self):\n", " bst = BstBalance(None)\n", " bst.check_balance()\n", "\n", " def test_check_balance(self):\n", " bst = BstBalance(Node(5))\n", - " assert_equal(bst.check_balance(), True)\n", + " self.assertEqual(bst.check_balance(), True)\n", "\n", " bst.insert(3)\n", " bst.insert(8)\n", " bst.insert(1)\n", " bst.insert(4)\n", - " assert_equal(bst.check_balance(), True)\n", + " self.assertEqual(bst.check_balance(), True)\n", "\n", " bst = BstBalance(Node(5))\n", " bst.insert(3)\n", " bst.insert(8)\n", " bst.insert(9)\n", " bst.insert(10)\n", - " assert_equal(bst.check_balance(), False)\n", + " self.assertEqual(bst.check_balance(), False)\n", "\n", " bst = BstBalance(Node(3))\n", " bst.insert(2)\n", @@ -184,14 +176,14 @@ " bst.insert(4)\n", " bst.insert(6)\n", " bst.insert(7)\n", - " assert_equal(bst.check_balance(), True)\n", + " self.assertEqual(bst.check_balance(), True)\n", "\n", " print('Success: test_check_balance')\n", "\n", "\n", "def main():\n", " test = TestCheckBalance()\n", - " test.test_check_balance_empty()\n", + " test.assertRaises(TypeError, test.test_check_balance_empty)\n", " test.test_check_balance()\n", "\n", "\n", @@ -202,9 +194,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -235,9 +225,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/check_balance/test_check_balance.py b/graphs_trees/check_balance/test_check_balance.py index 55bd223..61ef727 100644 --- a/graphs_trees/check_balance/test_check_balance.py +++ b/graphs_trees/check_balance/test_check_balance.py @@ -1,30 +1,28 @@ -from nose.tools import assert_equal -from nose.tools import raises +import unittest -class TestCheckBalance(object): +class TestCheckBalance(unittest.TestCase): - @raises(TypeError) def test_check_balance_empty(self): bst = BstBalance(None) bst.check_balance() def test_check_balance(self): bst = BstBalance(Node(5)) - assert_equal(bst.check_balance(), True) + self.assertEqual(bst.check_balance(), True) bst.insert(3) bst.insert(8) bst.insert(1) bst.insert(4) - assert_equal(bst.check_balance(), True) + self.assertEqual(bst.check_balance(), True) bst = BstBalance(Node(5)) bst.insert(3) bst.insert(8) bst.insert(9) bst.insert(10) - assert_equal(bst.check_balance(), False) + self.assertEqual(bst.check_balance(), False) bst = BstBalance(Node(3)) bst.insert(2) @@ -33,16 +31,16 @@ class TestCheckBalance(object): bst.insert(4) bst.insert(6) bst.insert(7) - assert_equal(bst.check_balance(), True) + self.assertEqual(bst.check_balance(), True) print('Success: test_check_balance') def main(): test = TestCheckBalance() - test.test_check_balance_empty() + test.assertRaises(TypeError, test.test_check_balance_empty) test.test_check_balance() if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/graph/graph.py b/graphs_trees/graph/graph.py index 0681db3..347a55e 100644 --- a/graphs_trees/graph/graph.py +++ b/graphs_trees/graph/graph.py @@ -2,6 +2,7 @@ from enum import Enum # Python 2 users: Run pip install enum34 class State(Enum): + unvisited = 0 visiting = 1 visited = 2 @@ -64,4 +65,4 @@ class Graph: if src_key is None or dst_key is None: raise TypeError('key cannot be None') self.add_edge(src_key, dst_key, weight) - self.add_edge(dst_key, src_key, weight) \ No newline at end of file + self.add_edge(dst_key, src_key, weight) diff --git a/graphs_trees/graph/graph_challenge.ipynb b/graphs_trees/graph/graph_challenge.ipynb index c8a7db7..0303eae 100644 --- a/graphs_trees/graph/graph_challenge.ipynb +++ b/graphs_trees/graph/graph_challenge.ipynb @@ -175,10 +175,10 @@ "outputs": [], "source": [ "# %load test_graph.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestGraph(object):\n", + "class TestGraph(unittest.TestCase):\n", "\n", " def create_graph(self):\n", " graph = Graph()\n", @@ -198,29 +198,29 @@ " graph.add_edge(5, 4, weight=8)\n", " graph.add_edge(5, 2, weight=9)\n", "\n", - " assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)\n", - " assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n", - " assert_equal(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n", - " assert_equal(graph.nodes[2].adj_weights[graph.nodes[3].key], 4)\n", - " assert_equal(graph.nodes[3].adj_weights[graph.nodes[4].key], 5)\n", - " assert_equal(graph.nodes[3].adj_weights[graph.nodes[5].key], 6)\n", - " assert_equal(graph.nodes[4].adj_weights[graph.nodes[0].key], 7)\n", - " assert_equal(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[0].adj_weights[graph.nodes[1].key], 5)\n", + " self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n", + " self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n", + " self.assertEqual(graph.nodes[2].adj_weights[graph.nodes[3].key], 4)\n", + " self.assertEqual(graph.nodes[3].adj_weights[graph.nodes[4].key], 5)\n", + " self.assertEqual(graph.nodes[3].adj_weights[graph.nodes[5].key], 6)\n", + " self.assertEqual(graph.nodes[4].adj_weights[graph.nodes[0].key], 7)\n", + " self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[4].key], 8)\n", + " self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[2].key], 9)\n", "\n", - " assert_equal(graph.nodes[0].incoming_edges, 1)\n", - " assert_equal(graph.nodes[1].incoming_edges, 1)\n", - " assert_equal(graph.nodes[2].incoming_edges, 2)\n", - " assert_equal(graph.nodes[3].incoming_edges, 1)\n", - " assert_equal(graph.nodes[4].incoming_edges, 2)\n", - " assert_equal(graph.nodes[5].incoming_edges, 2)\n", + " self.assertEqual(graph.nodes[0].incoming_edges, 1)\n", + " self.assertEqual(graph.nodes[1].incoming_edges, 1)\n", + " self.assertEqual(graph.nodes[2].incoming_edges, 2)\n", + " self.assertEqual(graph.nodes[3].incoming_edges, 1)\n", + " self.assertEqual(graph.nodes[4].incoming_edges, 2)\n", + " self.assertEqual(graph.nodes[5].incoming_edges, 2)\n", "\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", - " assert_equal(graph.nodes[4].incoming_edges, 1)\n", + " self.assertEqual(graph.nodes[4].incoming_edges, 1)\n", "\n", - " assert_equal(graph.nodes[0] < graph.nodes[1], True)\n", + " self.assertEqual(graph.nodes[0] < graph.nodes[1], True)\n", "\n", " print('Success: test_graph')\n", "\n", @@ -230,12 +230,12 @@ " graph.add_undirected_edge(0, 5, weight=2)\n", " graph.add_undirected_edge(1, 2, weight=3)\n", "\n", - " assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)\n", - " assert_equal(graph.nodes[1].adj_weights[graph.nodes[0].key], 5)\n", - " assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n", - " assert_equal(graph.nodes[5].adj_weights[graph.nodes[0].key], 2)\n", - " assert_equal(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[0].adj_weights[graph.nodes[1].key], 5)\n", + " self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[0].key], 5)\n", + " self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n", + " self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[0].key], 2)\n", + " self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n", + " self.assertEqual(graph.nodes[2].adj_weights[graph.nodes[1].key], 3)\n", "\n", " print('Success: test_graph_undirected')\n", "\n", @@ -276,7 +276,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.7.2" } }, "nbformat": 4, diff --git a/graphs_trees/graph/graph_solution.ipynb b/graphs_trees/graph/graph_solution.ipynb index ddfe0ae..7cbae77 100644 --- a/graphs_trees/graph/graph_solution.ipynb +++ b/graphs_trees/graph/graph_solution.ipynb @@ -154,9 +154,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -241,9 +239,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "%run graph.py" @@ -259,9 +255,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -273,10 +267,10 @@ ], "source": [ "%%writefile test_graph.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestGraph(object):\n", + "class TestGraph(unittest.TestCase):\n", "\n", " def create_graph(self):\n", " graph = Graph()\n", @@ -296,29 +290,29 @@ " graph.add_edge(5, 4, weight=8)\n", " graph.add_edge(5, 2, weight=9)\n", "\n", - " assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)\n", - " assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n", - " assert_equal(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n", - " assert_equal(graph.nodes[2].adj_weights[graph.nodes[3].key], 4)\n", - " assert_equal(graph.nodes[3].adj_weights[graph.nodes[4].key], 5)\n", - " assert_equal(graph.nodes[3].adj_weights[graph.nodes[5].key], 6)\n", - " assert_equal(graph.nodes[4].adj_weights[graph.nodes[0].key], 7)\n", - " assert_equal(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[0].adj_weights[graph.nodes[1].key], 5)\n", + " self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n", + " self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n", + " self.assertEqual(graph.nodes[2].adj_weights[graph.nodes[3].key], 4)\n", + " self.assertEqual(graph.nodes[3].adj_weights[graph.nodes[4].key], 5)\n", + " self.assertEqual(graph.nodes[3].adj_weights[graph.nodes[5].key], 6)\n", + " self.assertEqual(graph.nodes[4].adj_weights[graph.nodes[0].key], 7)\n", + " self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[4].key], 8)\n", + " self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[2].key], 9)\n", "\n", - " assert_equal(graph.nodes[0].incoming_edges, 1)\n", - " assert_equal(graph.nodes[1].incoming_edges, 1)\n", - " assert_equal(graph.nodes[2].incoming_edges, 2)\n", - " assert_equal(graph.nodes[3].incoming_edges, 1)\n", - " assert_equal(graph.nodes[4].incoming_edges, 2)\n", - " assert_equal(graph.nodes[5].incoming_edges, 2)\n", + " self.assertEqual(graph.nodes[0].incoming_edges, 1)\n", + " self.assertEqual(graph.nodes[1].incoming_edges, 1)\n", + " self.assertEqual(graph.nodes[2].incoming_edges, 2)\n", + " self.assertEqual(graph.nodes[3].incoming_edges, 1)\n", + " self.assertEqual(graph.nodes[4].incoming_edges, 2)\n", + " self.assertEqual(graph.nodes[5].incoming_edges, 2)\n", "\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", - " assert_equal(graph.nodes[4].incoming_edges, 1)\n", + " self.assertEqual(graph.nodes[4].incoming_edges, 1)\n", "\n", - " assert_equal(graph.nodes[0] < graph.nodes[1], True)\n", + " self.assertEqual(graph.nodes[0] < graph.nodes[1], True)\n", "\n", " print('Success: test_graph')\n", "\n", @@ -328,12 +322,12 @@ " graph.add_undirected_edge(0, 5, weight=2)\n", " graph.add_undirected_edge(1, 2, weight=3)\n", "\n", - " assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)\n", - " assert_equal(graph.nodes[1].adj_weights[graph.nodes[0].key], 5)\n", - " assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n", - " assert_equal(graph.nodes[5].adj_weights[graph.nodes[0].key], 2)\n", - " assert_equal(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[0].adj_weights[graph.nodes[1].key], 5)\n", + " self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[0].key], 5)\n", + " self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n", + " self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[0].key], 2)\n", + " self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n", + " self.assertEqual(graph.nodes[2].adj_weights[graph.nodes[1].key], 3)\n", "\n", " print('Success: test_graph_undirected')\n", "\n", @@ -351,9 +345,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -385,9 +377,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/graph/test_graph.py b/graphs_trees/graph/test_graph.py index 1ea2998..254ec50 100644 --- a/graphs_trees/graph/test_graph.py +++ b/graphs_trees/graph/test_graph.py @@ -1,7 +1,7 @@ -from nose.tools import assert_equal +import unittest -class TestGraph(object): +class TestGraph(unittest.TestCase): def create_graph(self): graph = Graph() @@ -21,29 +21,29 @@ class TestGraph(object): graph.add_edge(5, 4, weight=8) graph.add_edge(5, 2, weight=9) - assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5) - assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2) - assert_equal(graph.nodes[1].adj_weights[graph.nodes[2].key], 3) - assert_equal(graph.nodes[2].adj_weights[graph.nodes[3].key], 4) - assert_equal(graph.nodes[3].adj_weights[graph.nodes[4].key], 5) - assert_equal(graph.nodes[3].adj_weights[graph.nodes[5].key], 6) - assert_equal(graph.nodes[4].adj_weights[graph.nodes[0].key], 7) - assert_equal(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[0].adj_weights[graph.nodes[1].key], 5) + self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2) + self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[2].key], 3) + self.assertEqual(graph.nodes[2].adj_weights[graph.nodes[3].key], 4) + self.assertEqual(graph.nodes[3].adj_weights[graph.nodes[4].key], 5) + self.assertEqual(graph.nodes[3].adj_weights[graph.nodes[5].key], 6) + self.assertEqual(graph.nodes[4].adj_weights[graph.nodes[0].key], 7) + self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[4].key], 8) + self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[2].key], 9) - assert_equal(graph.nodes[0].incoming_edges, 1) - assert_equal(graph.nodes[1].incoming_edges, 1) - assert_equal(graph.nodes[2].incoming_edges, 2) - assert_equal(graph.nodes[3].incoming_edges, 1) - assert_equal(graph.nodes[4].incoming_edges, 2) - assert_equal(graph.nodes[5].incoming_edges, 2) + self.assertEqual(graph.nodes[0].incoming_edges, 1) + self.assertEqual(graph.nodes[1].incoming_edges, 1) + self.assertEqual(graph.nodes[2].incoming_edges, 2) + self.assertEqual(graph.nodes[3].incoming_edges, 1) + self.assertEqual(graph.nodes[4].incoming_edges, 2) + self.assertEqual(graph.nodes[5].incoming_edges, 2) 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]) - 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') @@ -53,12 +53,12 @@ class TestGraph(object): graph.add_undirected_edge(0, 5, weight=2) graph.add_undirected_edge(1, 2, weight=3) - assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5) - assert_equal(graph.nodes[1].adj_weights[graph.nodes[0].key], 5) - assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2) - assert_equal(graph.nodes[5].adj_weights[graph.nodes[0].key], 2) - assert_equal(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[0].adj_weights[graph.nodes[1].key], 5) + self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[0].key], 5) + self.assertEqual(graph.nodes[0].adj_weights[graph.nodes[5].key], 2) + self.assertEqual(graph.nodes[5].adj_weights[graph.nodes[0].key], 2) + self.assertEqual(graph.nodes[1].adj_weights[graph.nodes[2].key], 3) + self.assertEqual(graph.nodes[2].adj_weights[graph.nodes[1].key], 3) print('Success: test_graph_undirected') @@ -70,4 +70,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/graph_bfs/bfs_challenge.ipynb b/graphs_trees/graph_bfs/bfs_challenge.ipynb index c57d51a..fdef510 100644 --- a/graphs_trees/graph_bfs/bfs_challenge.ipynb +++ b/graphs_trees/graph_bfs/bfs_challenge.ipynb @@ -142,12 +142,13 @@ "outputs": [], "source": [ "# %load test_bfs.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestBfs(object):\n", + "class TestBfs(unittest.TestCase):\n", "\n", - " def __init__(self):\n", + " def __init__(self, *args, **kwargs):\n", + " super(TestBfs, self).__init__()\n", " self.results = Results()\n", "\n", " def test_bfs(self):\n", @@ -164,7 +165,7 @@ " graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 4, 8)\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", " print('Success: test_bfs')\n", "\n", @@ -204,7 +205,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.7.2" } }, "nbformat": 4, diff --git a/graphs_trees/graph_bfs/bfs_solution.ipynb b/graphs_trees/graph_bfs/bfs_solution.ipynb index 13fc897..c3449cb 100644 --- a/graphs_trees/graph_bfs/bfs_solution.ipynb +++ b/graphs_trees/graph_bfs/bfs_solution.ipynb @@ -103,9 +103,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../graph/graph.py" @@ -114,9 +112,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from collections import deque\n", @@ -149,9 +145,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../utils/results.py" @@ -160,9 +154,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -174,12 +166,13 @@ ], "source": [ "%%writefile test_bfs.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestBfs(object):\n", + "class TestBfs(unittest.TestCase):\n", "\n", - " def __init__(self):\n", + " def __init__(self, *args, **kwargs):\n", + " super(TestBfs, self).__init__()\n", " self.results = Results()\n", "\n", " def test_bfs(self):\n", @@ -196,7 +189,7 @@ " graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 4, 8)\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", " print('Success: test_bfs')\n", "\n", @@ -213,9 +206,7 @@ { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -246,9 +237,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/graph_bfs/test_bfs.py b/graphs_trees/graph_bfs/test_bfs.py index 359f3fc..22b0f29 100644 --- a/graphs_trees/graph_bfs/test_bfs.py +++ b/graphs_trees/graph_bfs/test_bfs.py @@ -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() def test_bfs(self): @@ -20,7 +21,7 @@ class TestBfs(object): graph.add_edge(3, 2, 7) graph.add_edge(3, 4, 8) 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') @@ -31,4 +32,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/graph_build_order/build_order_challenge.ipynb b/graphs_trees/graph_build_order/build_order_challenge.ipynb index ff75fd9..4ef9028 100644 --- a/graphs_trees/graph_build_order/build_order_challenge.ipynb +++ b/graphs_trees/graph_build_order/build_order_challenge.ipynb @@ -151,13 +151,13 @@ "outputs": [], "source": [ "# %load test_build_order.py\n", - "from nose.tools import assert_equal\n", - "from nose.tools import assert_true\n", + "import unittest\n", "\n", "\n", - "class TestBuildOrder(object):\n", + "class TestBuildOrder(unittest.TestCase):\n", "\n", - " def __init__(self):\n", + " def __init__(self, *args, **kwargs):\n", + " super(TestBuildOrder, self).__init__()\n", " self.dependencies = [\n", " Dependency('d', 'g'),\n", " Dependency('f', 'c'),\n", @@ -175,13 +175,13 @@ "\n", " expected_result0 = ('d', 'f')\n", " expected_result1 = ('c', 'b', 'g')\n", - " assert_true(processed_nodes[0].key in expected_result0)\n", - " assert_true(processed_nodes[1].key in expected_result0)\n", - " assert_true(processed_nodes[2].key in expected_result1)\n", - " assert_true(processed_nodes[3].key in expected_result1)\n", - " assert_true(processed_nodes[4].key in expected_result1)\n", - " assert_true(processed_nodes[5].key is 'a')\n", - " assert_true(processed_nodes[6].key is 'e')\n", + " self.assertTrue(processed_nodes[0].key in expected_result0)\n", + " self.assertTrue(processed_nodes[1].key in expected_result0)\n", + " self.assertTrue(processed_nodes[2].key in expected_result1)\n", + " self.assertTrue(processed_nodes[3].key in expected_result1)\n", + " self.assertTrue(processed_nodes[4].key in expected_result1)\n", + " self.assertTrue(processed_nodes[5].key is 'a')\n", + " self.assertTrue(processed_nodes[6].key is 'e')\n", "\n", " print('Success: test_build_order')\n", "\n", @@ -189,7 +189,7 @@ " self.dependencies.append(Dependency('e', 'f'))\n", " build_order = BuildOrder(self.dependencies)\n", " processed_nodes = build_order.find_build_order()\n", - " assert_true(processed_nodes is None)\n", + " self.assertTrue(processed_nodes is None)\n", "\n", " print('Success: test_build_order_circular')\n", "\n", @@ -230,7 +230,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.7.2" } }, "nbformat": 4, diff --git a/graphs_trees/graph_build_order/build_order_solution.ipynb b/graphs_trees/graph_build_order/build_order_solution.ipynb index 90f2087..80a0a0d 100644 --- a/graphs_trees/graph_build_order/build_order_solution.ipynb +++ b/graphs_trees/graph_build_order/build_order_solution.ipynb @@ -101,9 +101,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "from collections import deque\n", @@ -119,9 +117,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../graph/graph.py" @@ -130,9 +126,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class BuildOrder(object):\n", @@ -187,9 +181,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../utils/results.py" @@ -198,9 +190,7 @@ { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -212,13 +202,13 @@ ], "source": [ "%%writefile test_build_order.py\n", - "from nose.tools import assert_equal\n", - "from nose.tools import assert_true\n", + "import unittest\n", "\n", "\n", - "class TestBuildOrder(object):\n", + "class TestBuildOrder(unittest.TestCase):\n", "\n", - " def __init__(self):\n", + " def __init__(self, *args, **kwargs):\n", + " super(TestBuildOrder, self).__init__()\n", " self.dependencies = [\n", " Dependency('d', 'g'),\n", " Dependency('f', 'c'),\n", @@ -236,13 +226,13 @@ "\n", " expected_result0 = ('d', 'f')\n", " expected_result1 = ('c', 'b', 'g')\n", - " assert_true(processed_nodes[0].key in expected_result0)\n", - " assert_true(processed_nodes[1].key in expected_result0)\n", - " assert_true(processed_nodes[2].key in expected_result1)\n", - " assert_true(processed_nodes[3].key in expected_result1)\n", - " assert_true(processed_nodes[4].key in expected_result1)\n", - " assert_true(processed_nodes[5].key is 'a')\n", - " assert_true(processed_nodes[6].key is 'e')\n", + " self.assertTrue(processed_nodes[0].key in expected_result0)\n", + " self.assertTrue(processed_nodes[1].key in expected_result0)\n", + " self.assertTrue(processed_nodes[2].key in expected_result1)\n", + " self.assertTrue(processed_nodes[3].key in expected_result1)\n", + " self.assertTrue(processed_nodes[4].key in expected_result1)\n", + " self.assertTrue(processed_nodes[5].key is 'a')\n", + " self.assertTrue(processed_nodes[6].key is 'e')\n", "\n", " print('Success: test_build_order')\n", "\n", @@ -250,7 +240,7 @@ " self.dependencies.append(Dependency('e', 'f'))\n", " build_order = BuildOrder(self.dependencies)\n", " processed_nodes = build_order.find_build_order()\n", - " assert_true(processed_nodes is None)\n", + " self.assertTrue(processed_nodes is None)\n", "\n", " print('Success: test_build_order_circular')\n", "\n", @@ -268,9 +258,7 @@ { "cell_type": "code", "execution_count": 6, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -302,9 +290,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/graph_build_order/test_build_order.py b/graphs_trees/graph_build_order/test_build_order.py index 0671951..f12f4d6 100644 --- a/graphs_trees/graph_build_order/test_build_order.py +++ b/graphs_trees/graph_build_order/test_build_order.py @@ -1,10 +1,10 @@ -from nose.tools import assert_equal -from nose.tools import assert_true +import unittest -class TestBuildOrder(object): +class TestBuildOrder(unittest.TestCase): - def __init__(self): + def __init__(self, *args, **kwargs): + super(TestBuildOrder, self).__init__() self.dependencies = [ Dependency('d', 'g'), Dependency('f', 'c'), @@ -22,13 +22,13 @@ class TestBuildOrder(object): expected_result0 = ('d', 'f') expected_result1 = ('c', 'b', 'g') - assert_true(processed_nodes[0].key in expected_result0) - assert_true(processed_nodes[1].key in expected_result0) - assert_true(processed_nodes[2].key in expected_result1) - assert_true(processed_nodes[3].key in expected_result1) - assert_true(processed_nodes[4].key in expected_result1) - assert_true(processed_nodes[5].key is 'a') - assert_true(processed_nodes[6].key is 'e') + self.assertTrue(processed_nodes[0].key in expected_result0) + self.assertTrue(processed_nodes[1].key in expected_result0) + self.assertTrue(processed_nodes[2].key in expected_result1) + self.assertTrue(processed_nodes[3].key in expected_result1) + self.assertTrue(processed_nodes[4].key in expected_result1) + self.assertTrue(processed_nodes[5].key is 'a') + self.assertTrue(processed_nodes[6].key is 'e') print('Success: test_build_order') @@ -36,7 +36,7 @@ class TestBuildOrder(object): self.dependencies.append(Dependency('e', 'f')) build_order = BuildOrder(self.dependencies) 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') @@ -48,4 +48,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/graph_dfs/dfs_challenge.ipynb b/graphs_trees/graph_dfs/dfs_challenge.ipynb index abb36f7..e606bdc 100644 --- a/graphs_trees/graph_dfs/dfs_challenge.ipynb +++ b/graphs_trees/graph_dfs/dfs_challenge.ipynb @@ -101,9 +101,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class GraphDfs(Graph):\n", @@ -141,18 +139,17 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_dfs.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestDfs(object):\n", + "class TestDfs(unittest.TestCase):\n", "\n", - " def __init__(self):\n", + " def __init__(self, *args, **kwargs):\n", + " super(TestDfs, self).__init__()\n", " self.results = Results()\n", "\n", " def test_dfs(self):\n", @@ -169,7 +166,7 @@ " graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 4, 8)\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", " print('Success: test_dfs')\n", "\n", @@ -209,9 +206,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/graph_dfs/dfs_solution.ipynb b/graphs_trees/graph_dfs/dfs_solution.ipynb index 739820d..0936fac 100644 --- a/graphs_trees/graph_dfs/dfs_solution.ipynb +++ b/graphs_trees/graph_dfs/dfs_solution.ipynb @@ -96,9 +96,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../graph/graph.py" @@ -107,9 +105,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class GraphDfs(Graph):\n", @@ -134,9 +130,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../utils/results.py" @@ -145,9 +139,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -159,12 +151,13 @@ ], "source": [ "%%writefile test_dfs.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestDfs(object):\n", + "class TestDfs(unittest.TestCase):\n", "\n", - " def __init__(self):\n", + " def __init__(self, *args, **kwargs):\n", + " super(TestDfs, self).__init__()\n", " self.results = Results()\n", "\n", " def test_dfs(self):\n", @@ -181,7 +174,7 @@ " graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 4, 8)\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", " print('Success: test_dfs')\n", "\n", @@ -198,9 +191,7 @@ { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -231,9 +222,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/graph_dfs/test_dfs.py b/graphs_trees/graph_dfs/test_dfs.py index 8e1c50e..a40496e 100644 --- a/graphs_trees/graph_dfs/test_dfs.py +++ b/graphs_trees/graph_dfs/test_dfs.py @@ -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() def test_dfs(self): @@ -20,7 +21,7 @@ class TestDfs(object): graph.add_edge(3, 2, 7) graph.add_edge(3, 4, 8) 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') @@ -31,4 +32,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/graph_path_exists/path_exists_challenge.ipynb b/graphs_trees/graph_path_exists/path_exists_challenge.ipynb index dce34c0..1f70de1 100644 --- a/graphs_trees/graph_path_exists/path_exists_challenge.ipynb +++ b/graphs_trees/graph_path_exists/path_exists_challenge.ipynb @@ -103,9 +103,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class GraphPathExists(Graph):\n", @@ -132,16 +130,14 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_path_exists.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestPathExists(object):\n", + "class TestPathExists(unittest.TestCase):\n", "\n", " def test_path_exists(self):\n", " nodes = []\n", @@ -157,9 +153,9 @@ " graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 4, 8)\n", "\n", - " assert_equal(graph.path_exists(nodes[0], nodes[2]), True)\n", - " assert_equal(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[0], nodes[2]), True)\n", + " self.assertEqual(graph.path_exists(nodes[0], nodes[0]), True)\n", + " self.assertEqual(graph.path_exists(nodes[4], nodes[5]), False)\n", "\n", " print('Success: test_path_exists')\n", "\n", @@ -199,9 +195,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/graph_path_exists/path_exists_solution.ipynb b/graphs_trees/graph_path_exists/path_exists_solution.ipynb index 3e9e280..af246ba 100644 --- a/graphs_trees/graph_path_exists/path_exists_solution.ipynb +++ b/graphs_trees/graph_path_exists/path_exists_solution.ipynb @@ -107,9 +107,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../graph/graph.py" @@ -118,9 +116,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from collections import deque\n", @@ -157,9 +153,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -171,10 +165,10 @@ ], "source": [ "%%writefile test_path_exists.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestPathExists(object):\n", + "class TestPathExists(unittest.TestCase):\n", "\n", " def test_path_exists(self):\n", " nodes = []\n", @@ -190,9 +184,9 @@ " graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 4, 8)\n", "\n", - " assert_equal(graph.path_exists(nodes[0], nodes[2]), True)\n", - " assert_equal(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[0], nodes[2]), True)\n", + " self.assertEqual(graph.path_exists(nodes[0], nodes[0]), True)\n", + " self.assertEqual(graph.path_exists(nodes[4], nodes[5]), False)\n", "\n", " print('Success: test_path_exists')\n", "\n", @@ -209,9 +203,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -242,9 +234,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/graph_path_exists/test_path_exists.py b/graphs_trees/graph_path_exists/test_path_exists.py index a7374c7..1f81e15 100644 --- a/graphs_trees/graph_path_exists/test_path_exists.py +++ b/graphs_trees/graph_path_exists/test_path_exists.py @@ -1,7 +1,7 @@ -from nose.tools import assert_equal +import unittest -class TestPathExists(object): +class TestPathExists(unittest.TestCase): def test_path_exists(self): nodes = [] @@ -17,9 +17,9 @@ class TestPathExists(object): graph.add_edge(3, 2, 7) graph.add_edge(3, 4, 8) - assert_equal(graph.path_exists(nodes[0], nodes[2]), True) - assert_equal(graph.path_exists(nodes[0], nodes[0]), True) - assert_equal(graph.path_exists(nodes[4], nodes[5]), False) + self.assertEqual(graph.path_exists(nodes[0], nodes[2]), True) + self.assertEqual(graph.path_exists(nodes[0], nodes[0]), True) + self.assertEqual(graph.path_exists(nodes[4], nodes[5]), False) print('Success: test_path_exists') @@ -30,4 +30,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/graph_shortest_path/graph_shortest_path_challenge.ipynb b/graphs_trees/graph_shortest_path/graph_shortest_path_challenge.ipynb index 70e1d26..ae6f8b1 100644 --- a/graphs_trees/graph_shortest_path/graph_shortest_path_challenge.ipynb +++ b/graphs_trees/graph_shortest_path/graph_shortest_path_challenge.ipynb @@ -91,8 +91,8 @@ "graph.add_edge('h', 'g', weight=2)\n", "shortest_path = ShortestPath(graph)\n", "result = shortest_path.find_shortest_path('a', 'i')\n", - "assert_equal(result, ['a', 'c', 'd', 'g', 'i'])\n", - "assert_equal(shortest_path.path_weight['i'], 8)\n", + "self.assertEqual(result, ['a', 'c', 'd', 'g', 'i'])\n", + "self.assertEqual(shortest_path.path_weight['i'], 8)\n", "" ] }, @@ -174,10 +174,10 @@ "outputs": [], "source": [ "# %load test_shortest_path.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestShortestPath(object):\n", + "class TestShortestPath(unittest.TestCase):\n", "\n", " def test_shortest_path(self):\n", " graph = Graph()\n", @@ -202,8 +202,8 @@ " graph.add_edge('h', 'g', weight=2)\n", " shortest_path = ShortestPath(graph)\n", " result = shortest_path.find_shortest_path('a', 'i')\n", - " assert_equal(result, ['a', 'c', 'd', 'g', 'i'])\n", - " assert_equal(shortest_path.path_weight['i'], 8)\n", + " self.assertEqual(result, ['a', 'c', 'd', 'g', 'i'])\n", + " self.assertEqual(shortest_path.path_weight['i'], 8)\n", "\n", " print('Success: test_shortest_path')\n", "\n", @@ -243,7 +243,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.7.2" } }, "nbformat": 4, diff --git a/graphs_trees/graph_shortest_path/graph_shortest_path_solution.ipynb b/graphs_trees/graph_shortest_path/graph_shortest_path_solution.ipynb index 60abfa6..f7aec09 100644 --- a/graphs_trees/graph_shortest_path/graph_shortest_path_solution.ipynb +++ b/graphs_trees/graph_shortest_path/graph_shortest_path_solution.ipynb @@ -90,8 +90,8 @@ "graph.add_edge('h', 'g', weight=2)\n", "shortest_path = ShortestPath(graph)\n", "result = shortest_path.find_shortest_path('a', 'i')\n", - "assert_equal(result, ['a', 'c', 'd', 'g', 'i'])\n", - "assert_equal(shortest_path.path_weight['i'], 8)\n", + "self.assertEqual(result, ['a', 'c', 'd', 'g', 'i'])\n", + "self.assertEqual(shortest_path.path_weight['i'], 8)\n", "" ] }, @@ -154,9 +154,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../../arrays_strings/priority_queue/priority_queue.py" @@ -165,9 +163,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../graph/graph.py" @@ -176,9 +172,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "import sys\n", @@ -254,9 +248,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -268,10 +260,10 @@ ], "source": [ "%%writefile test_shortest_path.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestShortestPath(object):\n", + "class TestShortestPath(unittest.TestCase):\n", "\n", " def test_shortest_path(self):\n", " graph = Graph()\n", @@ -296,8 +288,8 @@ " graph.add_edge('h', 'g', weight=2)\n", " shortest_path = ShortestPath(graph)\n", " result = shortest_path.find_shortest_path('a', 'i')\n", - " assert_equal(result, ['a', 'c', 'd', 'g', 'i'])\n", - " assert_equal(shortest_path.path_weight['i'], 8)\n", + " self.assertEqual(result, ['a', 'c', 'd', 'g', 'i'])\n", + " self.assertEqual(shortest_path.path_weight['i'], 8)\n", "\n", " print('Success: test_shortest_path')\n", "\n", @@ -314,9 +306,7 @@ { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -347,9 +337,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/graph_shortest_path/test_shortest_path.py b/graphs_trees/graph_shortest_path/test_shortest_path.py index 237783b..2fc6e7b 100644 --- a/graphs_trees/graph_shortest_path/test_shortest_path.py +++ b/graphs_trees/graph_shortest_path/test_shortest_path.py @@ -1,7 +1,7 @@ -from nose.tools import assert_equal +import unittest -class TestShortestPath(object): +class TestShortestPath(unittest.TestCase): def test_shortest_path(self): graph = Graph() @@ -26,8 +26,8 @@ class TestShortestPath(object): graph.add_edge('h', 'g', weight=2) shortest_path = ShortestPath(graph) result = shortest_path.find_shortest_path('a', 'i') - assert_equal(result, ['a', 'c', 'd', 'g', 'i']) - assert_equal(shortest_path.path_weight['i'], 8) + self.assertEqual(result, ['a', 'c', 'd', 'g', 'i']) + self.assertEqual(shortest_path.path_weight['i'], 8) print('Success: test_shortest_path') @@ -38,4 +38,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/graph_shortest_path_unweighted/shortest_path_challenge.ipynb b/graphs_trees/graph_shortest_path_unweighted/shortest_path_challenge.ipynb index 58e7765..531abd3 100644 --- a/graphs_trees/graph_shortest_path_unweighted/shortest_path_challenge.ipynb +++ b/graphs_trees/graph_shortest_path_unweighted/shortest_path_challenge.ipynb @@ -111,9 +111,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class GraphShortestPath(Graph):\n", @@ -140,16 +138,14 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_shortest_path.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestShortestPath(object):\n", + "class TestShortestPath(unittest.TestCase):\n", "\n", " def test_shortest_path(self):\n", " nodes = []\n", @@ -165,9 +161,9 @@ " graph.add_edge(3, 2)\n", " graph.add_edge(3, 4)\n", "\n", - " assert_equal(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", - " assert_equal(graph.shortest_path(nodes[4].key, nodes[5].key), None)\n", + " self.assertEqual(graph.shortest_path(nodes[0].key, nodes[2].key), [0, 1, 3, 2])\n", + " self.assertEqual(graph.shortest_path(nodes[0].key, nodes[0].key), [0])\n", + " self.assertEqual(graph.shortest_path(nodes[4].key, nodes[5].key), None)\n", "\n", " print('Success: test_shortest_path')\n", "\n", @@ -207,9 +203,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/graph_shortest_path_unweighted/shortest_path_solution.ipynb b/graphs_trees/graph_shortest_path_unweighted/shortest_path_solution.ipynb index 1e1bc13..3edbcf5 100644 --- a/graphs_trees/graph_shortest_path_unweighted/shortest_path_solution.ipynb +++ b/graphs_trees/graph_shortest_path_unweighted/shortest_path_solution.ipynb @@ -116,9 +116,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../graph/graph.py" @@ -127,9 +125,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "from collections import deque\n", @@ -181,9 +177,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -195,10 +189,10 @@ ], "source": [ "%%writefile test_shortest_path.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestShortestPath(object):\n", + "class TestShortestPath(unittest.TestCase):\n", "\n", " def test_shortest_path(self):\n", " nodes = []\n", @@ -214,9 +208,9 @@ " graph.add_edge(3, 2)\n", " graph.add_edge(3, 4)\n", "\n", - " assert_equal(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", - " assert_equal(graph.shortest_path(nodes[4].key, nodes[5].key), None)\n", + " self.assertEqual(graph.shortest_path(nodes[0].key, nodes[2].key), [0, 1, 3, 2])\n", + " self.assertEqual(graph.shortest_path(nodes[0].key, nodes[0].key), [0])\n", + " self.assertEqual(graph.shortest_path(nodes[4].key, nodes[5].key), None)\n", "\n", " print('Success: test_shortest_path')\n", "\n", @@ -233,9 +227,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -266,9 +258,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/graph_shortest_path_unweighted/test_shortest_path.py b/graphs_trees/graph_shortest_path_unweighted/test_shortest_path.py index 83f61d4..8740fe2 100644 --- a/graphs_trees/graph_shortest_path_unweighted/test_shortest_path.py +++ b/graphs_trees/graph_shortest_path_unweighted/test_shortest_path.py @@ -1,7 +1,7 @@ -from nose.tools import assert_equal +import unittest -class TestShortestPath(object): +class TestShortestPath(unittest.TestCase): def test_shortest_path(self): nodes = [] @@ -17,9 +17,9 @@ class TestShortestPath(object): graph.add_edge(3, 2) graph.add_edge(3, 4) - assert_equal(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]) - assert_equal(graph.shortest_path(nodes[4].key, nodes[5].key), None) + self.assertEqual(graph.shortest_path(nodes[0].key, nodes[2].key), [0, 1, 3, 2]) + self.assertEqual(graph.shortest_path(nodes[0].key, nodes[0].key), [0]) + self.assertEqual(graph.shortest_path(nodes[4].key, nodes[5].key), None) print('Success: test_shortest_path') @@ -30,4 +30,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/invert_tree/invert_tree_challenge.ipynb b/graphs_trees/invert_tree/invert_tree_challenge.ipynb index fb18efb..2270d65 100644 --- a/graphs_trees/invert_tree/invert_tree_challenge.ipynb +++ b/graphs_trees/invert_tree/invert_tree_challenge.ipynb @@ -128,10 +128,10 @@ "outputs": [], "source": [ "# %load test_invert_tree.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestInvertTree(object):\n", + "class TestInvertTree(unittest.TestCase):\n", "\n", " def test_invert_tree(self):\n", " root = Node(5)\n", @@ -143,13 +143,13 @@ " node6 = bst.insert(6)\n", " node9 = bst.insert(9)\n", " result = bst.invert_tree()\n", - " assert_equal(result, root)\n", - " assert_equal(result.left, node7)\n", - " assert_equal(result.right, node2)\n", - " assert_equal(result.left.left, node9)\n", - " assert_equal(result.left.right, node6)\n", - " assert_equal(result.right.left, node3)\n", - " assert_equal(result.right.right, node1)\n", + " self.assertEqual(result, root)\n", + " self.assertEqual(result.left, node7)\n", + " self.assertEqual(result.right, node2)\n", + " self.assertEqual(result.left.left, node9)\n", + " self.assertEqual(result.left.right, node6)\n", + " self.assertEqual(result.right.left, node3)\n", + " self.assertEqual(result.right.right, node1)\n", " print('Success: test_invert_tree')\n", "\n", "\n", @@ -188,7 +188,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.7.2" } }, "nbformat": 4, diff --git a/graphs_trees/invert_tree/invert_tree_solution.ipynb b/graphs_trees/invert_tree/invert_tree_solution.ipynb index 3037e57..03c05d9 100644 --- a/graphs_trees/invert_tree/invert_tree_solution.ipynb +++ b/graphs_trees/invert_tree/invert_tree_solution.ipynb @@ -95,9 +95,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "%run ../bst/bst.py" @@ -106,9 +104,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class InverseBst(Bst):\n", @@ -137,9 +133,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -151,10 +145,10 @@ ], "source": [ "%%writefile test_invert_tree.py\n", - "from nose.tools import assert_equal, assert_raises\n", + "import unittest\n", "\n", "\n", - "class TestInvertTree(object):\n", + "class TestInvertTree(unittest.TestCase):\n", "\n", " def test_invert_tree(self):\n", " root = Node(5)\n", @@ -166,13 +160,13 @@ " node6 = bst.insert(6)\n", " node9 = bst.insert(9)\n", " result = bst.invert_tree()\n", - " assert_equal(result, root)\n", - " assert_equal(result.left, node7)\n", - " assert_equal(result.right, node2)\n", - " assert_equal(result.left.left, node9)\n", - " assert_equal(result.left.right, node6)\n", - " assert_equal(result.right.left, node3)\n", - " assert_equal(result.right.right, node1)\n", + " self.assertEqual(result, root)\n", + " self.assertEqual(result.left, node7)\n", + " self.assertEqual(result.right, node2)\n", + " self.assertEqual(result.left.left, node9)\n", + " self.assertEqual(result.left.right, node6)\n", + " self.assertEqual(result.right.left, node3)\n", + " self.assertEqual(result.right.right, node1)\n", " print('Success: test_invert_tree')\n", "\n", "\n", @@ -188,9 +182,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -221,9 +213,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/invert_tree/test_invert_tree.py b/graphs_trees/invert_tree/test_invert_tree.py index ab33458..b2608d2 100644 --- a/graphs_trees/invert_tree/test_invert_tree.py +++ b/graphs_trees/invert_tree/test_invert_tree.py @@ -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): root = Node(5) @@ -13,13 +13,13 @@ class TestInvertTree(object): node6 = bst.insert(6) node9 = bst.insert(9) result = bst.invert_tree() - assert_equal(result, root) - assert_equal(result.left, node7) - assert_equal(result.right, node2) - assert_equal(result.left.left, node9) - assert_equal(result.left.right, node6) - assert_equal(result.right.left, node3) - assert_equal(result.right.right, node1) + self.assertEqual(result, root) + self.assertEqual(result.left, node7) + self.assertEqual(result.right, node2) + self.assertEqual(result.left.left, node9) + self.assertEqual(result.left.right, node6) + self.assertEqual(result.right.left, node3) + self.assertEqual(result.right.right, node1) print('Success: test_invert_tree') @@ -29,4 +29,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/min_heap/min_heap.py b/graphs_trees/min_heap/min_heap.py index 81c0d37..77e1a60 100644 --- a/graphs_trees/min_heap/min_heap.py +++ b/graphs_trees/min_heap/min_heap.py @@ -29,12 +29,12 @@ class MinHeap(object): if key is None: raise TypeError('key cannot be None') 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): if index == 0: return - index_parent = (index-1) // 2 + index_parent = (index - 1) // 2 if self.array[index] < self.array[index_parent]: # Swap the indices and recurse self.array[index], self.array[index_parent] = \ @@ -54,13 +54,17 @@ class MinHeap(object): def _find_smaller_child(self, index): left_child_index = 2 * index + 1 right_child_index = 2 * index + 2 + # No right child if right_child_index >= len(self.array): + # No left child if left_child_index >= len(self.array): return -1 + # Left child only else: return left_child_index else: + # Compare left and right children if self.array[left_child_index] < self.array[right_child_index]: return left_child_index else: - return right_child_index \ No newline at end of file + return right_child_index diff --git a/graphs_trees/min_heap/min_heap_challenge.ipynb b/graphs_trees/min_heap/min_heap_challenge.ipynb index 88ed925..ef46386 100644 --- a/graphs_trees/min_heap/min_heap_challenge.ipynb +++ b/graphs_trees/min_heap/min_heap_challenge.ipynb @@ -95,9 +95,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class MinHeap(object):\n", @@ -140,34 +138,32 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_min_heap.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestMinHeap(object):\n", + "class TestMinHeap(unittest.TestCase):\n", "\n", " def test_min_heap(self):\n", " heap = MinHeap()\n", - " assert_equal(heap.peek_min(), None)\n", - " assert_equal(heap.extract_min(), None)\n", + " self.assertEqual(heap.peek_min(), None)\n", + " self.assertEqual(heap.extract_min(), None)\n", " heap.insert(20)\n", - " assert_equal(heap.peek_min(), 20)\n", + " self.assertEqual(heap.peek_min(), 20)\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(22)\n", " heap.insert(40)\n", " heap.insert(5)\n", - " assert_equal(heap.peek_min(), 5)\n", + " self.assertEqual(heap.peek_min(), 5)\n", " heap.insert(3)\n", - " assert_equal(heap.peek_min(), 3)\n", - " assert_equal(heap.extract_min(), 3)\n", - " assert_equal(heap.peek_min(), 5)\n", + " self.assertEqual(heap.peek_min(), 3)\n", + " self.assertEqual(heap.extract_min(), 3)\n", + " self.assertEqual(heap.peek_min(), 5)\n", " print('Success: test_min_heap')\n", "\n", " \n", @@ -206,9 +202,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/min_heap/min_heap_solution.ipynb b/graphs_trees/min_heap/min_heap_solution.ipynb index f886d51..70b91b3 100644 --- a/graphs_trees/min_heap/min_heap_solution.ipynb +++ b/graphs_trees/min_heap/min_heap_solution.ipynb @@ -194,9 +194,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -283,9 +281,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run min_heap.py" @@ -301,9 +297,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -315,46 +309,46 @@ ], "source": [ "%%writefile test_min_heap.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestMinHeap(object):\n", + "class TestMinHeap(unittest.TestCase):\n", "\n", " def test_min_heap(self):\n", " heap = MinHeap()\n", - " assert_equal(heap.peek_min(), None)\n", - " assert_equal(heap.extract_min(), None)\n", + " self.assertEqual(heap.peek_min(), None)\n", + " self.assertEqual(heap.extract_min(), None)\n", " heap.insert(20)\n", - " assert_equal(heap.array[0], 20)\n", + " self.assertEqual(heap.array[0], 20)\n", " heap.insert(5)\n", - " assert_equal(heap.array[0], 5)\n", - " assert_equal(heap.array[1], 20)\n", + " self.assertEqual(heap.array[0], 5)\n", + " self.assertEqual(heap.array[1], 20)\n", " heap.insert(15)\n", - " assert_equal(heap.array[0], 5)\n", - " assert_equal(heap.array[1], 20)\n", - " assert_equal(heap.array[2], 15)\n", + " self.assertEqual(heap.array[0], 5)\n", + " self.assertEqual(heap.array[1], 20)\n", + " self.assertEqual(heap.array[2], 15)\n", " heap.insert(22)\n", - " assert_equal(heap.array[0], 5)\n", - " assert_equal(heap.array[1], 20)\n", - " assert_equal(heap.array[2], 15)\n", - " assert_equal(heap.array[3], 22)\n", + " self.assertEqual(heap.array[0], 5)\n", + " self.assertEqual(heap.array[1], 20)\n", + " self.assertEqual(heap.array[2], 15)\n", + " self.assertEqual(heap.array[3], 22)\n", " heap.insert(40)\n", - " assert_equal(heap.array[0], 5)\n", - " assert_equal(heap.array[1], 20)\n", - " assert_equal(heap.array[2], 15)\n", - " assert_equal(heap.array[3], 22)\n", - " assert_equal(heap.array[4], 40)\n", + " self.assertEqual(heap.array[0], 5)\n", + " self.assertEqual(heap.array[1], 20)\n", + " self.assertEqual(heap.array[2], 15)\n", + " self.assertEqual(heap.array[3], 22)\n", + " self.assertEqual(heap.array[4], 40)\n", " heap.insert(3)\n", - " assert_equal(heap.array[0], 3)\n", - " assert_equal(heap.array[1], 20)\n", - " assert_equal(heap.array[2], 5)\n", - " assert_equal(heap.array[3], 22)\n", - " assert_equal(heap.array[4], 40)\n", - " assert_equal(heap.array[5], 15)\n", + " self.assertEqual(heap.array[0], 3)\n", + " self.assertEqual(heap.array[1], 20)\n", + " self.assertEqual(heap.array[2], 5)\n", + " self.assertEqual(heap.array[3], 22)\n", + " self.assertEqual(heap.array[4], 40)\n", + " self.assertEqual(heap.array[5], 15)\n", " mins = []\n", " while heap:\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", "\n", " \n", @@ -370,9 +364,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -403,9 +395,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/min_heap/test_min_heap.py b/graphs_trees/min_heap/test_min_heap.py index 103724d..02a744a 100644 --- a/graphs_trees/min_heap/test_min_heap.py +++ b/graphs_trees/min_heap/test_min_heap.py @@ -1,43 +1,43 @@ -from nose.tools import assert_equal +import unittest -class TestMinHeap(object): +class TestMinHeap(unittest.TestCase): def test_min_heap(self): heap = MinHeap() - assert_equal(heap.peek_min(), None) - assert_equal(heap.extract_min(), None) + self.assertEqual(heap.peek_min(), None) + self.assertEqual(heap.extract_min(), None) heap.insert(20) - assert_equal(heap.array[0], 20) + self.assertEqual(heap.array[0], 20) heap.insert(5) - assert_equal(heap.array[0], 5) - assert_equal(heap.array[1], 20) + self.assertEqual(heap.array[0], 5) + self.assertEqual(heap.array[1], 20) heap.insert(15) - assert_equal(heap.array[0], 5) - assert_equal(heap.array[1], 20) - assert_equal(heap.array[2], 15) + self.assertEqual(heap.array[0], 5) + self.assertEqual(heap.array[1], 20) + self.assertEqual(heap.array[2], 15) heap.insert(22) - assert_equal(heap.array[0], 5) - assert_equal(heap.array[1], 20) - assert_equal(heap.array[2], 15) - assert_equal(heap.array[3], 22) + self.assertEqual(heap.array[0], 5) + self.assertEqual(heap.array[1], 20) + self.assertEqual(heap.array[2], 15) + self.assertEqual(heap.array[3], 22) heap.insert(40) - assert_equal(heap.array[0], 5) - assert_equal(heap.array[1], 20) - assert_equal(heap.array[2], 15) - assert_equal(heap.array[3], 22) - assert_equal(heap.array[4], 40) + self.assertEqual(heap.array[0], 5) + self.assertEqual(heap.array[1], 20) + self.assertEqual(heap.array[2], 15) + self.assertEqual(heap.array[3], 22) + self.assertEqual(heap.array[4], 40) heap.insert(3) - assert_equal(heap.array[0], 3) - assert_equal(heap.array[1], 20) - assert_equal(heap.array[2], 5) - assert_equal(heap.array[3], 22) - assert_equal(heap.array[4], 40) - assert_equal(heap.array[5], 15) + self.assertEqual(heap.array[0], 3) + self.assertEqual(heap.array[1], 20) + self.assertEqual(heap.array[2], 5) + self.assertEqual(heap.array[3], 22) + self.assertEqual(heap.array[4], 40) + self.assertEqual(heap.array[5], 15) mins = [] while heap: 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') @@ -47,4 +47,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/tree_bfs/bfs_challenge.ipynb b/graphs_trees/tree_bfs/bfs_challenge.ipynb index 2a3e062..7df77ff 100644 --- a/graphs_trees/tree_bfs/bfs_challenge.ipynb +++ b/graphs_trees/tree_bfs/bfs_challenge.ipynb @@ -82,9 +82,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class BstBfs(Bst):\n", @@ -115,18 +113,17 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_bfs.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestBfs(object):\n", + "class TestBfs(unittest.TestCase):\n", "\n", - " def __init__(self):\n", + " def __init__(self, *args, **kwargs):\n", + " super(TestBfs, self).__init__()\n", " self.results = Results()\n", "\n", " def test_bfs(self):\n", @@ -136,7 +133,7 @@ " bst.insert(1)\n", " bst.insert(3)\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", " print('Success: test_bfs')\n", "\n", @@ -176,9 +173,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/tree_bfs/bfs_solution.ipynb b/graphs_trees/tree_bfs/bfs_solution.ipynb index f96815f..2000177 100644 --- a/graphs_trees/tree_bfs/bfs_solution.ipynb +++ b/graphs_trees/tree_bfs/bfs_solution.ipynb @@ -78,9 +78,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../bst/bst.py" @@ -89,9 +87,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from collections import deque\n", @@ -123,9 +119,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "%run ../utils/results.py" @@ -134,9 +128,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -148,12 +140,13 @@ ], "source": [ "%%writefile test_bfs.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestBfs(object):\n", + "class TestBfs(unittest.TestCase):\n", "\n", - " def __init__(self):\n", + " def __init__(self, *args, **kwargs):\n", + " super(TestBfs, self).__init__()\n", " self.results = Results()\n", "\n", " def test_bfs(self):\n", @@ -163,7 +156,7 @@ " bst.insert(1)\n", " bst.insert(3)\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", " print('Success: test_bfs')\n", "\n", @@ -180,9 +173,7 @@ { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -213,9 +204,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/tree_bfs/test_bfs.py b/graphs_trees/tree_bfs/test_bfs.py index ed0ba6d..9c5046b 100644 --- a/graphs_trees/tree_bfs/test_bfs.py +++ b/graphs_trees/tree_bfs/test_bfs.py @@ -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() def test_bfs(self): @@ -13,7 +14,7 @@ class TestBfs(object): bst.insert(1) bst.insert(3) 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') @@ -24,4 +25,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/tree_dfs/dfs_challenge.ipynb b/graphs_trees/tree_dfs/dfs_challenge.ipynb index 8498e7e..629de37 100644 --- a/graphs_trees/tree_dfs/dfs_challenge.ipynb +++ b/graphs_trees/tree_dfs/dfs_challenge.ipynb @@ -82,21 +82,59 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "%run ../bst/bst.py\n", - "%load ../bst/bst.py" + "# %load ../bst/bst.py\n", + "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", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class BstDfs(Bst):\n", @@ -124,9 +162,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../utils/results.py" @@ -135,18 +171,17 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_dfs.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestDfs(object):\n", + "class TestDfs(unittest.TestCase):\n", "\n", - " def __init__(self):\n", + " def __init__(self, *args, **kwargs):\n", + " super(TestDfs, self).__init__()\n", " self.results = Results()\n", "\n", " def test_dfs(self):\n", @@ -157,15 +192,15 @@ " bst.insert(3)\n", "\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", "\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", "\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", "\n", " bst = BstDfs(Node(1))\n", @@ -175,15 +210,15 @@ " bst.insert(5)\n", "\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", "\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", "\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", " print('Success: test_dfs')\n", "\n", @@ -223,9 +258,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/tree_dfs/dfs_solution.ipynb b/graphs_trees/tree_dfs/dfs_solution.ipynb index 1bda80d..0c1eea8 100644 --- a/graphs_trees/tree_dfs/dfs_solution.ipynb +++ b/graphs_trees/tree_dfs/dfs_solution.ipynb @@ -116,9 +116,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../bst/bst.py" @@ -127,9 +125,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class BstDfs(Bst):\n", @@ -163,9 +159,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../utils/results.py" @@ -174,9 +168,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -188,12 +180,13 @@ ], "source": [ "%%writefile test_dfs.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestDfs(object):\n", + "class TestDfs(unittest.TestCase):\n", "\n", - " def __init__(self):\n", + " def __init__(self, *args, **kwargs):\n", + " super(TestDfs, self).__init__()\n", " self.results = Results()\n", "\n", " def test_dfs(self):\n", @@ -204,15 +197,15 @@ " bst.insert(3)\n", "\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", "\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", "\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", "\n", " bst = BstDfs(Node(1))\n", @@ -222,15 +215,15 @@ " bst.insert(5)\n", "\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", "\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", "\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", " print('Success: test_dfs')\n", "\n", @@ -247,9 +240,7 @@ { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -280,9 +271,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/tree_dfs/test_dfs.py b/graphs_trees/tree_dfs/test_dfs.py index 25966d4..d9db5b9 100644 --- a/graphs_trees/tree_dfs/test_dfs.py +++ b/graphs_trees/tree_dfs/test_dfs.py @@ -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() def test_dfs(self): @@ -14,15 +15,15 @@ class TestDfs(object): bst.insert(3) 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() 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() 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() bst = BstDfs(Node(1)) @@ -32,15 +33,15 @@ class TestDfs(object): bst.insert(5) 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() 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() 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') @@ -51,4 +52,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/tree_height/height_challenge.ipynb b/graphs_trees/tree_height/height_challenge.ipynb index f23465d..90921fd 100644 --- a/graphs_trees/tree_height/height_challenge.ipynb +++ b/graphs_trees/tree_height/height_challenge.ipynb @@ -83,9 +83,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class BstHeight(Bst):\n", @@ -112,25 +110,23 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_height.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestHeight(object):\n", + "class TestHeight(unittest.TestCase):\n", "\n", " def test_height(self):\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(8)\n", " bst.insert(1)\n", " bst.insert(3)\n", - " assert_equal(bst.height(bst.root), 3)\n", + " self.assertEqual(bst.height(bst.root), 3)\n", "\n", " print('Success: test_height')\n", "\n", @@ -170,9 +166,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/tree_height/height_solution.ipynb b/graphs_trees/tree_height/height_solution.ipynb index 3a70b93..966abef 100644 --- a/graphs_trees/tree_height/height_solution.ipynb +++ b/graphs_trees/tree_height/height_solution.ipynb @@ -77,9 +77,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../bst/bst.py" @@ -88,9 +86,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class BstHeight(Bst):\n", @@ -112,9 +108,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -126,19 +120,19 @@ ], "source": [ "%%writefile test_height.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestHeight(object):\n", + "class TestHeight(unittest.TestCase):\n", "\n", " def test_height(self):\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(8)\n", " bst.insert(1)\n", " bst.insert(3)\n", - " assert_equal(bst.height(bst.root), 3)\n", + " self.assertEqual(bst.height(bst.root), 3)\n", "\n", " print('Success: test_height')\n", "\n", @@ -155,9 +149,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -188,9 +180,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/tree_height/test_height.py b/graphs_trees/tree_height/test_height.py index fbb45cb..4dcf37a 100644 --- a/graphs_trees/tree_height/test_height.py +++ b/graphs_trees/tree_height/test_height.py @@ -1,16 +1,16 @@ -from nose.tools import assert_equal +import unittest -class TestHeight(object): +class TestHeight(unittest.TestCase): def test_height(self): bst = BstHeight(Node(5)) - assert_equal(bst.height(bst.root), 1) + self.assertEqual(bst.height(bst.root), 1) bst.insert(2) bst.insert(8) bst.insert(1) bst.insert(3) - assert_equal(bst.height(bst.root), 3) + self.assertEqual(bst.height(bst.root), 3) print('Success: test_height') @@ -21,4 +21,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/tree_lca/test_lca.py b/graphs_trees/tree_lca/test_lca.py index 1a25af3..e322aa0 100644 --- a/graphs_trees/tree_lca/test_lca.py +++ b/graphs_trees/tree_lca/test_lca.py @@ -1,7 +1,7 @@ -from nose.tools import assert_equal +import unittest -class TestLowestCommonAncestor(object): +class TestLowestCommonAncestor(unittest.TestCase): def test_lca(self): node10 = Node(10) @@ -26,13 +26,13 @@ class TestLowestCommonAncestor(object): root = node10 node0 = Node(0) binary_tree = BinaryTree() - assert_equal(binary_tree.lca(root, node0, node5), None) - assert_equal(binary_tree.lca(root, node5, node0), None) - assert_equal(binary_tree.lca(root, node1, node8), node3) - assert_equal(binary_tree.lca(root, node12, node8), node5) - assert_equal(binary_tree.lca(root, node12, node40), node10) - assert_equal(binary_tree.lca(root, node9, node20), node9) - assert_equal(binary_tree.lca(root, node3, node5), node5) + self.assertEqual(binary_tree.lca(root, node0, node5), None) + self.assertEqual(binary_tree.lca(root, node5, node0), None) + self.assertEqual(binary_tree.lca(root, node1, node8), node3) + self.assertEqual(binary_tree.lca(root, node12, node8), node5) + self.assertEqual(binary_tree.lca(root, node12, node40), node10) + self.assertEqual(binary_tree.lca(root, node9, node20), node9) + self.assertEqual(binary_tree.lca(root, node3, node5), node5) print('Success: test_lca') @@ -42,4 +42,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/tree_lca/tree_lca_challenge.ipynb b/graphs_trees/tree_lca/tree_lca_challenge.ipynb index e5f2549..0f799a0 100644 --- a/graphs_trees/tree_lca/tree_lca_challenge.ipynb +++ b/graphs_trees/tree_lca/tree_lca_challenge.ipynb @@ -136,10 +136,10 @@ "outputs": [], "source": [ "# %load test_lca.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestLowestCommonAncestor(object):\n", + "class TestLowestCommonAncestor(unittest.TestCase):\n", "\n", " def test_lca(self):\n", " node10 = Node(10)\n", @@ -164,13 +164,13 @@ " root = node10\n", " node0 = Node(0)\n", " binary_tree = BinaryTree()\n", - " assert_equal(binary_tree.lca(root, node0, node5), None)\n", - " assert_equal(binary_tree.lca(root, node5, node0), None)\n", - " assert_equal(binary_tree.lca(root, node1, node8), node3)\n", - " assert_equal(binary_tree.lca(root, node12, node8), node5)\n", - " assert_equal(binary_tree.lca(root, node12, node40), node10)\n", - " assert_equal(binary_tree.lca(root, node9, node20), node9)\n", - " assert_equal(binary_tree.lca(root, node3, node5), node5)\n", + " self.assertEqual(binary_tree.lca(root, node0, node5), None)\n", + " self.assertEqual(binary_tree.lca(root, node5, node0), None)\n", + " self.assertEqual(binary_tree.lca(root, node1, node8), node3)\n", + " self.assertEqual(binary_tree.lca(root, node12, node8), node5)\n", + " self.assertEqual(binary_tree.lca(root, node12, node40), node10)\n", + " self.assertEqual(binary_tree.lca(root, node9, node20), node9)\n", + " self.assertEqual(binary_tree.lca(root, node3, node5), node5)\n", " print('Success: test_lca')\n", "\n", "\n", @@ -209,7 +209,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.7.2" } }, "nbformat": 4, diff --git a/graphs_trees/tree_lca/tree_lca_solution.ipynb b/graphs_trees/tree_lca/tree_lca_solution.ipynb index 4fc621f..6692cab 100644 --- a/graphs_trees/tree_lca/tree_lca_solution.ipynb +++ b/graphs_trees/tree_lca/tree_lca_solution.ipynb @@ -96,9 +96,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "class Node(object):\n", @@ -115,9 +113,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class BinaryTree(object):\n", @@ -155,9 +151,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class LcaResult(object):\n", @@ -209,9 +203,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -223,10 +215,10 @@ ], "source": [ "%%writefile test_lca.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestLowestCommonAncestor(object):\n", + "class TestLowestCommonAncestor(unittest.TestCase):\n", "\n", " def test_lca(self):\n", " node10 = Node(10)\n", @@ -251,13 +243,13 @@ " root = node10\n", " node0 = Node(0)\n", " binary_tree = BinaryTree()\n", - " assert_equal(binary_tree.lca(root, node0, node5), None)\n", - " assert_equal(binary_tree.lca(root, node5, node0), None)\n", - " assert_equal(binary_tree.lca(root, node1, node8), node3)\n", - " assert_equal(binary_tree.lca(root, node12, node8), node5)\n", - " assert_equal(binary_tree.lca(root, node12, node40), node10)\n", - " assert_equal(binary_tree.lca(root, node9, node20), node9)\n", - " assert_equal(binary_tree.lca(root, node3, node5), node5)\n", + " self.assertEqual(binary_tree.lca(root, node0, node5), None)\n", + " self.assertEqual(binary_tree.lca(root, node5, node0), None)\n", + " self.assertEqual(binary_tree.lca(root, node1, node8), node3)\n", + " self.assertEqual(binary_tree.lca(root, node12, node8), node5)\n", + " self.assertEqual(binary_tree.lca(root, node12, node40), node10)\n", + " self.assertEqual(binary_tree.lca(root, node9, node20), node9)\n", + " self.assertEqual(binary_tree.lca(root, node3, node5), node5)\n", " print('Success: test_lca')\n", "\n", "\n", @@ -273,9 +265,7 @@ { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -306,9 +296,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/tree_level_lists/test_tree_level_lists.py b/graphs_trees/tree_level_lists/test_tree_level_lists.py index 2fe0f7f..89e7567 100644 --- a/graphs_trees/tree_level_lists/test_tree_level_lists.py +++ b/graphs_trees/tree_level_lists/test_tree_level_lists.py @@ -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): bst = BstLevelLists(Node(5)) @@ -24,11 +24,11 @@ class TestTreeLevelLists(object): results.add_result(node) results_list.append(results) - assert_equal(str(results_list[0]), '[5]') - assert_equal(str(results_list[1]), '[3, 8]') - assert_equal(str(results_list[2]), '[2, 4, 7, 9]') - assert_equal(str(results_list[3]), '[1, 6, 10]') - assert_equal(str(results_list[4]), '[11]') + self.assertEqual(str(results_list[0]), '[5]') + self.assertEqual(str(results_list[1]), '[3, 8]') + self.assertEqual(str(results_list[2]), '[2, 4, 7, 9]') + self.assertEqual(str(results_list[3]), '[1, 6, 10]') + self.assertEqual(str(results_list[4]), '[11]') print('Success: test_tree_level_lists') @@ -39,4 +39,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/tree_level_lists/tree_level_lists_challenge.ipynb b/graphs_trees/tree_level_lists/tree_level_lists_challenge.ipynb index 5e3db80..621872b 100644 --- a/graphs_trees/tree_level_lists/tree_level_lists_challenge.ipynb +++ b/graphs_trees/tree_level_lists/tree_level_lists_challenge.ipynb @@ -86,9 +86,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class BstLevelLists(Bst):\n", @@ -126,16 +124,14 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_tree_level_lists.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestTreeLevelLists(object):\n", + "class TestTreeLevelLists(unittest.TestCase):\n", "\n", " def test_tree_level_lists(self):\n", " bst = BstLevelLists(Node(5))\n", @@ -158,11 +154,11 @@ " results.add_result(node)\n", " results_list.append(results)\n", "\n", - " assert_equal(str(results_list[0]), '[5]')\n", - " assert_equal(str(results_list[1]), '[3, 8]')\n", - " assert_equal(str(results_list[2]), '[2, 4, 7, 9]')\n", - " assert_equal(str(results_list[3]), '[1, 6, 10]')\n", - " assert_equal(str(results_list[4]), '[11]')\n", + " self.assertEqual(str(results_list[0]), '[5]')\n", + " self.assertEqual(str(results_list[1]), '[3, 8]')\n", + " self.assertEqual(str(results_list[2]), '[2, 4, 7, 9]')\n", + " self.assertEqual(str(results_list[3]), '[1, 6, 10]')\n", + " self.assertEqual(str(results_list[4]), '[11]')\n", "\n", " print('Success: test_tree_level_lists')\n", "\n", @@ -202,9 +198,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/tree_level_lists/tree_level_lists_solution.ipynb b/graphs_trees/tree_level_lists/tree_level_lists_solution.ipynb index 2ffc3b4..fee7f77 100644 --- a/graphs_trees/tree_level_lists/tree_level_lists_solution.ipynb +++ b/graphs_trees/tree_level_lists/tree_level_lists_solution.ipynb @@ -87,9 +87,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../bst/bst.py" @@ -98,9 +96,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class BstLevelLists(Bst):\n", @@ -134,9 +130,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "%run ../utils/results.py" @@ -145,9 +139,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -159,10 +151,10 @@ ], "source": [ "%%writefile test_tree_level_lists.py\n", - "from nose.tools import assert_equal\n", + "import unittest\n", "\n", "\n", - "class TestTreeLevelLists(object):\n", + "class TestTreeLevelLists(unittest.TestCase):\n", "\n", " def test_tree_level_lists(self):\n", " bst = BstLevelLists(Node(5))\n", @@ -185,11 +177,11 @@ " results.add_result(node)\n", " results_list.append(results)\n", "\n", - " assert_equal(str(results_list[0]), '[5]')\n", - " assert_equal(str(results_list[1]), '[3, 8]')\n", - " assert_equal(str(results_list[2]), '[2, 4, 7, 9]')\n", - " assert_equal(str(results_list[3]), '[1, 6, 10]')\n", - " assert_equal(str(results_list[4]), '[11]')\n", + " self.assertEqual(str(results_list[0]), '[5]')\n", + " self.assertEqual(str(results_list[1]), '[3, 8]')\n", + " self.assertEqual(str(results_list[2]), '[2, 4, 7, 9]')\n", + " self.assertEqual(str(results_list[3]), '[1, 6, 10]')\n", + " self.assertEqual(str(results_list[4]), '[11]')\n", "\n", " print('Success: test_tree_level_lists')\n", "\n", @@ -206,9 +198,7 @@ { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -239,9 +229,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/trie/test_trie.py b/graphs_trees/trie/test_trie.py index a0c1bdf..079d175 100644 --- a/graphs_trees/trie/test_trie.py +++ b/graphs_trees/trie/test_trie.py @@ -1,8 +1,7 @@ -from nose.tools import assert_true -from nose.tools import raises +import unittest -class TestTrie(object): +class TestTrie(unittest.TestCase): def test_trie(self): trie = Trie() @@ -13,7 +12,7 @@ class TestTrie(object): for word in words: trie.insert(word) 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') trie.remove('me') @@ -21,9 +20,9 @@ class TestTrie(object): words = ['a', 'at', 'has', 'hat', 'he', 'men', 'mens', 'met'] 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: - assert_true(trie.find(word) is None) + self.assertTrue(trie.find(word) is None) print('Test: Remove mens') trie.remove('mens') @@ -31,9 +30,9 @@ class TestTrie(object): words = ['a', 'at', 'has', 'hat', 'he', 'men', 'met'] 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: - assert_true(trie.find(word) is None) + self.assertTrue(trie.find(word) is None) print('Test: Remove a') trie.remove('a') @@ -41,9 +40,9 @@ class TestTrie(object): words = ['at', 'has', 'hat', 'he', 'men', 'met'] 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: - assert_true(trie.find(word) is None) + self.assertTrue(trie.find(word) is None) print('Test: Remove has') trie.remove('has') @@ -51,24 +50,23 @@ class TestTrie(object): words = ['at', 'hat', 'he', 'men', 'met'] 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: - assert_true(trie.find(word) is None) + self.assertTrue(trie.find(word) is None) print('Success: test_trie') - @raises(Exception) def test_trie_remove_invalid(self): print('Test: Remove from empty trie') trie = Trie() - assert_true(trie.remove('foo') is None) + self.assertTrue(trie.remove('foo') is None) def main(): test = TestTrie() test.test_trie() - test.test_trie_remove_invalid() + test.assertRaises(KeyError, test.test_trie_remove_invalid) if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/graphs_trees/trie/trie.py b/graphs_trees/trie/trie.py index 7cfe0fa..4dc8fa5 100644 --- a/graphs_trees/trie/trie.py +++ b/graphs_trees/trie/trie.py @@ -72,5 +72,5 @@ class Trie(object): return for key, child in node.children.items(): if child.terminates: - result.append(curr_word+key) - self._list_words(child, curr_word+key, result) \ No newline at end of file + result.append(curr_word + key) + self._list_words(child, curr_word + key, result) diff --git a/graphs_trees/trie/trie_challenge.ipynb b/graphs_trees/trie/trie_challenge.ipynb index 8889146..f4ce8b8 100644 --- a/graphs_trees/trie/trie_challenge.ipynb +++ b/graphs_trees/trie/trie_challenge.ipynb @@ -110,9 +110,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from collections import OrderedDict\n", @@ -165,21 +163,17 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_trie.py\n", - "from nose.tools import assert_true\n", + "import unittest\n", "\n", "\n", - "class TestTrie(object):\n", + "class TestTrie(unittest.TestCase): \n", "\n", " def test_trie(self):\n", - " print('Test: Remove from empty trie')\n", " trie = Trie()\n", - " assert_true(trie.remove('foo') is None)\n", "\n", " print('Test: Insert')\n", " words = ['a', 'at', 'has', 'hat', 'he',\n", @@ -187,11 +181,7 @@ " for word in words:\n", " trie.insert(word)\n", " for word in trie.list_words():\n", - " assert_true(trie.find(word) is not None)\n", - "\n", - " # Remove me\n", - " # Remove mens\n", - " # Remove a\n", + " self.assertTrue(trie.find(word) is not None)\n", " \n", " print('Test: Remove me')\n", " trie.remove('me')\n", @@ -199,9 +189,9 @@ " words = ['a', 'at', 'has', 'hat', 'he',\n", " 'men', 'mens', 'met']\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", - " assert_true(trie.find(word) is None)\n", + " self.assertTrue(trie.find(word) is None)\n", "\n", " print('Test: Remove mens')\n", " trie.remove('mens')\n", @@ -209,9 +199,9 @@ " words = ['a', 'at', 'has', 'hat', 'he',\n", " 'men', 'met']\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", - " assert_true(trie.find(word) is None)\n", + " self.assertTrue(trie.find(word) is None)\n", "\n", " print('Test: Remove a')\n", " trie.remove('a')\n", @@ -219,9 +209,9 @@ " words = ['at', 'has', 'hat', 'he',\n", " 'men', 'met']\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", - " assert_true(trie.find(word) is None)\n", + " self.assertTrue(trie.find(word) is None)\n", "\n", " print('Test: Remove has')\n", " trie.remove('has')\n", @@ -229,16 +219,22 @@ " words = ['at', 'hat', 'he',\n", " 'men', 'met']\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", - " assert_true(trie.find(word) is None)\n", + " self.assertTrue(trie.find(word) is None)\n", "\n", " print('Success: test_trie')\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", "def main():\n", " test = TestTrie()\n", " test.test_trie()\n", + " test.assertRaises(KeyError, test.test_trie_remove_invalid)\n", "\n", "\n", "if __name__ == '__main__':\n", @@ -271,9 +267,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.0" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/graphs_trees/trie/trie_solution.ipynb b/graphs_trees/trie/trie_solution.ipynb index aec6ba2..0a5d881 100644 --- a/graphs_trees/trie/trie_solution.ipynb +++ b/graphs_trees/trie/trie_solution.ipynb @@ -165,9 +165,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -260,9 +258,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "%run trie.py" @@ -278,9 +274,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -292,11 +286,10 @@ ], "source": [ "%%writefile test_trie.py\n", - "from nose.tools import assert_true\n", - "from nose.tools import raises\n", + "import unittest\n", "\n", "\n", - "class TestTrie(object): \n", + "class TestTrie(unittest.TestCase): \n", "\n", " def test_trie(self):\n", " trie = Trie()\n", @@ -307,7 +300,7 @@ " for word in words:\n", " trie.insert(word)\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", " print('Test: Remove me')\n", " trie.remove('me')\n", @@ -315,9 +308,9 @@ " words = ['a', 'at', 'has', 'hat', 'he',\n", " 'men', 'mens', 'met']\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", - " assert_true(trie.find(word) is None)\n", + " self.assertTrue(trie.find(word) is None)\n", "\n", " print('Test: Remove mens')\n", " trie.remove('mens')\n", @@ -325,9 +318,9 @@ " words = ['a', 'at', 'has', 'hat', 'he',\n", " 'men', 'met']\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", - " assert_true(trie.find(word) is None)\n", + " self.assertTrue(trie.find(word) is None)\n", "\n", " print('Test: Remove a')\n", " trie.remove('a')\n", @@ -335,9 +328,9 @@ " words = ['at', 'has', 'hat', 'he',\n", " 'men', 'met']\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", - " assert_true(trie.find(word) is None)\n", + " self.assertTrue(trie.find(word) is None)\n", "\n", " print('Test: Remove has')\n", " trie.remove('has')\n", @@ -345,23 +338,22 @@ " words = ['at', 'hat', 'he',\n", " 'men', 'met']\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", - " assert_true(trie.find(word) is None)\n", + " self.assertTrue(trie.find(word) is None)\n", "\n", " print('Success: test_trie')\n", "\n", - " @raises(Exception)\n", " def test_trie_remove_invalid(self):\n", " print('Test: Remove from empty trie')\n", " trie = Trie()\n", - " assert_true(trie.remove('foo') is None) \n", + " self.assertTrue(trie.remove('foo') is None) \n", "\n", "\n", "def main():\n", " test = TestTrie()\n", " test.test_trie()\n", - " test.test_trie_remove_invalid()\n", + " test.assertRaises(KeyError, test.test_trie_remove_invalid)\n", "\n", "\n", "if __name__ == '__main__':\n", @@ -371,9 +363,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -410,9 +400,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.7.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 }