Skip to content

Instantly share code, notes, and snippets.

@alexgolec
Last active September 3, 2019 21:42
Show Gist options
  • Save alexgolec/1ba52c463a4f40483852c64260d32561 to your computer and use it in GitHub Desktop.
Save alexgolec/1ba52c463a4f40483852c64260d32561 to your computer and use it in GitHub Desktop.
class RateGraph(object):
def __init__(self, rates):
'Initialize the graph from an iterable of (start, end, rate) tuples.'
self.graph = {}
for orig, dest, rate in rates:
self.add_conversion(orig, dest, rate)
def add_conversion(self, orig, dest, rate):
'Insert a conversion into the graph.'
if orig not in self.graph:
self.graph[orig] = {}
self.graph[orig][dest] = rate
def get_neighbors(self, node):
'Returns an iterable of the nodes neighboring the given node.'
if node not in self.graph:
return None
return self.graph[node].items()
def get_nodes(self):
'Returns an iterable of all the nodes in the graph.'
return self.graph.keys()
@pspeter
Copy link

pspeter commented Sep 3, 2019

The comment in line 10 contains spoilers ;)

@alexgolec
Copy link
Author

alexgolec commented Sep 3, 2019

whoooooooops, thanks for pointing that out

fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment