0

I've been trying to code 3 different recursive function(Backtracking). The first one should show all permutations(orders doesn't matters) and there's a specif size. The second one should show all cyclic permutations. The third one should show all permutations(orders matters) and there's a specif size.

I was able to do the first one by myself:

def Arranjo(list, repeat):
    if repeat <= 0:
        return [[]]
    solution = []
    for i in range(0, len(list)):
        remainingList = list[:i] + list[i + 1 :]
        for j in Arranjo(remainingList, repeat - 1):
            solution.append([list[i]] + j)
    return solution


# 0 to 100
list = [x for x in range(1, 101)]

print(Arranjo(list, 3))

I've been facing some issues in the other two exercises. I've already spent hours trying to do the second exercise, but I don't have any idea about how to do it. That's what I was able to do:

def PermutacaoCircular(list):
    if len(list) == 0:
        return
    elif len(list) == 1:
        return list
    else:
        solution = []
        sub_solution = []
        for i in range(len(list)):
            // code here

    return solution

list = [1, 2, 3]
print(PermutacaoCircular(list))

The third one, I don't even know how to start.

Sule26
  • 1
  • 2
  • What have you tried? Is there any reason why you can't use methods from `itertools`? – BrokenBenchmark Apr 05 '22 at 19:12
  • @BrokenBenchmark I tried to get N numbers from the beginning of the list and put them at the end, but I don't know how to insert the recursive part on it. Yeah, there is. I my studying recursive functions at College, and those exercises are for practice. – Sule26 Apr 05 '22 at 21:15

0 Answers0