Merge pull request #100 from donnemartin/develop

Move graph and tree challenges and solutions to classes
This commit is contained in:
Donne Martin
2016-09-10 07:44:45 -04:00
committed by GitHub
12 changed files with 143 additions and 127 deletions

View File

@@ -103,9 +103,11 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def bfs(root, visit_func):\n", "class GraphBfs(Graph):\n",
" # TODO: Implement me\n", "\n",
" pass" " def bfs(self, root, visit_func):\n",
" # TODO: Implement me\n",
" pass"
] ]
}, },
{ {
@@ -152,7 +154,7 @@
"\n", "\n",
" def test_bfs(self):\n", " def test_bfs(self):\n",
" nodes = []\n", " nodes = []\n",
" graph = Graph()\n", " graph = GraphBfs()\n",
" for id in range(0, 6):\n", " for id in range(0, 6):\n",
" nodes.append(graph.add_node(id))\n", " nodes.append(graph.add_node(id))\n",
" graph.add_edge(0, 1, 5)\n", " graph.add_edge(0, 1, 5)\n",
@@ -163,7 +165,7 @@
" graph.add_edge(2, 1, 6)\n", " graph.add_edge(2, 1, 6)\n",
" graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 2, 7)\n",
" graph.add_edge(3, 4, 8)\n", " graph.add_edge(3, 4, 8)\n",
" 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", " assert_equal(str(self.results), \"[0, 1, 4, 5, 3, 2]\")\n",
"\n", "\n",
" print('Success: test_bfs')\n", " print('Success: test_bfs')\n",

View File

@@ -115,19 +115,21 @@
"from collections import deque\n", "from collections import deque\n",
"\n", "\n",
"\n", "\n",
"def bfs(root, visit_func):\n", "class GraphBfs(Graph):\n",
" if root is None:\n", "\n",
" return\n", " def bfs(self, root, visit_func):\n",
" queue = deque()\n", " if root is None:\n",
" queue.append(root)\n", " return\n",
" root.visit_state = State.visited\n", " queue = deque()\n",
" while queue:\n", " queue.append(root)\n",
" node = queue.popleft()\n", " root.visit_state = State.visited\n",
" visit_func(node)\n", " while queue:\n",
" for adjacent_node in node.adjacent:\n", " node = queue.popleft()\n",
" if adjacent_node.visit_state == State.unvisited:\n", " visit_func(node)\n",
" queue.append(adjacent_node)\n", " for adjacent_node in node.adj_nodes.values():\n",
" adjacent_node.visit_state = State.visited" " if adjacent_node.visit_state == State.unvisited:\n",
" queue.append(adjacent_node)\n",
" adjacent_node.visit_state = State.visited"
] ]
}, },
{ {
@@ -175,7 +177,7 @@
"\n", "\n",
" def test_bfs(self):\n", " def test_bfs(self):\n",
" nodes = []\n", " nodes = []\n",
" graph = Graph()\n", " graph = GraphBfs()\n",
" for id in range(0, 6):\n", " for id in range(0, 6):\n",
" nodes.append(graph.add_node(id))\n", " nodes.append(graph.add_node(id))\n",
" graph.add_edge(0, 1, 5)\n", " graph.add_edge(0, 1, 5)\n",
@@ -186,7 +188,7 @@
" graph.add_edge(2, 1, 6)\n", " graph.add_edge(2, 1, 6)\n",
" graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 2, 7)\n",
" graph.add_edge(3, 4, 8)\n", " graph.add_edge(3, 4, 8)\n",
" 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", " assert_equal(str(self.results), \"[0, 1, 4, 5, 3, 2]\")\n",
"\n", "\n",
" print('Success: test_bfs')\n", " print('Success: test_bfs')\n",

View File

@@ -8,7 +8,7 @@ class TestBfs(object):
def test_bfs(self): def test_bfs(self):
nodes = [] nodes = []
graph = Graph() graph = GraphBfs()
for id in range(0, 6): for id in range(0, 6):
nodes.append(graph.add_node(id)) nodes.append(graph.add_node(id))
graph.add_edge(0, 1, 5) graph.add_edge(0, 1, 5)
@@ -19,7 +19,7 @@ class TestBfs(object):
graph.add_edge(2, 1, 6) graph.add_edge(2, 1, 6)
graph.add_edge(3, 2, 7) graph.add_edge(3, 2, 7)
graph.add_edge(3, 4, 8) 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]") assert_equal(str(self.results), "[0, 1, 4, 5, 3, 2]")
print('Success: test_bfs') print('Success: test_bfs')

View File

@@ -104,9 +104,11 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def dfs(root, visit_func):\n", "class GraphDfs(Graph):\n",
" # TODO: Implement me\n", "\n",
" pass" " def dfs(self, root, visit_func):\n",
" # TODO: Implement me\n",
" pass"
] ]
}, },
{ {
@@ -153,7 +155,7 @@
"\n", "\n",
" def test_dfs(self):\n", " def test_dfs(self):\n",
" nodes = []\n", " nodes = []\n",
" graph = Graph()\n", " graph = GraphDfs()\n",
" for id in range(0, 6):\n", " for id in range(0, 6):\n",
" nodes.append(graph.add_node(id))\n", " nodes.append(graph.add_node(id))\n",
" graph.add_edge(0, 1, 5)\n", " graph.add_edge(0, 1, 5)\n",
@@ -164,7 +166,7 @@
" graph.add_edge(2, 1, 6)\n", " graph.add_edge(2, 1, 6)\n",
" graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 2, 7)\n",
" graph.add_edge(3, 4, 8)\n", " graph.add_edge(3, 4, 8)\n",
" 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", " assert_equal(str(self.results), \"[0, 1, 3, 2, 4, 5]\")\n",
"\n", "\n",
" print('Success: test_dfs')\n", " print('Success: test_dfs')\n",

View File

@@ -110,14 +110,16 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def dfs(root, visit_func):\n", "class GraphDfs(Graph):\n",
" if root is None:\n", "\n",
" return\n", " def dfs(self, root, visit_func):\n",
" visit_func(root)\n", " if root is None:\n",
" root.visit_state = State.visited\n", " return\n",
" for node in root.adjacent:\n", " visit_func(root)\n",
" if node.visit_state == State.unvisited:\n", " root.visit_state = State.visited\n",
" dfs(node, visit_func)" " 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", "\n",
" def test_dfs(self):\n", " def test_dfs(self):\n",
" nodes = []\n", " nodes = []\n",
" graph = Graph()\n", " graph = GraphDfs()\n",
" for id in range(0, 6):\n", " for id in range(0, 6):\n",
" nodes.append(graph.add_node(id))\n", " nodes.append(graph.add_node(id))\n",
" graph.add_edge(0, 1, 5)\n", " graph.add_edge(0, 1, 5)\n",
@@ -176,7 +178,7 @@
" graph.add_edge(2, 1, 6)\n", " graph.add_edge(2, 1, 6)\n",
" graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 2, 7)\n",
" graph.add_edge(3, 4, 8)\n", " graph.add_edge(3, 4, 8)\n",
" 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", " assert_equal(str(self.results), \"[0, 1, 3, 2, 4, 5]\")\n",
"\n", "\n",
" print('Success: test_dfs')\n", " print('Success: test_dfs')\n",

View File

@@ -8,7 +8,7 @@ class TestDfs(object):
def test_dfs(self): def test_dfs(self):
nodes = [] nodes = []
graph = Graph() graph = GraphDfs()
for id in range(0, 6): for id in range(0, 6):
nodes.append(graph.add_node(id)) nodes.append(graph.add_node(id))
graph.add_edge(0, 1, 5) graph.add_edge(0, 1, 5)
@@ -19,7 +19,7 @@ class TestDfs(object):
graph.add_edge(2, 1, 6) graph.add_edge(2, 1, 6)
graph.add_edge(3, 2, 7) graph.add_edge(3, 2, 7)
graph.add_edge(3, 4, 8) 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]") assert_equal(str(self.results), "[0, 1, 3, 2, 4, 5]")
print('Success: test_dfs') print('Success: test_dfs')

View File

@@ -106,9 +106,11 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def path_exists(start, end):\n", "class GraphPathExists(Graph):\n",
" # TODO: Implement me\n", "\n",
" pass" " def path_exists(self, start, end):\n",
" # TODO: Implement me\n",
" pass"
] ]
}, },
{ {
@@ -141,7 +143,7 @@
"\n", "\n",
" def test_path_exists(self):\n", " def test_path_exists(self):\n",
" nodes = []\n", " nodes = []\n",
" graph = Graph()\n", " graph = GraphPathExists()\n",
" for id in range(0, 6):\n", " for id in range(0, 6):\n",
" nodes.append(graph.add_node(id))\n", " nodes.append(graph.add_node(id))\n",
" graph.add_edge(0, 1, 5)\n", " graph.add_edge(0, 1, 5)\n",
@@ -153,9 +155,9 @@
" graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 2, 7)\n",
" graph.add_edge(3, 4, 8)\n", " graph.add_edge(3, 4, 8)\n",
"\n", "\n",
" assert_equal(path_exists(nodes[0], nodes[2]), True)\n", " assert_equal(graph.path_exists(nodes[0], nodes[2]), True)\n",
" assert_equal(path_exists(nodes[0], nodes[0]), True)\n", " assert_equal(graph.path_exists(nodes[0], nodes[0]), True)\n",
" assert_equal(path_exists(nodes[4], nodes[5]), False)\n", " assert_equal(graph.path_exists(nodes[4], nodes[5]), False)\n",
"\n", "\n",
" print('Success: test_path_exists')\n", " print('Success: test_path_exists')\n",
"\n", "\n",

View File

@@ -124,23 +124,25 @@
"from collections import deque\n", "from collections import deque\n",
"\n", "\n",
"\n", "\n",
"def path_exists(start, end):\n", "class GraphPathExists(Graph):\n",
" if start is None or end is None:\n", "\n",
" return False\n", " def path_exists(self, start, end):\n",
" if start is end:\n", " if start is None or end is None:\n",
" return True\n", " return False\n",
" queue = deque()\n", " if start is end:\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", " return True\n",
" for adj_node in node.adjacent:\n", " queue = deque()\n",
" if adj_node.visit_state == State.unvisited:\n", " queue.append(start)\n",
" queue.append(adj_node)\n", " start.visit_state = State.visited\n",
" adj_node.visit_state = State.visited\n", " while queue:\n",
" return False" " 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", "\n",
" def test_path_exists(self):\n", " def test_path_exists(self):\n",
" nodes = []\n", " nodes = []\n",
" graph = Graph()\n", " graph = GraphPathExists()\n",
" for id in range(0, 6):\n", " for id in range(0, 6):\n",
" nodes.append(graph.add_node(id))\n", " nodes.append(graph.add_node(id))\n",
" graph.add_edge(0, 1, 5)\n", " graph.add_edge(0, 1, 5)\n",
@@ -186,9 +188,9 @@
" graph.add_edge(3, 2, 7)\n", " graph.add_edge(3, 2, 7)\n",
" graph.add_edge(3, 4, 8)\n", " graph.add_edge(3, 4, 8)\n",
"\n", "\n",
" assert_equal(path_exists(nodes[0], nodes[2]), True)\n", " assert_equal(graph.path_exists(nodes[0], nodes[2]), True)\n",
" assert_equal(path_exists(nodes[0], nodes[0]), True)\n", " assert_equal(graph.path_exists(nodes[0], nodes[0]), True)\n",
" assert_equal(path_exists(nodes[4], nodes[5]), False)\n", " assert_equal(graph.path_exists(nodes[4], nodes[5]), False)\n",
"\n", "\n",
" print('Success: test_path_exists')\n", " print('Success: test_path_exists')\n",
"\n", "\n",

View File

@@ -5,7 +5,7 @@ class TestPathExists(object):
def test_path_exists(self): def test_path_exists(self):
nodes = [] nodes = []
graph = Graph() graph = GraphPathExists()
for id in range(0, 6): for id in range(0, 6):
nodes.append(graph.add_node(id)) nodes.append(graph.add_node(id))
graph.add_edge(0, 1, 5) graph.add_edge(0, 1, 5)
@@ -17,9 +17,9 @@ class TestPathExists(object):
graph.add_edge(3, 2, 7) graph.add_edge(3, 2, 7)
graph.add_edge(3, 4, 8) graph.add_edge(3, 4, 8)
assert_equal(path_exists(nodes[0], nodes[2]), True) assert_equal(graph.path_exists(nodes[0], nodes[2]), True)
assert_equal(path_exists(nodes[0], nodes[0]), True) assert_equal(graph.path_exists(nodes[0], nodes[0]), True)
assert_equal(path_exists(nodes[4], nodes[5]), False) assert_equal(graph.path_exists(nodes[4], nodes[5]), False)
print('Success: test_path_exists') print('Success: test_path_exists')

View File

@@ -4,19 +4,19 @@ from nose.tools import assert_equal
class TestTreeLevelLists(object): class TestTreeLevelLists(object):
def test_tree_level_lists(self): def test_tree_level_lists(self):
node = Node(5) bst = BstLevelLists(Node(5))
insert(node, 3) bst.insert(3)
insert(node, 8) bst.insert(8)
insert(node, 2) bst.insert(2)
insert(node, 4) bst.insert(4)
insert(node, 1) bst.insert(1)
insert(node, 7) bst.insert(7)
insert(node, 6) bst.insert(6)
insert(node, 9) bst.insert(9)
insert(node, 10) bst.insert(10)
insert(node, 11) bst.insert(11)
levels = create_level_lists(node) levels = bst.create_level_lists()
results_list = [] results_list = []
for level in levels: for level in levels:
results = Results() results = Results()

View File

@@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "\n",
"* [Constraints](#Constraints)\n", "* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n", "* [Test Cases](#Test-Cases)\n",
@@ -91,9 +91,11 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def create_level_lists(root):\n", "class BstLevelLists(Bst):\n",
" # TODO: Implement me\n", "\n",
" pass" " def create_level_lists(self):\n",
" # TODO: Implement me\n",
" pass"
] ]
}, },
{ {
@@ -136,19 +138,19 @@
"class TestTreeLevelLists(object):\n", "class TestTreeLevelLists(object):\n",
"\n", "\n",
" def test_tree_level_lists(self):\n", " def test_tree_level_lists(self):\n",
" node = Node(5)\n", " bst = BstLevelLists(Node(5))\n",
" insert(node, 3)\n", " bst.insert(3)\n",
" insert(node, 8)\n", " bst.insert(8)\n",
" insert(node, 2)\n", " bst.insert(2)\n",
" insert(node, 4)\n", " bst.insert(4)\n",
" insert(node, 1)\n", " bst.insert(1)\n",
" insert(node, 7)\n", " bst.insert(7)\n",
" insert(node, 6)\n", " bst.insert(6)\n",
" insert(node, 9)\n", " bst.insert(9)\n",
" insert(node, 10)\n", " bst.insert(10)\n",
" insert(node, 11)\n", " bst.insert(11)\n",
"\n", "\n",
" levels = create_level_lists(node)\n", " levels = bst.create_level_lists()\n",
" results_list = []\n", " results_list = []\n",
" for level in levels:\n", " for level in levels:\n",
" results = Results()\n", " results = Results()\n",

View File

@@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "\n",
"* [Constraints](#Constraints)\n", "* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n", "* [Test Cases](#Test-Cases)\n",
@@ -103,23 +103,25 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def create_level_lists(root):\n", "class BstLevelLists(Bst):\n",
" if root is None:\n", "\n",
" return\n", " def create_level_lists(self):\n",
" results = []\n", " if self.root is None:\n",
" current = []\n", " return\n",
" parents = []\n", " results = []\n",
" current.append(root)\n",
" while current:\n",
" results.append(current)\n",
" parents = list(current)\n",
" current = []\n", " current = []\n",
" for parent in parents:\n", " parents = []\n",
" if parent.left is not None:\n", " current.append(self.root)\n",
" current.append(parent.left)\n", " while current:\n",
" if parent.right is not None:\n", " results.append(current)\n",
" current.append(parent.right)\n", " parents = list(current)\n",
" return results" " 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", "class TestTreeLevelLists(object):\n",
"\n", "\n",
" def test_tree_level_lists(self):\n", " def test_tree_level_lists(self):\n",
" node = Node(5)\n", " bst = BstLevelLists(Node(5))\n",
" insert(node, 3)\n", " bst.insert(3)\n",
" insert(node, 8)\n", " bst.insert(8)\n",
" insert(node, 2)\n", " bst.insert(2)\n",
" insert(node, 4)\n", " bst.insert(4)\n",
" insert(node, 1)\n", " bst.insert(1)\n",
" insert(node, 7)\n", " bst.insert(7)\n",
" insert(node, 6)\n", " bst.insert(6)\n",
" insert(node, 9)\n", " bst.insert(9)\n",
" insert(node, 10)\n", " bst.insert(10)\n",
" insert(node, 11)\n", " bst.insert(11)\n",
"\n", "\n",
" levels = create_level_lists(node)\n", " levels = bst.create_level_lists()\n",
" results_list = []\n", " results_list = []\n",
" for level in levels:\n", " for level in levels:\n",
" results = Results()\n", " results = Results()\n",