1

I want to write a recursive function that prints out the ways to reach a point where N and K are positive so for example if the point is (1,1) r= right u = up

So the function will print ur ,ru. If the point is (2,2) then the possible steps are: rruu, ruru, urru, uurr I have bulited a function that print all the combination that posibale of the r and u posibale(when it contains also rrrr or uuuu). but i need only the sequences that num of 'r' = num of 'u'. The func i have written suposse to help the function i wanted

def assist_print_sequences(char_list, last_seq, n):
    # base case
    if n > 0:
        for char in char_list:
            # looping over all the chars in list and adds them to last chars until n<1.
            assist_print_sequences(char_list, last_seq + char, n-1)
    # when n is smaller than 1.
    else:
        print(last_seq)
Imri Avni
  • 13
  • 2

1 Answers1

2

Seems that what you want is a permutation of target

target = ["r"] * n + ["u"] * k
target_string = "".join(target)

Then pick an answer from How to generate all permutations of a list in Python and you have all paths to the point you aim for, recursively (permutations can be found recursively)

ted
  • 13,596
  • 9
  • 65
  • 107