mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-07 09:58:03 +00:00
Rework graph challenge and solution (#86)
This commit is contained in:
@@ -35,8 +35,10 @@
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
"* Is the graph directed?\n",
|
||||
" * Yes\n",
|
||||
" * Implement both\n",
|
||||
"* Do the edges have weights?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume the inputs are valid?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
@@ -65,8 +67,7 @@
|
||||
"* `source` and `destination` nodes within `graph` are connected with specified `weight`.\n",
|
||||
"\n",
|
||||
"Note: \n",
|
||||
"* We'll be using an OrderedDict to make the outputs more consistent and simplify our testing.\n",
|
||||
"* Graph will be used as a building block for more complex graph challenges."
|
||||
"* The Graph class will be used as a building block for more complex graph challenges."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -93,24 +94,24 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import OrderedDict\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Node:\n",
|
||||
"\n",
|
||||
" def __init__(self, id):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" self.adjacent = OrderedDict() # key = node, val = weight\n",
|
||||
" self.adjacent = {} # key = node, val = weight\n",
|
||||
"\n",
|
||||
" def __str__(self):\n",
|
||||
" return str(self.id)\n",
|
||||
"\n",
|
||||
" def add_neighbor(self, neighbor, weight=0):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
"class Graph:\n",
|
||||
"\n",
|
||||
" def __init__(self):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" self.nodes = OrderedDict() # key = node id, val = node\n",
|
||||
" self.nodes = {} # key = node id, val = node\n",
|
||||
"\n",
|
||||
" def add_node(self, id):\n",
|
||||
" # TODO: Implement me\n",
|
||||
@@ -118,6 +119,10 @@
|
||||
"\n",
|
||||
" def add_edge(self, source, dest, weight=0):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def add_undirected_edge(self, source, dest, weight=0):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
]
|
||||
},
|
||||
@@ -149,19 +154,24 @@
|
||||
"\n",
|
||||
"class TestGraph(object):\n",
|
||||
"\n",
|
||||
" def test_graph(self):\n",
|
||||
" def create_graph(self):\n",
|
||||
" graph = Graph()\n",
|
||||
" for id in range(0, 6):\n",
|
||||
" graph.add_node(id)\n",
|
||||
" graph.add_edge(0, 1, 5)\n",
|
||||
" graph.add_edge(0, 5, 2)\n",
|
||||
" graph.add_edge(1, 2, 3)\n",
|
||||
" graph.add_edge(2, 3, 4)\n",
|
||||
" graph.add_edge(3, 4, 5)\n",
|
||||
" graph.add_edge(3, 5, 6)\n",
|
||||
" graph.add_edge(4, 0, 7)\n",
|
||||
" graph.add_edge(5, 4, 8)\n",
|
||||
" graph.add_edge(5, 2, 9)\n",
|
||||
" return graph\n",
|
||||
"\n",
|
||||
" def test_graph(self):\n",
|
||||
" graph = self.create_graph()\n",
|
||||
"\n",
|
||||
" graph.add_edge(0, 1, weight=5)\n",
|
||||
" graph.add_edge(0, 5, weight=2)\n",
|
||||
" graph.add_edge(1, 2, weight=3)\n",
|
||||
" graph.add_edge(2, 3, weight=4)\n",
|
||||
" graph.add_edge(3, 4, weight=5)\n",
|
||||
" graph.add_edge(3, 5, weight=6)\n",
|
||||
" graph.add_edge(4, 0, weight=7)\n",
|
||||
" graph.add_edge(5, 4, weight=8)\n",
|
||||
" graph.add_edge(5, 2, weight=9)\n",
|
||||
"\n",
|
||||
" assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5)\n",
|
||||
" assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2)\n",
|
||||
@@ -175,10 +185,27 @@
|
||||
"\n",
|
||||
" print('Success: test_graph')\n",
|
||||
"\n",
|
||||
" def test_graph_undirected(self):\n",
|
||||
" graph = self.create_graph()\n",
|
||||
"\n",
|
||||
" graph.add_undirected_edge(0, 1, weight=5)\n",
|
||||
" graph.add_undirected_edge(0, 5, weight=2)\n",
|
||||
" graph.add_undirected_edge(1, 2, weight=3)\n",
|
||||
"\n",
|
||||
" assert_equal(graph.nodes[0].adjacent[graph.nodes[1]], 5)\n",
|
||||
" assert_equal(graph.nodes[1].adjacent[graph.nodes[0]], 5)\n",
|
||||
" assert_equal(graph.nodes[0].adjacent[graph.nodes[5]], 2)\n",
|
||||
" assert_equal(graph.nodes[5].adjacent[graph.nodes[0]], 2)\n",
|
||||
" assert_equal(graph.nodes[1].adjacent[graph.nodes[2]], 3)\n",
|
||||
" assert_equal(graph.nodes[2].adjacent[graph.nodes[1]], 3)\n",
|
||||
"\n",
|
||||
" print('Success: test_graph')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestGraph()\n",
|
||||
" test.test_graph()\n",
|
||||
" test.test_graph_undirected()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
@@ -197,21 +224,21 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user