I have some problem to solve recursively without using any module and I need you to guide me please.
You are placed on a grid board at the origin (0, 0) and you want to reach to the destination n, k (That is, n moves rightward and k moves upward). We can only move one step rightward or upward a time. Implement a function that recieves two numbers n, k and print all the paths to reach to the destination n, k only by stepping rightward or upward. A step upward is represented by 'u' and rightward by 'r'. Every path has to be a sequence of chars u, r and every path has to be printed in a single line.
I have tried to do something:
def paths_ur(n, k):
paths_ur_helper(n, k, 0, 0)
def paths_ur_helper(n, k, right_moves, up_moves):
if right_moves == n and up_moves == k: #If we reach the destination there is nothing to return
return []
if right_moves < n and up_moves < k: #Check that we are in range of the board
return 'r' + paths_ur_helper(n, k, right_moves + 1, up_moves) +
\ +'u' + paths_ur_helper(n, k, right_moves, up_moves + 1)
But it goes wrong, probably because I don't imagine correctly the way recursion works...
Thanks.