mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Update graph challenge (#117)
Use more specific exception types. Update constraints. Update add_undirected_edge to use add_edge.
This commit is contained in:
parent
3d4ae5c22f
commit
2d98b8a496
|
@ -23,15 +23,17 @@ class Node:
|
||||||
return self.key < other.key
|
return self.key < other.key
|
||||||
|
|
||||||
def add_neighbor(self, neighbor, weight=0):
|
def add_neighbor(self, neighbor, weight=0):
|
||||||
if neighbor is None:
|
if neighbor is None or weight is None:
|
||||||
raise Exception('Invalid neighbor')
|
raise TypeError('neighbor or weight cannot be None')
|
||||||
neighbor.incoming_edges += 1
|
neighbor.incoming_edges += 1
|
||||||
self.adj_weights[neighbor.key] = weight
|
self.adj_weights[neighbor.key] = weight
|
||||||
self.adj_nodes[neighbor.key] = neighbor
|
self.adj_nodes[neighbor.key] = neighbor
|
||||||
|
|
||||||
def remove_neighbor(self, neighbor):
|
def remove_neighbor(self, neighbor):
|
||||||
if neighbor is None or neighbor.key not in self.adj_nodes:
|
if neighbor is None:
|
||||||
raise Exception('Invalid neighbor')
|
raise TypeError('neighbor cannot be None')
|
||||||
|
if neighbor.key not in self.adj_nodes:
|
||||||
|
raise KeyError('neighbor not found')
|
||||||
neighbor.incoming_edges -= 1
|
neighbor.incoming_edges -= 1
|
||||||
del self.adj_weights[neighbor.key]
|
del self.adj_weights[neighbor.key]
|
||||||
del self.adj_nodes[neighbor.key]
|
del self.adj_nodes[neighbor.key]
|
||||||
|
@ -44,7 +46,7 @@ class Graph:
|
||||||
|
|
||||||
def add_node(self, key):
|
def add_node(self, key):
|
||||||
if key is None:
|
if key is None:
|
||||||
raise Exception('Invalid key')
|
raise TypeError('key cannot be None')
|
||||||
if key in self.nodes:
|
if key in self.nodes:
|
||||||
return self.nodes[key]
|
return self.nodes[key]
|
||||||
self.nodes[key] = Node(key)
|
self.nodes[key] = Node(key)
|
||||||
|
@ -60,9 +62,8 @@ class Graph:
|
||||||
self.nodes[source_key].add_neighbor(self.nodes[dest_key],
|
self.nodes[source_key].add_neighbor(self.nodes[dest_key],
|
||||||
weight)
|
weight)
|
||||||
|
|
||||||
def add_undirected_edge(self, source_key, dest_key, weight=0):
|
def add_undirected_edge(self, src_key, dst_key, weight=0):
|
||||||
if source_key is None or dest_key is None:
|
if src_key is None or dst_key is None:
|
||||||
raise Exception('Invalid key')
|
raise TypeError('key cannot be None')
|
||||||
self.add_edge(source_key, dest_key, weight)
|
self.add_edge(src_key, dst_key, weight)
|
||||||
self.nodes[dest_key].add_neighbor(self.nodes[source_key],
|
self.add_edge(dst_key, src_key, weight)
|
||||||
weight)
|
|
|
@ -38,12 +38,14 @@
|
||||||
" * Implement both\n",
|
" * Implement both\n",
|
||||||
"* Do the edges have weights?\n",
|
"* Do the edges have weights?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Can we assume the inputs are valid?\n",
|
"* Can the graph have cycles?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* If we try to add a node that already exists, do we just do nothing?\n",
|
"* If we try to add a node that already exists, do we just do nothing?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* If we try to delete a node that doesn't exist, do we just do nothing?\n",
|
"* If we try to delete a node that doesn't exist, do we just do nothing?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
|
"* Can we assume the inputs are valid?\n",
|
||||||
|
" * Yes\n",
|
||||||
"* Can we assume this fits memory?\n",
|
"* Can we assume this fits memory?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
|
|
|
@ -37,10 +37,14 @@
|
||||||
" * Implement both\n",
|
" * Implement both\n",
|
||||||
"* Do the edges have weights?\n",
|
"* Do the edges have weights?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
|
"* Can the graph have cycles?\n",
|
||||||
|
" * Yes\n",
|
||||||
"* If we try to add a node that already exists, do we just do nothing?\n",
|
"* If we try to add a node that already exists, do we just do nothing?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* If we try to delete a node that doesn't exist, do we just do nothing?\n",
|
"* If we try to delete a node that doesn't exist, do we just do nothing?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
|
"* Can we assume the inputs are valid?\n",
|
||||||
|
" * Yes\n",
|
||||||
"* Can we assume this fits memory?\n",
|
"* Can we assume this fits memory?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
|
@ -187,15 +191,17 @@
|
||||||
" return self.key < other.key\n",
|
" return self.key < other.key\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def add_neighbor(self, neighbor, weight=0):\n",
|
" def add_neighbor(self, neighbor, weight=0):\n",
|
||||||
" if neighbor is None:\n",
|
" if neighbor is None or weight is None:\n",
|
||||||
" raise Exception('Invalid neighbor')\n",
|
" raise TypeError('neighbor or weight cannot be None')\n",
|
||||||
" neighbor.incoming_edges += 1\n",
|
" neighbor.incoming_edges += 1\n",
|
||||||
" self.adj_weights[neighbor.key] = weight\n",
|
" self.adj_weights[neighbor.key] = weight\n",
|
||||||
" self.adj_nodes[neighbor.key] = neighbor\n",
|
" self.adj_nodes[neighbor.key] = neighbor\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def remove_neighbor(self, neighbor):\n",
|
" def remove_neighbor(self, neighbor):\n",
|
||||||
" if neighbor is None or neighbor.key not in self.adj_nodes:\n",
|
" if neighbor is None:\n",
|
||||||
" raise Exception('Invalid neighbor')\n",
|
" raise TypeError('neighbor cannot be None')\n",
|
||||||
|
" if neighbor.key not in self.adj_nodes:\n",
|
||||||
|
" raise KeyError('neighbor not found')\n",
|
||||||
" neighbor.incoming_edges -= 1\n",
|
" neighbor.incoming_edges -= 1\n",
|
||||||
" del self.adj_weights[neighbor.key]\n",
|
" del self.adj_weights[neighbor.key]\n",
|
||||||
" del self.adj_nodes[neighbor.key]\n",
|
" del self.adj_nodes[neighbor.key]\n",
|
||||||
|
@ -208,7 +214,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
" def add_node(self, key):\n",
|
" def add_node(self, key):\n",
|
||||||
" if key is None:\n",
|
" if key is None:\n",
|
||||||
" raise Exception('Invalid key')\n",
|
" raise TypeError('key cannot be None')\n",
|
||||||
" if key in self.nodes:\n",
|
" if key in self.nodes:\n",
|
||||||
" return self.nodes[key]\n",
|
" return self.nodes[key]\n",
|
||||||
" self.nodes[key] = Node(key)\n",
|
" self.nodes[key] = Node(key)\n",
|
||||||
|
@ -224,12 +230,11 @@
|
||||||
" self.nodes[source_key].add_neighbor(self.nodes[dest_key],\n",
|
" self.nodes[source_key].add_neighbor(self.nodes[dest_key],\n",
|
||||||
" weight)\n",
|
" weight)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def add_undirected_edge(self, source_key, dest_key, weight=0):\n",
|
" def add_undirected_edge(self, src_key, dst_key, weight=0):\n",
|
||||||
" if source_key is None or dest_key is None:\n",
|
" if src_key is None or dst_key is None:\n",
|
||||||
" raise Exception('Invalid key')\n",
|
" raise TypeError('key cannot be None')\n",
|
||||||
" self.add_edge(source_key, dest_key, weight)\n",
|
" self.add_edge(src_key, dst_key, weight)\n",
|
||||||
" self.nodes[dest_key].add_neighbor(self.nodes[source_key],\n",
|
" self.add_edge(dst_key, src_key, weight)"
|
||||||
" weight)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user