From e1bf3e791a4e05054edd4852c2b64a92d8527971 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 10 Sep 2016 07:40:52 -0400 Subject: [PATCH 1/4] Move graph bfs to a class --- graphs_trees/graph_bfs/bfs_challenge.ipynb | 12 ++++---- graphs_trees/graph_bfs/bfs_solution.ipynb | 32 ++++++++++++---------- graphs_trees/graph_bfs/test_bfs.py | 4 +-- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/graphs_trees/graph_bfs/bfs_challenge.ipynb b/graphs_trees/graph_bfs/bfs_challenge.ipynb index 7b83e20..e78c026 100644 --- a/graphs_trees/graph_bfs/bfs_challenge.ipynb +++ b/graphs_trees/graph_bfs/bfs_challenge.ipynb @@ -103,9 +103,11 @@ }, "outputs": [], "source": [ - "def bfs(root, visit_func):\n", - " # TODO: Implement me\n", - " pass" + "class GraphBfs(Graph):\n", + "\n", + " def bfs(self, root, visit_func):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -152,7 +154,7 @@ "\n", " def test_bfs(self):\n", " nodes = []\n", - " graph = Graph()\n", + " graph = GraphBfs()\n", " for id in range(0, 6):\n", " nodes.append(graph.add_node(id))\n", " graph.add_edge(0, 1, 5)\n", @@ -163,7 +165,7 @@ " graph.add_edge(2, 1, 6)\n", " graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 4, 8)\n", - " bfs(nodes[0], self.results.add_result)\n", + " graph.bfs(nodes[0], self.results.add_result)\n", " assert_equal(str(self.results), \"[0, 1, 4, 5, 3, 2]\")\n", "\n", " print('Success: test_bfs')\n", diff --git a/graphs_trees/graph_bfs/bfs_solution.ipynb b/graphs_trees/graph_bfs/bfs_solution.ipynb index 7a6fd1e..b4b098b 100644 --- a/graphs_trees/graph_bfs/bfs_solution.ipynb +++ b/graphs_trees/graph_bfs/bfs_solution.ipynb @@ -115,19 +115,21 @@ "from collections import deque\n", "\n", "\n", - "def bfs(root, visit_func):\n", - " if root is None:\n", - " return\n", - " queue = deque()\n", - " queue.append(root)\n", - " root.visit_state = State.visited\n", - " while queue:\n", - " node = queue.popleft()\n", - " visit_func(node)\n", - " for adjacent_node in node.adjacent:\n", - " if adjacent_node.visit_state == State.unvisited:\n", - " queue.append(adjacent_node)\n", - " adjacent_node.visit_state = State.visited" + "class GraphBfs(Graph):\n", + "\n", + " def bfs(self, root, visit_func):\n", + " if root is None:\n", + " return\n", + " queue = deque()\n", + " queue.append(root)\n", + " root.visit_state = State.visited\n", + " while queue:\n", + " node = queue.popleft()\n", + " visit_func(node)\n", + " for adjacent_node in node.adj_nodes.values():\n", + " if adjacent_node.visit_state == State.unvisited:\n", + " queue.append(adjacent_node)\n", + " adjacent_node.visit_state = State.visited" ] }, { @@ -175,7 +177,7 @@ "\n", " def test_bfs(self):\n", " nodes = []\n", - " graph = Graph()\n", + " graph = GraphBfs()\n", " for id in range(0, 6):\n", " nodes.append(graph.add_node(id))\n", " graph.add_edge(0, 1, 5)\n", @@ -186,7 +188,7 @@ " graph.add_edge(2, 1, 6)\n", " graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 4, 8)\n", - " bfs(nodes[0], self.results.add_result)\n", + " graph.bfs(nodes[0], self.results.add_result)\n", " assert_equal(str(self.results), \"[0, 1, 4, 5, 3, 2]\")\n", "\n", " print('Success: test_bfs')\n", diff --git a/graphs_trees/graph_bfs/test_bfs.py b/graphs_trees/graph_bfs/test_bfs.py index 795d279..359f3fc 100644 --- a/graphs_trees/graph_bfs/test_bfs.py +++ b/graphs_trees/graph_bfs/test_bfs.py @@ -8,7 +8,7 @@ class TestBfs(object): def test_bfs(self): nodes = [] - graph = Graph() + graph = GraphBfs() for id in range(0, 6): nodes.append(graph.add_node(id)) graph.add_edge(0, 1, 5) @@ -19,7 +19,7 @@ class TestBfs(object): graph.add_edge(2, 1, 6) graph.add_edge(3, 2, 7) graph.add_edge(3, 4, 8) - bfs(nodes[0], self.results.add_result) + graph.bfs(nodes[0], self.results.add_result) assert_equal(str(self.results), "[0, 1, 4, 5, 3, 2]") print('Success: test_bfs') From 62f806100e604c1125397de163e5a3eb15a14516 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 10 Sep 2016 07:41:23 -0400 Subject: [PATCH 2/4] Move graph dfs to a class --- graphs_trees/graph_dfs/dfs_challenge.ipynb | 12 +++++++----- graphs_trees/graph_dfs/dfs_solution.ipynb | 22 ++++++++++++---------- graphs_trees/graph_dfs/test_dfs.py | 4 ++-- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/graphs_trees/graph_dfs/dfs_challenge.ipynb b/graphs_trees/graph_dfs/dfs_challenge.ipynb index 6732d68..6fda702 100644 --- a/graphs_trees/graph_dfs/dfs_challenge.ipynb +++ b/graphs_trees/graph_dfs/dfs_challenge.ipynb @@ -104,9 +104,11 @@ }, "outputs": [], "source": [ - "def dfs(root, visit_func):\n", - " # TODO: Implement me\n", - " pass" + "class GraphDfs(Graph):\n", + "\n", + " def dfs(self, root, visit_func):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -153,7 +155,7 @@ "\n", " def test_dfs(self):\n", " nodes = []\n", - " graph = Graph()\n", + " graph = GraphDfs()\n", " for id in range(0, 6):\n", " nodes.append(graph.add_node(id))\n", " graph.add_edge(0, 1, 5)\n", @@ -164,7 +166,7 @@ " graph.add_edge(2, 1, 6)\n", " graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 4, 8)\n", - " dfs(nodes[0], self.results.add_result)\n", + " graph.dfs(nodes[0], self.results.add_result)\n", " assert_equal(str(self.results), \"[0, 1, 3, 2, 4, 5]\")\n", "\n", " print('Success: test_dfs')\n", diff --git a/graphs_trees/graph_dfs/dfs_solution.ipynb b/graphs_trees/graph_dfs/dfs_solution.ipynb index f0ca00d..7fbb3e3 100644 --- a/graphs_trees/graph_dfs/dfs_solution.ipynb +++ b/graphs_trees/graph_dfs/dfs_solution.ipynb @@ -110,14 +110,16 @@ }, "outputs": [], "source": [ - "def dfs(root, visit_func):\n", - " if root is None:\n", - " return\n", - " visit_func(root)\n", - " root.visit_state = State.visited\n", - " for node in root.adjacent:\n", - " if node.visit_state == State.unvisited:\n", - " dfs(node, visit_func)" + "class GraphDfs(Graph):\n", + "\n", + " def dfs(self, root, visit_func):\n", + " if root is None:\n", + " return\n", + " visit_func(root)\n", + " root.visit_state = State.visited\n", + " for node in root.adj_nodes.values():\n", + " if node.visit_state == State.unvisited:\n", + " self.dfs(node, visit_func)" ] }, { @@ -165,7 +167,7 @@ "\n", " def test_dfs(self):\n", " nodes = []\n", - " graph = Graph()\n", + " graph = GraphDfs()\n", " for id in range(0, 6):\n", " nodes.append(graph.add_node(id))\n", " graph.add_edge(0, 1, 5)\n", @@ -176,7 +178,7 @@ " graph.add_edge(2, 1, 6)\n", " graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 4, 8)\n", - " dfs(nodes[0], self.results.add_result)\n", + " graph.dfs(nodes[0], self.results.add_result)\n", " assert_equal(str(self.results), \"[0, 1, 3, 2, 4, 5]\")\n", "\n", " print('Success: test_dfs')\n", diff --git a/graphs_trees/graph_dfs/test_dfs.py b/graphs_trees/graph_dfs/test_dfs.py index 2830387..8e1c50e 100644 --- a/graphs_trees/graph_dfs/test_dfs.py +++ b/graphs_trees/graph_dfs/test_dfs.py @@ -8,7 +8,7 @@ class TestDfs(object): def test_dfs(self): nodes = [] - graph = Graph() + graph = GraphDfs() for id in range(0, 6): nodes.append(graph.add_node(id)) graph.add_edge(0, 1, 5) @@ -19,7 +19,7 @@ class TestDfs(object): graph.add_edge(2, 1, 6) graph.add_edge(3, 2, 7) graph.add_edge(3, 4, 8) - dfs(nodes[0], self.results.add_result) + graph.dfs(nodes[0], self.results.add_result) assert_equal(str(self.results), "[0, 1, 3, 2, 4, 5]") print('Success: test_dfs') From cd50f73069ad7c842ee9378229ef95fa13b7fced Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 10 Sep 2016 07:42:21 -0400 Subject: [PATCH 3/4] Move graph path exists to a class --- .../path_exists_challenge.ipynb | 16 +++---- .../path_exists_solution.ipynb | 42 ++++++++++--------- .../graph_path_exists/test_path_exists.py | 8 ++-- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/graphs_trees/graph_path_exists/path_exists_challenge.ipynb b/graphs_trees/graph_path_exists/path_exists_challenge.ipynb index 51a3100..8ffdb97 100644 --- a/graphs_trees/graph_path_exists/path_exists_challenge.ipynb +++ b/graphs_trees/graph_path_exists/path_exists_challenge.ipynb @@ -106,9 +106,11 @@ }, "outputs": [], "source": [ - "def path_exists(start, end):\n", - " # TODO: Implement me\n", - " pass" + "class GraphPathExists(Graph):\n", + "\n", + " def path_exists(self, start, end):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -141,7 +143,7 @@ "\n", " def test_path_exists(self):\n", " nodes = []\n", - " graph = Graph()\n", + " graph = GraphPathExists()\n", " for id in range(0, 6):\n", " nodes.append(graph.add_node(id))\n", " graph.add_edge(0, 1, 5)\n", @@ -153,9 +155,9 @@ " graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 4, 8)\n", "\n", - " assert_equal(path_exists(nodes[0], nodes[2]), True)\n", - " assert_equal(path_exists(nodes[0], nodes[0]), True)\n", - " assert_equal(path_exists(nodes[4], nodes[5]), False)\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", "\n", " print('Success: test_path_exists')\n", "\n", diff --git a/graphs_trees/graph_path_exists/path_exists_solution.ipynb b/graphs_trees/graph_path_exists/path_exists_solution.ipynb index 899ee08..da45b70 100644 --- a/graphs_trees/graph_path_exists/path_exists_solution.ipynb +++ b/graphs_trees/graph_path_exists/path_exists_solution.ipynb @@ -124,23 +124,25 @@ "from collections import deque\n", "\n", "\n", - "def path_exists(start, end):\n", - " if start is None or end is None:\n", - " return False\n", - " if start is end:\n", - " return True\n", - " queue = deque()\n", - " queue.append(start)\n", - " start.visit_state = State.visited\n", - " while queue:\n", - " node = queue.popleft()\n", - " if node is end:\n", + "class GraphPathExists(Graph):\n", + "\n", + " def path_exists(self, start, end):\n", + " if start is None or end is None:\n", + " return False\n", + " if start is end:\n", " return True\n", - " for adj_node in node.adjacent:\n", - " if adj_node.visit_state == State.unvisited:\n", - " queue.append(adj_node)\n", - " adj_node.visit_state = State.visited\n", - " return False" + " queue = deque()\n", + " queue.append(start)\n", + " start.visit_state = State.visited\n", + " while queue:\n", + " node = queue.popleft()\n", + " if node is end:\n", + " return True\n", + " for adj_node in node.adj_nodes.values():\n", + " if adj_node.visit_state == State.unvisited:\n", + " queue.append(adj_node)\n", + " adj_node.visit_state = State.visited\n", + " return False" ] }, { @@ -174,7 +176,7 @@ "\n", " def test_path_exists(self):\n", " nodes = []\n", - " graph = Graph()\n", + " graph = GraphPathExists()\n", " for id in range(0, 6):\n", " nodes.append(graph.add_node(id))\n", " graph.add_edge(0, 1, 5)\n", @@ -186,9 +188,9 @@ " graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 4, 8)\n", "\n", - " assert_equal(path_exists(nodes[0], nodes[2]), True)\n", - " assert_equal(path_exists(nodes[0], nodes[0]), True)\n", - " assert_equal(path_exists(nodes[4], nodes[5]), False)\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", "\n", " print('Success: test_path_exists')\n", "\n", diff --git a/graphs_trees/graph_path_exists/test_path_exists.py b/graphs_trees/graph_path_exists/test_path_exists.py index a99bca0..a7374c7 100644 --- a/graphs_trees/graph_path_exists/test_path_exists.py +++ b/graphs_trees/graph_path_exists/test_path_exists.py @@ -5,7 +5,7 @@ class TestPathExists(object): def test_path_exists(self): nodes = [] - graph = Graph() + graph = GraphPathExists() for id in range(0, 6): nodes.append(graph.add_node(id)) graph.add_edge(0, 1, 5) @@ -17,9 +17,9 @@ class TestPathExists(object): graph.add_edge(3, 2, 7) graph.add_edge(3, 4, 8) - assert_equal(path_exists(nodes[0], nodes[2]), True) - assert_equal(path_exists(nodes[0], nodes[0]), True) - assert_equal(path_exists(nodes[4], nodes[5]), False) + 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) print('Success: test_path_exists') From 1928712364b048642f92131cb1682a4bb3cdf775 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 10 Sep 2016 07:42:54 -0400 Subject: [PATCH 4/4] Move tree level lists to a class --- .../tree_level_lists/test_tree_level_lists.py | 24 ++++---- .../tree_level_lists_challenge.ipynb | 34 ++++++----- .../tree_level_lists_solution.ipynb | 60 ++++++++++--------- 3 files changed, 61 insertions(+), 57 deletions(-) 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 9a9b4b2..2fe0f7f 100644 --- a/graphs_trees/tree_level_lists/test_tree_level_lists.py +++ b/graphs_trees/tree_level_lists/test_tree_level_lists.py @@ -4,19 +4,19 @@ from nose.tools import assert_equal class TestTreeLevelLists(object): def test_tree_level_lists(self): - node = Node(5) - insert(node, 3) - insert(node, 8) - insert(node, 2) - insert(node, 4) - insert(node, 1) - insert(node, 7) - insert(node, 6) - insert(node, 9) - insert(node, 10) - insert(node, 11) + bst = BstLevelLists(Node(5)) + bst.insert(3) + bst.insert(8) + bst.insert(2) + bst.insert(4) + bst.insert(1) + bst.insert(7) + bst.insert(6) + bst.insert(9) + bst.insert(10) + bst.insert(11) - levels = create_level_lists(node) + levels = bst.create_level_lists() results_list = [] for level in levels: results = Results() 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 a2be9ab..5e3db80 100644 --- a/graphs_trees/tree_level_lists/tree_level_lists_challenge.ipynb +++ b/graphs_trees/tree_level_lists/tree_level_lists_challenge.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Problem: Create a linked list for each level of a binary tree.\n", + "## Problem: Create a list for each level of a binary tree.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", @@ -91,9 +91,11 @@ }, "outputs": [], "source": [ - "def create_level_lists(root):\n", - " # TODO: Implement me\n", - " pass" + "class BstLevelLists(Bst):\n", + "\n", + " def create_level_lists(self):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -136,19 +138,19 @@ "class TestTreeLevelLists(object):\n", "\n", " def test_tree_level_lists(self):\n", - " node = Node(5)\n", - " insert(node, 3)\n", - " insert(node, 8)\n", - " insert(node, 2)\n", - " insert(node, 4)\n", - " insert(node, 1)\n", - " insert(node, 7)\n", - " insert(node, 6)\n", - " insert(node, 9)\n", - " insert(node, 10)\n", - " insert(node, 11)\n", + " bst = BstLevelLists(Node(5))\n", + " bst.insert(3)\n", + " bst.insert(8)\n", + " bst.insert(2)\n", + " bst.insert(4)\n", + " bst.insert(1)\n", + " bst.insert(7)\n", + " bst.insert(6)\n", + " bst.insert(9)\n", + " bst.insert(10)\n", + " bst.insert(11)\n", "\n", - " levels = create_level_lists(node)\n", + " levels = bst.create_level_lists()\n", " results_list = []\n", " for level in levels:\n", " results = Results()\n", 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 2cd6700..2ffc3b4 100644 --- a/graphs_trees/tree_level_lists/tree_level_lists_solution.ipynb +++ b/graphs_trees/tree_level_lists/tree_level_lists_solution.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Problem: Create a linked list for each level of a binary tree.\n", + "## Problem: Create a list for each level of a binary tree.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", @@ -103,23 +103,25 @@ }, "outputs": [], "source": [ - "def create_level_lists(root):\n", - " if root is None:\n", - " return\n", - " results = []\n", - " current = []\n", - " parents = []\n", - " current.append(root)\n", - " while current:\n", - " results.append(current)\n", - " parents = list(current)\n", + "class BstLevelLists(Bst):\n", + "\n", + " def create_level_lists(self):\n", + " if self.root is None:\n", + " return\n", + " results = []\n", " current = []\n", - " for parent in parents:\n", - " if parent.left is not None:\n", - " current.append(parent.left)\n", - " if parent.right is not None:\n", - " current.append(parent.right)\n", - " return results" + " parents = []\n", + " current.append(self.root)\n", + " while current:\n", + " results.append(current)\n", + " parents = list(current)\n", + " current = []\n", + " for parent in parents:\n", + " if parent.left is not None:\n", + " current.append(parent.left)\n", + " if parent.right is not None:\n", + " current.append(parent.right)\n", + " return results" ] }, { @@ -163,19 +165,19 @@ "class TestTreeLevelLists(object):\n", "\n", " def test_tree_level_lists(self):\n", - " node = Node(5)\n", - " insert(node, 3)\n", - " insert(node, 8)\n", - " insert(node, 2)\n", - " insert(node, 4)\n", - " insert(node, 1)\n", - " insert(node, 7)\n", - " insert(node, 6)\n", - " insert(node, 9)\n", - " insert(node, 10)\n", - " insert(node, 11)\n", + " bst = BstLevelLists(Node(5))\n", + " bst.insert(3)\n", + " bst.insert(8)\n", + " bst.insert(2)\n", + " bst.insert(4)\n", + " bst.insert(1)\n", + " bst.insert(7)\n", + " bst.insert(6)\n", + " bst.insert(9)\n", + " bst.insert(10)\n", + " bst.insert(11)\n", "\n", - " levels = create_level_lists(node)\n", + " levels = bst.create_level_lists()\n", " results_list = []\n", " for level in levels:\n", " results = Results()\n",