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
... | |
elif (w1, w2) in synonym_words: | |
continue | |
... |
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
... | |
synonyms = {} | |
for w1, w2 in synonym_words: | |
synonyms[w1] = w2 | |
... | |
elif synonyms[w1] == w2: | |
continue |
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
... | |
synonyms = defaultdict(set) | |
for w1, w2 in synonym_words: | |
synonyms[w1].append(w2) | |
synonyms[w2].append(w1) | |
... | |
elif w2 in synonyms.get(w1, tuple()): | |
continue |
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
... | |
synonyms = defaultdict(set) | |
for w1, w2 in synonym_words: | |
synonyms[w1].append(w2) | |
... | |
elif (w2 in synonyms.get(w1, tuple()) or | |
w1 in synonyms.get(w2, tuple())): | |
continue |
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 synonym_queries(synonym_words, queries): | |
''' | |
synonym_words: iterable of pairs of strings representing synonymous words | |
queries: iterable of pairs of strings representing queries to be tested for | |
synonymous-ness | |
''' | |
synonyms = defaultdict(set) | |
for w1, w2 in synonym_words: | |
synonyms[w1].add(w2) |
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
synonyms = defaultdict(set) | |
for w1, w2 in synonym_words: | |
for w in synonyms[w1]: | |
synonyms[w].add(w2) | |
synonyms[w1].add(w2) | |
for w in synonyms[w2]: | |
synonyms[w].add(w1) | |
synonyms[w2].add(w1) |
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
class DisjointSet(object): | |
def __init__(self): | |
self.parents = {} | |
def get_root(self, w): | |
words_traversed = [] | |
while self.parents[w] != w: | |
words_traversed.append(w) | |
w = self.parents[w] | |
for word in words_traversed: |
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
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.' |
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 __dfs_helper(rate_graph, node, end, rate_from_origin, visited): | |
if node == end: | |
return rate_from_origin | |
visited.add(node) | |
for unit, rate in rate_graph.get_neighbors(node): | |
if unit not in visited: |
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 bfs(rate_graph, start, end): | |
to_visit = deque() | |
to_visit.appendleft( (start, 1.0) ) | |
visited = set() | |
while to_visit: | |
node, rate_from_origin = to_visit.pop() | |
if node == end: |