mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-08 10:28:03 +00:00
Rework graph challenge and solution (#90)
This commit is contained in:
@@ -39,6 +39,12 @@
|
||||
"* Do the edges have weights?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume the inputs are valid?\n",
|
||||
" * Yes\n",
|
||||
"* If we try to add a node that already exists, do we just do nothing?\n",
|
||||
" * Yes\n",
|
||||
"* If we try to delete a node that doesn't exist, do we just do nothing?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume this fits memory?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
@@ -94,24 +100,43 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from enum import Enum # Python 2 users: Run pip install enum34\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class State(Enum):\n",
|
||||
" unvisited = 0\n",
|
||||
" visiting = 1\n",
|
||||
" visited = 2\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Node:\n",
|
||||
"\n",
|
||||
" def __init__(self, id):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" self.adjacent = {} # key = node, val = weight\n",
|
||||
" def __init__(self, key):\n",
|
||||
" self.key = key\n",
|
||||
" self.visit_state = State.unvisited\n",
|
||||
" self.incoming_edges = 0\n",
|
||||
" self.adj_nodes = {} # Key = key, val = Node\n",
|
||||
" self.adj_weights = {} # Key = Node, val = weight\n",
|
||||
"\n",
|
||||
" def __str__(self):\n",
|
||||
" def __repr__(self):\n",
|
||||
" return str(self.id)\n",
|
||||
"\n",
|
||||
" def __lt__(self, left, right):\n",
|
||||
" return left.id < right.id\n",
|
||||
"\n",
|
||||
" def add_neighbor(self, neighbor, weight=0):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def remove_neighbor(self, neighbor):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Graph:\n",
|
||||
"\n",
|
||||
" def __init__(self):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" self.nodes = {} # key = node id, val = node\n",
|
||||
" self.nodes = {} # Key = key, val = Node\n",
|
||||
"\n",
|
||||
" def add_node(self, id):\n",
|
||||
" # TODO: Implement me\n",
|
||||
@@ -156,13 +181,12 @@
|
||||
"\n",
|
||||
" def create_graph(self):\n",
|
||||
" graph = Graph()\n",
|
||||
" for id in range(0, 6):\n",
|
||||
" graph.add_node(id)\n",
|
||||
" for key in range(0, 6):\n",
|
||||
" graph.add_node(key)\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",
|
||||
@@ -173,33 +197,46 @@
|
||||
" 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",
|
||||
" assert_equal(graph.nodes[1].adjacent[graph.nodes[2]], 3)\n",
|
||||
" assert_equal(graph.nodes[2].adjacent[graph.nodes[3]], 4)\n",
|
||||
" assert_equal(graph.nodes[3].adjacent[graph.nodes[4]], 5)\n",
|
||||
" assert_equal(graph.nodes[3].adjacent[graph.nodes[5]], 6)\n",
|
||||
" assert_equal(graph.nodes[4].adjacent[graph.nodes[0]], 7)\n",
|
||||
" assert_equal(graph.nodes[5].adjacent[graph.nodes[4]], 8)\n",
|
||||
" assert_equal(graph.nodes[5].adjacent[graph.nodes[2]], 9)\n",
|
||||
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)\n",
|
||||
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n",
|
||||
" assert_equal(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n",
|
||||
" assert_equal(graph.nodes[2].adj_weights[graph.nodes[3].key], 4)\n",
|
||||
" assert_equal(graph.nodes[3].adj_weights[graph.nodes[4].key], 5)\n",
|
||||
" assert_equal(graph.nodes[3].adj_weights[graph.nodes[5].key], 6)\n",
|
||||
" assert_equal(graph.nodes[4].adj_weights[graph.nodes[0].key], 7)\n",
|
||||
" assert_equal(graph.nodes[5].adj_weights[graph.nodes[4].key], 8)\n",
|
||||
" assert_equal(graph.nodes[5].adj_weights[graph.nodes[2].key], 9)\n",
|
||||
"\n",
|
||||
" assert_equal(graph.nodes[0].incoming_edges, 1)\n",
|
||||
" assert_equal(graph.nodes[1].incoming_edges, 1)\n",
|
||||
" assert_equal(graph.nodes[2].incoming_edges, 2)\n",
|
||||
" assert_equal(graph.nodes[3].incoming_edges, 1)\n",
|
||||
" assert_equal(graph.nodes[4].incoming_edges, 2)\n",
|
||||
" assert_equal(graph.nodes[5].incoming_edges, 2)\n",
|
||||
"\n",
|
||||
" graph.nodes[0].remove_neighbor(graph.nodes[1])\n",
|
||||
" assert_equal(graph.nodes[1].incoming_edges, 0)\n",
|
||||
" graph.nodes[3].remove_neighbor(graph.nodes[4])\n",
|
||||
" assert_equal(graph.nodes[4].incoming_edges, 1)\n",
|
||||
"\n",
|
||||
" assert_equal(graph.nodes[0] < graph.nodes[1], True)\n",
|
||||
"\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",
|
||||
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[1].key], 5)\n",
|
||||
" assert_equal(graph.nodes[1].adj_weights[graph.nodes[0].key], 5)\n",
|
||||
" assert_equal(graph.nodes[0].adj_weights[graph.nodes[5].key], 2)\n",
|
||||
" assert_equal(graph.nodes[5].adj_weights[graph.nodes[0].key], 2)\n",
|
||||
" assert_equal(graph.nodes[1].adj_weights[graph.nodes[2].key], 3)\n",
|
||||
" assert_equal(graph.nodes[2].adj_weights[graph.nodes[1].key], 3)\n",
|
||||
"\n",
|
||||
" print('Success: test_graph')\n",
|
||||
" print('Success: test_graph_undirected')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
Reference in New Issue
Block a user