Last active
March 5, 2022 17:59
-
-
Save amencke/b4bbeb6d35d9b08cbc108d230a80a8ee to your computer and use it in GitHub Desktop.
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
➜ python python3 nxnrecurse.py | |
1 2 3 4 5 | |
2 4 6 8 10 | |
3 6 9 12 15 | |
4 8 12 16 20 | |
5 10 15 20 25 | |
➜ python vi nxnrecurse.py | |
➜ python cat nxnrecurse.py | |
def table(N): | |
def _helper(n): | |
if n == 0: # base case. We break the recursion here | |
return # return nothing because we're printing the solution | |
for i in range(1, N + 1): # 1, 2, 3, 4, 5. range() is inclusive of the first but exclusive of the last | |
print(i * ((N + 1) - n), end=" ") # N is always the value provided to the call to table() | |
# Use N+1 because i goes from 1 to 5. If we used N, we,' | |
# miss out on the 1 x 5, 2 x 5, 3 x 5 etc when N = 5. The | |
# end=" " part just stops a newline being printed after each | |
# individual number | |
print() # newline at the end of each recursive function call | |
_helper(n-1) # recursive call, each time decrementing the value of n | |
_helper(N) | |
table(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment