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
... | |
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 = {} | |
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
... | |
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
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 | |
''' | |
output = [] | |
for q1, q2 in queries: | |
q1, q2 = q1.split(), q2.split() | |
if len(q1) != len(q2): |
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 = [ | |
('rate', 'ratings'), | |
('approval', 'popularity'), | |
] | |
QUERIES = [ | |
('obama approval rate', 'obama popularity ratings'), | |
('obama approval rates', 'obama popularity ratings'), | |
('obama approval rate', 'popularity ratings obama') | |
] |
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
NEIGHBORS_MAP = { | |
0: (0, 0, 0, 0, 1, 0, 1, 0, 0, 0), | |
1: (0, 0, 0, 0, 0, 0, 1, 0, 1, 0), | |
2: (0, 0, 0, 0, 0, 0, 0, 1, 0, 1), | |
3: (0, 0, 0, 1, 0, 0, 0, 0, 1, 0), | |
4: (1, 0, 0, 1, 0, 0, 0, 0, 0, 1), | |
5: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0), | |
6: (1, 1, 0, 0, 0, 0, 0, 1, 0, 0), | |
7: (0, 0, 1, 0, 0, 0, 1, 0, 0, 0), | |
8: (0, 1, 0, 1, 0, 0, 0, 0, 0 ,0), |
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
NEIGHBORS_MAP = { | |
0: (4, 6), | |
1: (6, 8), | |
2: (7, 9), | |
3: (4, 8), | |
4: (3, 9, 0), | |
5: tuple(), # 5 has no neighbors | |
6: (1, 7, 0), | |
7: (2, 6), | |
8: (1, 3), |
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 matrix_multiply(A, B): | |
A_rows, A_cols = len(A), len(A[0]) | |
B_rows, B_cols = len(B), len(B[0]) | |
result = list(map(lambda i: [0] * B_cols, range(A_rows))) | |
for row in range(A_rows): | |
for col in range(B_cols): | |
for i in range(B_rows): | |
result[row][col] += A[row][i] * B[i][col] |
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 count_sequences(start_position, num_hops): | |
prior_case = [1] * 10 | |
current_case = [0] * 10 | |
current_num_hops = 1 | |
while current_num_hops <= num_hops: | |
current_case = [0] * 10 | |
current_num_hops += 1 | |
for position in range(0, 10): |