From 2d98b8a496d7090054f4ca891b5c9270078ba805 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sun, 13 Nov 2016 07:32:33 -0500 Subject: [PATCH] Update graph challenge (#117) Use more specific exception types. Update constraints. Update add_undirected_edge to use add_edge. --- graphs_trees/graph/graph.py | 23 ++++++++++---------- graphs_trees/graph/graph_challenge.ipynb | 4 +++- graphs_trees/graph/graph_solution.ipynb | 27 ++++++++++++++---------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/graphs_trees/graph/graph.py b/graphs_trees/graph/graph.py index 03f0a62..755b4bb 100644 --- a/graphs_trees/graph/graph.py +++ b/graphs_trees/graph/graph.py @@ -23,15 +23,17 @@ class Node: return self.key < other.key def add_neighbor(self, neighbor, weight=0): - if neighbor is None: - raise Exception('Invalid neighbor') + if neighbor is None or weight is None: + raise TypeError('neighbor or weight cannot be None') neighbor.incoming_edges += 1 self.adj_weights[neighbor.key] = weight self.adj_nodes[neighbor.key] = neighbor def remove_neighbor(self, neighbor): - if neighbor is None or neighbor.key not in self.adj_nodes: - raise Exception('Invalid neighbor') + if neighbor is None: + raise TypeError('neighbor cannot be None') + if neighbor.key not in self.adj_nodes: + raise KeyError('neighbor not found') neighbor.incoming_edges -= 1 del self.adj_weights[neighbor.key] del self.adj_nodes[neighbor.key] @@ -44,7 +46,7 @@ class Graph: def add_node(self, key): if key is None: - raise Exception('Invalid key') + raise TypeError('key cannot be None') if key in self.nodes: return self.nodes[key] self.nodes[key] = Node(key) @@ -60,9 +62,8 @@ class Graph: self.nodes[source_key].add_neighbor(self.nodes[dest_key], weight) - def add_undirected_edge(self, source_key, dest_key, weight=0): - if source_key is None or dest_key is None: - raise Exception('Invalid key') - self.add_edge(source_key, dest_key, weight) - self.nodes[dest_key].add_neighbor(self.nodes[source_key], - weight) \ No newline at end of file + def add_undirected_edge(self, src_key, dst_key, weight=0): + if src_key is None or dst_key is None: + raise TypeError('key cannot be None') + self.add_edge(src_key, dst_key, weight) + self.add_edge(dst_key, src_key, weight) \ No newline at end of file diff --git a/graphs_trees/graph/graph_challenge.ipynb b/graphs_trees/graph/graph_challenge.ipynb index 21c61f8..091ae49 100644 --- a/graphs_trees/graph/graph_challenge.ipynb +++ b/graphs_trees/graph/graph_challenge.ipynb @@ -38,12 +38,14 @@ " * Implement both\n", "* Do the edges have weights?\n", " * Yes\n", - "* Can we assume the inputs are valid?\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", " * Yes\n", "* If we try to delete a node that doesn't exist, do we just do nothing?\n", " * Yes\n", + "* Can we assume the inputs are valid?\n", + " * Yes\n", "* Can we assume this fits memory?\n", " * Yes" ] diff --git a/graphs_trees/graph/graph_solution.ipynb b/graphs_trees/graph/graph_solution.ipynb index 7221c82..64ec832 100644 --- a/graphs_trees/graph/graph_solution.ipynb +++ b/graphs_trees/graph/graph_solution.ipynb @@ -37,10 +37,14 @@ " * Implement both\n", "* Do the edges have weights?\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", " * Yes\n", "* If we try to delete a node that doesn't exist, do we just do nothing?\n", " * Yes\n", + "* Can we assume the inputs are valid?\n", + " * Yes\n", "* Can we assume this fits memory?\n", " * Yes" ] @@ -187,15 +191,17 @@ " return self.key < other.key\n", "\n", " def add_neighbor(self, neighbor, weight=0):\n", - " if neighbor is None:\n", - " raise Exception('Invalid neighbor')\n", + " if neighbor is None or weight is None:\n", + " raise TypeError('neighbor or weight cannot be None')\n", " neighbor.incoming_edges += 1\n", " self.adj_weights[neighbor.key] = weight\n", " self.adj_nodes[neighbor.key] = neighbor\n", "\n", " def remove_neighbor(self, neighbor):\n", - " if neighbor is None or neighbor.key not in self.adj_nodes:\n", - " raise Exception('Invalid neighbor')\n", + " if neighbor is None:\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", " del self.adj_weights[neighbor.key]\n", " del self.adj_nodes[neighbor.key]\n", @@ -208,7 +214,7 @@ "\n", " def add_node(self, key):\n", " if key is None:\n", - " raise Exception('Invalid key')\n", + " raise TypeError('key cannot be None')\n", " if key in self.nodes:\n", " return self.nodes[key]\n", " self.nodes[key] = Node(key)\n", @@ -224,12 +230,11 @@ " self.nodes[source_key].add_neighbor(self.nodes[dest_key],\n", " weight)\n", "\n", - " def add_undirected_edge(self, source_key, dest_key, weight=0):\n", - " if source_key is None or dest_key is None:\n", - " raise Exception('Invalid key')\n", - " self.add_edge(source_key, dest_key, weight)\n", - " self.nodes[dest_key].add_neighbor(self.nodes[source_key],\n", - " weight)" + " def add_undirected_edge(self, src_key, dst_key, weight=0):\n", + " if src_key is None or dst_key is None:\n", + " raise TypeError('key cannot be None')\n", + " self.add_edge(src_key, dst_key, weight)\n", + " self.add_edge(dst_key, src_key, weight)" ] }, {