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): | |
| 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 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 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 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 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 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 = { | |
| 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), |
NewerOlder