mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-06 17:38:02 +00:00
Move graph path exists to a class
This commit is contained in:
@@ -106,7 +106,9 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def path_exists(start, end):\n",
|
"class GraphPathExists(Graph):\n",
|
||||||
|
"\n",
|
||||||
|
" def path_exists(self, start, end):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass"
|
" 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",
|
||||||
|
|||||||
@@ -124,7 +124,9 @@
|
|||||||
"from collections import deque\n",
|
"from collections import deque\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def path_exists(start, end):\n",
|
"class GraphPathExists(Graph):\n",
|
||||||
|
"\n",
|
||||||
|
" def path_exists(self, start, end):\n",
|
||||||
" if start is None or end is None:\n",
|
" if start is None or end is None:\n",
|
||||||
" return False\n",
|
" return False\n",
|
||||||
" if start is end:\n",
|
" if start is end:\n",
|
||||||
@@ -136,7 +138,7 @@
|
|||||||
" node = queue.popleft()\n",
|
" node = queue.popleft()\n",
|
||||||
" if node is end:\n",
|
" if node is end:\n",
|
||||||
" return True\n",
|
" return True\n",
|
||||||
" for adj_node in node.adjacent:\n",
|
" for adj_node in node.adj_nodes.values():\n",
|
||||||
" if adj_node.visit_state == State.unvisited:\n",
|
" if adj_node.visit_state == State.unvisited:\n",
|
||||||
" queue.append(adj_node)\n",
|
" queue.append(adj_node)\n",
|
||||||
" adj_node.visit_state = State.visited\n",
|
" adj_node.visit_state = State.visited\n",
|
||||||
@@ -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",
|
||||||
|
|||||||
@@ -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')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user