mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Update graph remove_neighbor error handling (#93)
This commit is contained in:
parent
0e866dc7c0
commit
113eddd3c3
|
@ -30,14 +30,11 @@ class Node:
|
||||||
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:
|
if neighbor is None or neighbor.key not in self.adj_nodes:
|
||||||
raise Exception('Invalid neighbor')
|
|
||||||
if neighbor.key in self.adj_nodes:
|
|
||||||
neighbor.incoming_edges -= 1
|
|
||||||
del self.adj_weights[neighbor.key]
|
|
||||||
del self.adj_nodes[neighbor.key]
|
|
||||||
else:
|
|
||||||
raise Exception('Invalid neighbor')
|
raise Exception('Invalid neighbor')
|
||||||
|
neighbor.incoming_edges -= 1
|
||||||
|
del self.adj_weights[neighbor.key]
|
||||||
|
del self.adj_nodes[neighbor.key]
|
||||||
|
|
||||||
|
|
||||||
class Graph:
|
class Graph:
|
||||||
|
|
|
@ -194,14 +194,11 @@
|
||||||
" 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:\n",
|
" if neighbor is None or neighbor.key not in self.adj_nodes:\n",
|
||||||
" raise Exception('Invalid neighbor')\n",
|
|
||||||
" if neighbor.key in self.adj_nodes:\n",
|
|
||||||
" neighbor.incoming_edges -= 1\n",
|
|
||||||
" del self.adj_weights[neighbor.key]\n",
|
|
||||||
" del self.adj_nodes[neighbor.key]\n",
|
|
||||||
" else:\n",
|
|
||||||
" raise Exception('Invalid neighbor')\n",
|
" raise Exception('Invalid neighbor')\n",
|
||||||
|
" neighbor.incoming_edges -= 1\n",
|
||||||
|
" del self.adj_weights[neighbor.key]\n",
|
||||||
|
" del self.adj_nodes[neighbor.key]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class Graph:\n",
|
"class Graph:\n",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user