0

I want N number of nested for loops, the last for loop should have number += 1 while everything else should have push(move) and should end with a undo() I saw a way to solve in another language that said something about recursion.

The following example if for 5 for loops:

number = 0
for move in all_legal_moves():
    push(move)
    for move2 in all_legal_moves():
        push(move2)
        for move3 in all_legal_moves():
            push(move3)
            for move4 in all_legal_moves():
                push(move4)
                for move5 in all_legal_moves():
                    number += 1
                undo()
            undo()
        undo()
    undo()

I want a function that takes the depth (the number of nested for loops (N)) and return number. I would prefer an answer that doesn't imports any libraries

Johan Jomy
  • 592
  • 2
  • 7
  • 22
  • 1
    Have you looked at recursion yourself at all? Can you share your own attempt at making this a recursive function? Or is your question how to write a recursive function at all? Or even a function at all? – Grismar Oct 23 '21 at 05:28
  • i think you will find [this Q&A](https://stackoverflow.com/a/65512563/633183) to be helpful – Mulan Oct 23 '21 at 07:56

1 Answers1

2

Recursion is simply a function keeps calling itself until it meets certain condition. You can learn more here. I think in this case you can make the code above recursive by having it either push + go to another recursion level + undo or number += 1 by checking if the current loop level (level in the code below) is equal to N (max_level in the code below)

number = 0

def moves(level, max_level):
    global number
    if level < max_level:
        for move in all_legal_moves():
            push(move)
            moves(level + 1, max_level)
            undo()
    else:
        number += 1


moves(move, 1, 5)
print('Number of possible moves:', number)

Or if you don't want to have number as a global variable, you can make the function return the number of possible iterations instead

def moves(level, max_level):
    number = 0
    if level < max_level:
        for move in all_legal_moves():
            push(move)
            number += moves(level + 1, max_level)
            undo()
    else:
        number += 1
    return number

number_of_possible_moves = moves(move, 1, 5)
print('Number of possible moves', number_of_possible_moves)
tax evader
  • 2,082
  • 1
  • 7
  • 9
  • what should i give for the ```move``` parameter? and the inner calling should be ```moves(move, level + 1, max_level)``` right – Johan Jomy Oct 23 '21 at 05:42
  • 1
    I removed the ```move``` parameter and it works now thanks! – Johan Jomy Oct 23 '21 at 05:45
  • Oh right, I changed the function parameters from 2 to 3 parameters but I haven't changed it where it's being called. Yeah removing `move` parameter should work. Thanks for point out! – tax evader Oct 23 '21 at 05:55
  • 1
    oh and ```number_of_moves(depth, level=0)``` might be a better way. In the function ```number += number_of_moves(depth, level+1)``` and for calling the function ```number_of_possible_moves = number_of_moves(3)``` would be easier, not a big deal but still and thanks a lot – Johan Jomy Oct 23 '21 at 06:01