Created
September 13, 2019 14:18
-
-
Save gituser768/a1ad7798bef9e1ed0dcd0bdd68b74449 to your computer and use it in GitHub Desktop.
yield a stream of paths from start to end on a grid
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 locs_available_here(d, h, cur): | |
if cur[0] < d - 1: yield (cur[0] + 1, cur[1]) | |
if cur[1] < h - 1: yield (cur[0], cur[1] + 1) | |
def new_paths(d, h, path_so_far, to_loc): | |
end_loc = (d - 1, h - 1) | |
if to_loc == end_loc: | |
yield path_so_far + [end_loc] | |
else: | |
for new_loc in locs_available_here(d, h, to_loc): | |
yield from new_paths(d, h, path_so_far + [to_loc], to_loc=new_loc) | |
def get_stream(d, h): | |
cur = (0, 0) | |
path_so_far = [cur] | |
for new_loc in locs_available_here(d, h, cur): | |
yield from new_paths(d, h, path_so_far, to_loc=new_loc) | |
def main(): | |
stream_of_paths = get_stream(3, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment