简体   繁体   中英

I don't understand how this function works and how it comes to the result of 60

def A(x, y):
    if x == 0:
        return y + 2
    if y == 0:
        return A(x - 1, 1) + 1
    return A(x - 1, A(x, y - 1)) * 2

print(A(1, 3))

The output is 60 . I ran the code but I have no clue how you get to that value. Sorry for the rather dumb question.

A appears to be recursive, so when you call A(1,3) , it keeps calling itself.

Let's walk through it:

  • The first time you run it, x != 0 , so it doesn't return y+2

  • y != 0 , so it doesn't return A(x-1, 1) + 1

  • Instead, it returns A(x-1, A(x, y-1)) * 2

A view of this can be summarised as:

A(1,3):
    return A(x-1, A(x, y-1)) * 2
    return A(0, A(1, 2)) * 2:
        A(1,2):
            return A(x-1, A(x, y-1)) * 2
            return A(0, A(1, 1)) * 2:
                A(1,1):
                    return A(x-1, A(x, y-1)) * 2
                    return A(0, A(1, 0)) * 2:
                        A(1,0):
                            return A(x-1, 1) + 1
                            return A(0, 1) + 1:
                                A(0,1):
                                    return y+2
                                    return 3
                                3 + 1 = 4
                                return 4
                    return A(0,4) * 2:
                        A(0,4):
                            return y+2
                            return 6
                        6*2 = 12
                    return 12
            return A(0, 12) * 2:
                A(0,12):
                    return y+2
                    return 14
                14*2 = 28
            return 28
    return A(0, 28) * 2:
        A(0,28):
            return y+2
            return 30
        30 * 2 = 60
    return 60

Hopefully that 'tree' helps you visualise what is going on.

Let's go through your program step by step:

  • First, we call the function A , with parameters 1, and 3.
  • The function A starts running and sets x to the first parameter, 1, and y to the second parameter, 3.
  • Execution then continues until it comes to the first if statement.
  • x is not 0, so we skip to the next statement, which is the second if statement.
  • y is not 0, so we skip to the next statement, which is the second call to the A function with the parameters x - 1 (which evaluates to 0), and A(x, y - 1) as the second parameter.
  • The second parameter results in a third call to A, with the parameters 1 and 2. This is computed first, then the result of it is set to the the second parameter of the second call of A
  • the second call to A hits the return statement, and returns the result to the first call to A. This value is then multiplied by 2, and the first call to A then returns it's value to the initial print function, which displays the number 60.

This can quickly get hard to keep track of by hand, so it's helpful to add a print statement, or use a debugger.

Here's an example of your function modified with a print statement:

def A(x, y):
    print('in A.  x:',x,'y:',y)
    if x == 0:
        return y + 2
    if y == 0:
        return A(x - 1, 1) + 1
    return A(x - 1, A(x, y - 1)) * 2

print(A(1, 3))

which produces the following output:

in A.  x: 1 y: 3
in A.  x: 1 y: 2
in A.  x: 1 y: 1
in A.  x: 1 y: 0
in A.  x: 0 y: 1
in A.  x: 0 y: 4
in A.  x: 0 y: 12
in A.  x: 0 y: 28
60

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM