-1

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.

HelpMe
  • 91
  • 10

1 Answers1

1

Simplest Logic I can think of:

  1. Identify the x, y co-ordinates of the destination. Starting co-ordinates being (0,0)
  2. Create a string with 'u' & 'r' each occurring the vertical and horizontal distances.
  3. Use itertools.permutations to find all possible permutations of the string above and append to a previously blank list.
  4. Print each unique element of the list.

Implementation code:

from itertools import permutations
from more_itertools import unique_everseen

x = (0,0)  # start point tuple
y = (1,2)  # End point tuple // You can also get this dynamically via user input, if needed
h = y[0] - x[0]  # horizontal distance (difference of x-coordinates of DST, SRC
v = y[1] - x[1]  # vertical distance (difference of y-coordinates of DST, SRC
plist = [] # blank list to store permutation result
path = 'u'*h + 'r'*v  # gives result as 'uur' (two up and one right, in this case)
for  subset in (permutations(path, h+v)): # use permutations on the path string
    plist.append(subset)                  # append each result to the plist
# print (plist)                           // Optional to verify the code
for item in list(unique_everseen(plist)):
    print ("{}\n".format(item))           # Print unique values from plist in a new line

For some reason, the permutations module is returning duplicates. Hence the use of 'unique_everseen'. Hope this is what you wanted.

ParvBanks
  • 1,316
  • 1
  • 9
  • 15
  • Please be careful while using very distant points since it returns a huge list and takes up too much memory while doing so. – ParvBanks Dec 05 '18 at 11:28
  • I got your way, it's cool. Sorry, I didn't mention it but I mustn't use some module to solve the problem. How can I overcome the modules? Thank you! – HelpMe Dec 05 '18 at 11:29
  • You could use the function definition for permutation to your code if not the module. See documentation: https://docs.python.org/3/library/itertools.html#itertools.permutations For unique values, there can be an easier workaround, if needed. – ParvBanks Dec 05 '18 at 11:32
  • BTW, can you include that stipulation in your question for other patrons in the community? – ParvBanks Dec 05 '18 at 11:33
  • Edited. I didn't learn the method of yield so I don't understand the code you have sent but I'll try something. Thanks. – HelpMe Dec 05 '18 at 11:35
  • yield is like return that is used to recursively return values from a loop. You should read more about it. – ParvBanks Dec 05 '18 at 11:37