嗨,有人能帮我理解代码里发生了什么吗?

2024-03-29 05:53:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道这是一个递归函数,它按顺序返回表示数n的方法数,即不大于k的数之和,但我不明白它是如何实现的。你知道吗

def all_sums(n, k):

    if n == 0:
        return 1
    elif n < 0: 
        return 0
    else:
        res = 0
        for i in range(1, k+1):
            res = res + all_sums(n-i, k)
        return res

Tags: 方法inforreturnif顺序defrange
1条回答
网友
1楼 · 发布于 2024-03-29 05:53:19

首先,您应该知道这个函数是递归的。基本上,这意味着代码用不同的数字调用自己。递归的一个很好的例子是fibbonacci序列,它将序列中前面的两个数字相加得到下一个数字。其代码是:

def F(n):
    if n == 0:
        return 0 #base case no.1 because there are no previous elements to add together.
    elif n == 1:
        return 1 #base case no.2 because there are not enough previous elements to add together.
    else:
        return F(n-1)+F(n-2) #adds together the result of whatever the last number in the sequence was to whatever the number before that was.

一旦理解了递归的工作原理,就可以简单地跟踪代码。如果这在精神上很难做到,试着在一张纸上画出来。我发现在编写任何程序时,这通常都是一种有用的策略。因为这个原因,我总是在电脑旁边放一张纸和一支铅笔。让我们一起快速运行一下代码,以便大致了解发生了什么:

def all_sums(n,k):

在这里,我们定义方法,并将参数nk传递给它。你知道吗

if n == 0:
    return 1

这是递归函数的“基本情况”,本质上是为了确保代码不会永远运行,并关闭函数。你知道吗

elif n < 0:
    return 0

这表明如果n小于0,则返回0(因为n不能为负)。这被认为是一个“特殊情况”,以防止有人意外地搞砸了程序。你知道吗

else:
    res = 0

如果没有其他“特殊情况”发生,请执行以下所有操作。首先,我们将变量设置为0。你知道吗

for i in range(1, k+1):
    res = res + all_sums(n-i, k)

调用一个for循环,该循环从1开始,通过每个整数直到(但不包括)k+1。对于每个迭代,它将res变量设置为res在调用同一函数的结果之前的任何值,使用n-i作为第一个变量。你知道吗

return res

这段代码只是在for循环完成后输出res的结果。你知道吗

如果您想了解代码是如何工作的,可以向代码的各个部分添加print语句,并观察它的输出。另外,如果这让你感到困惑的话,你可能想读一下recursion。你知道吗

编辑

下面是all_sums(3,3)的基本运行,使用伪代码。但是,首先,这里是您的代码,添加了一些注释和print语句(这是我在一个名为测试.py“:”

def all_sums(n, k):

    if n == 0: #base case 1
        return 1
    elif n < 0: #base case 2
        return 0
    else: #recursive case
        res = 0
        for i in range(1, k+1):
            res = res + all_sums(n-i, k)
        print res #output res to the screen
        return res

print all_sums(3,3)

这是我的代码。请注意,每次降低一个级别时,res都是一个不同的变量,这取决于变量的范围。每次我签入时,都是在新函数调用中运行代码的时候。你知道吗

all_sums(3,3):
    res = 0
    0 + all_sums((3-1),3)
        res = 0
        0 + all_sums((2-1),3)
            res = 0
            0 + all_sums((1-1),3)
                returning 1 #first base case
            1 + all_sums((1-2),3)
                returning 0 #second base case
            1 + all_sums((1-3),3)
                returning 0 #second base case
            PRINTING 1 TO THE SCREEN
            returning 1 #end of recursive case
        1 + all_sums((2-2),3)
            returning 1 #first base case
        2 + all_sums((2-3),3)
            returning 0 #second base case
        PRINTING 2 TO THE SCREEN
        returning 2 #end of recursive case
    2 + all_sums((3-2),3)
        res = 0
        0 + all_sums((1-1),3)
            returning 1 #first base case
        1 + all_sums((1-2),3)
            returning 0 #second base case
        1 + all_sums((1-3),3)
            returning 0 #second base case
        PRINTING 1 TO THE SCREEN
        returning 1 #end of recursive case
    3 + all_sums((3-3),3)
        returning 1 #first base case
    PRINTING 4 TO THE SCREEN
    returning 4 #end of recursive
returning 4 #end of recursive case (and your original function call)

PRINTING 4 TO THE SCREEN AS THE RESULT OF all_sums(3,3)

相关问题 更多 >