简体   繁体   English

需要帮助理解python的递归

[英]Need help understanding recursion for python

I am new to recursion and want to know what is the logic to the following code.我是递归的新手,想知道以下代码的逻辑是什么。

The question is the powerSums question on hackerrank: https://www.hackerrank.com/challenges/the-power-sum/problem?isFullScreen=true问题是关于hackerrank的powerSums问题: https ://www.hackerrank.com/challenges/the-power-sum/problem ? isFullScreen = true

def powerSum(X, N, current = 1):
    pw = pow(current, N)
    if pw > X:
        return 0
    elif pw == X:
        return 1
    else:
        return powerSum(X, N, current+1) + powerSum(X-pw, N, current+1)

Also, I had been trying to convert the answer to a nested list or tuple of the set of values, but cannot seem to figure it out.此外,我一直试图将答案转换为一组值的嵌套列表或元组,但似乎无法弄清楚。 How can this be done?如何才能做到这一点?

Eg, for a case where X=5 and N=2, the solution I want to yield is: [(10), (6,8), (1,3,4,5,7)] instead of 3, which the above code is yielding.例如,对于 X=5 和 N=2 的情况,我想要产生的解决方案是:[(10), (6,8), (1,3,4,5,7)] 而不是 3,其中上面的代码正在产生。

Your current code is returning the number of solutions.您当前的代码正在返回解决方案的数量。 There are two recursive calls, following the two base cases.在两个基本情况之后,有两个递归调用。 In the first recursive call, we're checking how many solutions exist for X without including current**N .在第一次递归调用中,我们正在检查X存在多少解决方案而不包括current**N The second recursive check tests how many solutions exist if we do include current**N .如果我们确实包含current**N则第二个递归检查测试存在多少解决方案。

If you want to return the actual solutions themselves (as a list of tuples), you'd need to change the code a bit.如果您想返回实际的解决方案本身(作为元组列表),您需要稍微更改代码。

Start with the base cases.从基本案例开始。 If pw > X , there's no solution with current this large, so we should return an empty list.如果pw > X ,没有current这么大的解决方案,所以我们应该返回一个空列表。 If pw == X , then we've found a solution, and should return a one-element list, containing the one element tuple (current,) (note the trailing comma, which is necessary to make the parentheses form a tuple, rather than just being for order of operations).如果pw == X ,那么我们已经找到了一个解决方案,并且应该返回一个单元素列表,其中包含单元素元组(current,) (注意尾随逗号,这是使括号形成一个元组所必需的,而不是而不仅仅是为了操作顺序)。

The recursive cases need to get a little more complicated too.递归情况也需要变得更复杂一些。 Both calls will return lists, which we want to combine.两个调用都将返回我们想要组合的列表。 For the first recursion, we can just use the returned list as is.对于第一次递归,我们可以直接使用返回的列表。 For the second one, we'll need to add our current value to the start of each tuple.对于第二个,我们需要将current值添加到每个元组的开头。 I suggest using a generator expression (though a list comprehension could also work if you wanted to concatenate the lists with + instead of using list.extend ).我建议使用生成器表达式(尽管如果您想使用+而不是使用list.extend连接列表,列表理解也可以工作)。

def powerSum(X, N, current = 1):
    pw = pow(current, N)
    if pw > X:
        return []
    elif pw == X:
        return [(current,)]
    else:
        result = powerSum(X, N, current+1)
        result.extend((current,) + tup for tup in powerSum(X-pw, N, current+1))
        return result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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