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": [],
"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",

View File

@@ -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",

View File

@@ -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')

View File

@@ -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",

View File

@@ -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",

View File

@@ -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')

View File

@@ -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",

View File

@@ -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",

View File

@@ -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')

View File

@@ -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()

View File

@@ -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",

View File

@@ -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",