This file contains 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 = { | |
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), | |
9: (2, 4), |
This file contains 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 yield_sequences(starting_position, num_hops, sequence=None): | |
if sequence is None: | |
sequence = [starting_position] | |
if num_hops == 0: | |
yield sequence | |
return | |
for neighbor in neighbors(starting_position): | |
yield from yield_sequences( |
This file contains 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 neighbors import neighbors | |
def count_sequences(start_position, num_hops): | |
if num_hops == 0: | |
return 1 | |
num_sequences = 0 | |
for position in neighbors(start_position): | |
num_sequences += count_sequences(position, num_hops - 1) | |
return num_sequences |
This file contains 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): | |
cache = {} | |
def helper(position, num_hops): | |
if (position, num_hops) in cache: | |
return cache[ (position, num_hops) ] | |
if num_hops == 0: | |
return 1 |
This file contains 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): |
This file contains 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 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 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 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 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): |
OlderNewer