Created
December 3, 2020 21:21
-
-
Save jmwind/346b9980b3cd2f6bef3497fc7ae0a447 to your computer and use it in GitHub Desktop.
recursion example
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 makeSteps(s, path, result): | |
if s == 0: | |
path.append(0) | |
rev = [ele for ele in reversed(path)] | |
path_strings = [str(int) for int in rev] | |
val = "-".join(path_strings) | |
result.append(val) | |
print("Found a path: " + val) | |
if s >= 1: | |
p1 = path[:] | |
p1.append(s) | |
print("Taking 1 step while on step = " + str(s) + " current path = " + str(p1)) | |
makeSteps(s - 1, p1, result) | |
if s >= 2: | |
p1 = path[:] | |
p1.append(s) | |
print("Taking 2 steps while on step = " + str(s) + " current path = " + str(p1)) | |
makeSteps(s - 2, p1, result) | |
def walkTheSteps(n): | |
result = [] | |
makeSteps(n, [], result) | |
return result | |
paths = walkTheSteps(4) | |
print("Finished and found {0} paths: {1}".format(len(paths), paths)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment