Created
July 8, 2018 01:36
-
-
Save franklinchou/e33d51849195cbc2929752eac495c202 to your computer and use it in GitHub Desktop.
Example of "bubble up" recursion
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 traverse_backward(): | |
def recurse(i): | |
if i == 0: | |
return | |
recurse(i - 1) | |
print(i) | |
recurse(5) | |
def traverse_forward(): | |
def recurse(i): | |
if i == 0: | |
return | |
print(i) | |
recurse(i - 1) | |
recurse(5) | |
if __name__ == "__main__": | |
traverse_forward() # Prints 5, 4, 3, 2, 1 | |
traverse_backward() # Prints 1, 2, 3, 4, 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment