Graph 1 - DO NOT MERGE#7
Open
markreynoso wants to merge 130 commits into
Open
Conversation
… print - not done
nhuntwalker
reviewed
Dec 7, 2017
|
|
||
| def add_node(self, val): | ||
| """Add a node with value of val to graph.""" | ||
| self._graph[val] = {} |
There was a problem hiding this comment.
So if I add the same node twice, I'm going to overwrite what I had before?
| self._graph[val1][val2] = weight | ||
| else: | ||
| self._graph[val1] = {val2: weight} | ||
| self._graph[val2] = {} |
There was a problem hiding this comment.
You could refactor this whole set of if statements to ask questions and do operations like...
is val1 in the graph? if not, add it
is val2 in the graph? if not, add it
is val2 one of val1's neighbors? if not, add it as a neighbor with the given weight. If so, re-assign the weight of that edge to be the given weight.
| for key in self._graph: | ||
| for i in self._graph[key]: | ||
| if i == val: | ||
| del self._graph[key][val] |
There was a problem hiding this comment.
if val in self._graph:
del self._graph[val]
for key in self._graph:
if val in self._graph[key]:
del self._graph[key][val]| if val in self._graph: | ||
| return True | ||
| else: | ||
| return False |
| if child == val and key not in neighbors: | ||
| neighbors.append(key) | ||
| else: | ||
| return neighbors |
There was a problem hiding this comment.
"neighbors" only goes one way. If "val" points to some other node in the graph, that other node is "val"'s neighbor. However, "val" is not that node's neighbor.
| else: | ||
| return neighbors | ||
| except KeyError: | ||
| raise ValueError('This node dosent exit') |
| if val2 in self._graph[val1]: | ||
| return True | ||
| else: | ||
| return False |
| else: | ||
| return False | ||
| else: | ||
| raise ValueError('These edges do not exist.') |
There was a problem hiding this comment.
That's not why this error is being raised.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.