2

I am using recursion with backtracking, i have used multiple recursion call

My code snippet : -

func test(currentIndex: Int, op: inout [Int]) {
    if currentIndex > 1 {
        print("exit here**********************")
        return
    }
    print("##before", currentIndex)
    test(currentIndex: currentIndex+1, op: &op)
    print("after------------------------------------------------------", currentIndex)
    test(currentIndex: currentIndex+1, op: &op)
    print("^^^Final ================  ", currentIndex)
}

var op1: [Int] = []
test(currentIndex: 0, op: &op1)

It prints ->

##before 0
##before 1
exit here**********************
after------------------------------------------------------ 1
exit here**********************
^^^Final ================   1
after------------------------------------------------------ 0
##before 1
exit here**********************
after------------------------------------------------------ 1
exit here**********************
^^^Final ================   1
^^^Final ================   0

I am not able to understand last 4 print statements.

Amit
  • 556
  • 1
  • 7
  • 24
  • 5
    I suggest you step through your code with the debugger. It's an important learning experience, I think: you'll understand the output once you see how the code flows step-by-step (search for Swift debugger tutorials if you don't know how to use it yet). – DarkDust Nov 14 '22 at 16:37
  • 1
    +1 to just stepping through the debugger. Indenting it helps you see the tree-like nature of the callstack over time, which can also be useful. https://gist.github.com/amomchilov/8a8f10030037c5245e799702b90ab5d3 – Alexander Nov 14 '22 at 18:18

1 Answers1

1

You have:

func test(currentIndex: Int, op: inout [Int]) {
    if currentIndex > 1 {
        print("exit here**********************")
        return
    }
    print("##before", currentIndex)
    test(currentIndex: currentIndex+1, op: &op) //First
    print("after------------------------------------------------------", currentIndex)
    test(currentIndex: currentIndex+1, op: &op) //Second
    print("^^^Final ================  ", currentIndex)
}

var op1: [Int] = []
test(currentIndex: 0, op: &op1)

Let's manually analyze what's doing the last line which launch the test. We'll copy/paste the code, as we were debugging, if we were manually writing all the lines. I used indent to keep clarity. It's making the "trace" of your code.

//Index is 0:
if currentIndex > 1 {} //Skipped

print("##before", currentIndex)
test(currentIndex: currentIndex+1, op: &op) //First

    //Index is 1
    if currentIndex > 1 {} //Skipped

    print("##before", currentIndex)
    test(currentIndex: currentIndex+1, op: &op) //First

        //Index is 2
        if currentIndex > 1 {
            print("exit here**********************")
            return
        }

    print("after------------------------------------------------------", currentIndex)
    test(currentIndex: currentIndex+1, op: &op)  //Second

        //Index is 2
        if currentIndex > 1 {
            print("exit here**********************")
            return
        }

    print("^^^Final ================  ", currentIndex)

print("after------------------------------------------------------", currentIndex)
test(currentIndex: currentIndex+1, op: &op) //Second
    //Index is 1
    if currentIndex > 1 {} //Skipped

    print("##before", currentIndex)
    test(currentIndex: currentIndex+1, op: &op) //First

        //Index is 2
        if currentIndex > 1 {
            print("exit here**********************")
            return
        }

    print("after------------------------------------------------------", currentIndex)
    test(currentIndex: currentIndex+1, op: &op)  //Second

        //Index is 2
        if currentIndex > 1 {
            print("exit here**********************")
            return
        }

    print("^^^Final ================  ", currentIndex)

print("^^^Final ================  ", currentIndex) 

So you get:

##before 0 
##before 1 
exit here**********************
after------------------------------------------------------ 1
exit here********************** 
^^^Final ================   1
after------------------------------------------------------ 0
##before 1
exit here**********************
after------------------------------------------------------ 1
exit here**********************
^^^Final ================   1
^^^Final ================   0

You can also use the debugger, put a breakpoint, see the callstack...

Larme
  • 24,190
  • 6
  • 51
  • 81