mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Update graph challenge (#122)
Simplify add_node logic. Use more specific TypeError Exception.
This commit is contained in:
parent
430cca2a01
commit
520aa89555
|
@ -47,20 +47,18 @@ class Graph:
|
||||||
def add_node(self, key):
|
def add_node(self, key):
|
||||||
if key is None:
|
if key is None:
|
||||||
raise TypeError('key cannot be None')
|
raise TypeError('key cannot be None')
|
||||||
if key in self.nodes:
|
if key not in self.nodes:
|
||||||
return self.nodes[key]
|
self.nodes[key] = Node(key)
|
||||||
self.nodes[key] = Node(key)
|
|
||||||
return self.nodes[key]
|
return self.nodes[key]
|
||||||
|
|
||||||
def add_edge(self, source_key, dest_key, weight=0):
|
def add_edge(self, source_key, dest_key, weight=0):
|
||||||
if source_key is None or dest_key is None:
|
if source_key is None or dest_key is None:
|
||||||
raise Exception('Invalid key')
|
raise KeyError('Invalid key')
|
||||||
if source_key not in self.nodes:
|
if source_key not in self.nodes:
|
||||||
self.add_node(source_key)
|
self.add_node(source_key)
|
||||||
if dest_key not in self.nodes:
|
if dest_key not in self.nodes:
|
||||||
self.add_node(dest_key)
|
self.add_node(dest_key)
|
||||||
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, src_key, dst_key, weight=0):
|
def add_undirected_edge(self, src_key, dst_key, weight=0):
|
||||||
if src_key is None or dst_key is None:
|
if src_key is None or dst_key is None:
|
||||||
|
|
|
@ -215,20 +215,18 @@
|
||||||
" def add_node(self, key):\n",
|
" def add_node(self, key):\n",
|
||||||
" if key is None:\n",
|
" if key is None:\n",
|
||||||
" raise TypeError('key cannot be None')\n",
|
" raise TypeError('key cannot be None')\n",
|
||||||
" if key in self.nodes:\n",
|
" if key not in self.nodes:\n",
|
||||||
" return self.nodes[key]\n",
|
" self.nodes[key] = Node(key)\n",
|
||||||
" self.nodes[key] = Node(key)\n",
|
|
||||||
" return self.nodes[key]\n",
|
" return self.nodes[key]\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def add_edge(self, source_key, dest_key, weight=0):\n",
|
" def add_edge(self, source_key, dest_key, weight=0):\n",
|
||||||
" if source_key is None or dest_key is None:\n",
|
" if source_key is None or dest_key is None:\n",
|
||||||
" raise Exception('Invalid key')\n",
|
" raise KeyError('Invalid key')\n",
|
||||||
" if source_key not in self.nodes:\n",
|
" if source_key not in self.nodes:\n",
|
||||||
" self.add_node(source_key)\n",
|
" self.add_node(source_key)\n",
|
||||||
" if dest_key not in self.nodes:\n",
|
" if dest_key not in self.nodes:\n",
|
||||||
" self.add_node(dest_key)\n",
|
" self.add_node(dest_key)\n",
|
||||||
" self.nodes[source_key].add_neighbor(self.nodes[dest_key],\n",
|
" self.nodes[source_key].add_neighbor(self.nodes[dest_key], weight)\n",
|
||||||
" weight)\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" def add_undirected_edge(self, src_key, dst_key, weight=0):\n",
|
" def add_undirected_edge(self, src_key, dst_key, weight=0):\n",
|
||||||
" if src_key is None or dst_key is None:\n",
|
" if src_key is None or dst_key is None:\n",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user