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
def add_conversion(self, orig, dest, rate): | |
'Insert a conversion into the graph. Note we insert its inverse also.' | |
if orig not in self.graph: | |
self.graph[orig] = {} | |
self.graph[orig][dest] = rate | |
if dest not in self.graph: | |
self.graph[dest] = {} | |
self.graph[dest][orig] = 1.0 / rate |
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
from collections import deque | |
def make_conversions(graph): | |
def conversions_bfs(rate_graph, start, conversions): | |
to_visit = deque() | |
to_visit.appendleft( (start, 1.0) ) | |
while to_visit: | |
node, rate_from_origin = to_visit.pop() | |
conversions[node] = (start, rate_from_origin) |
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
def convert(conversions, start, end): | |
'Given a conversion structure, performs a constant-time conversion' | |
try: | |
start_root, start_rate = conversions[start] | |
end_root, end_rate = conversions[end] | |
except KeyError: | |
return None | |
if start_root != end_root: | |
return None |
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
import argparse | |
import collections | |
import datetime | |
import openpyxl | |
import tableformatter | |
import re | |
import tda | |
OlderNewer