[英]How to if given a list of integers and a number called x, return recursively the sum of every x'th number in the list
So, How to if given a list of integers and a number called x, return recursively the sum of every x'th number in the list.那么,如果给定一个整数列表和一个名为 x 的数字,如何递归返回列表中每个第 x 个数字的总和。
In this task "indexing" starts from 1, so if x = 2
and nums = [2, 3, 4, -9]
, the output should be -6
(3 + -9).在此任务中,“索引”从 1 开始,因此如果x = 2
且nums = [2, 3, 4, -9]
,则 output 应为-6
(3 + -9)。
X can also be negative, in that case indexing starts from the end of the list, see examples below. X 也可以是负数,在这种情况下,索引从列表的末尾开始,请参见下面的示例。
If x = 0
, the sum should be 0
as well.如果x = 0
,则总和也应为0
。
For example:例如:
print(x_sum_recursion([], 3)) # 0
print(x_sum_recursion([2, 5, 6, 0, 15, 5], 3)) # 11
print(x_sum_recursion([0, 5, 6, -5, -9, 3], 1)) # 0
print(x_sum_recursion([43, 90, 115, 500], -2)) # 158
print(x_sum_recursion([1, 2], -9)) # 0
print(x_sum_recursion([2, 3, 6], 5)) # 0
print(x_sum_recursion([6, 5, 3, 2, 9, 8, 6, 5, 4], 3)) # 15
I have been trying to do this function for 5 hours straight!!!我一直在尝试这样做 function 连续 5 个小时!!!
Would like do see how someone else would solve this.想看看其他人会如何解决这个问题。
This is the best i have come up with.这是我想出的最好的。
def x_sum_rec_Four(nums: list, x: int) -> int:
if len(nums) == 0:
return 0
elif len(nums) < x:
return 0
elif x > 0:
i = x - 1
return nums[i] + x_sum_rec_Four(nums[i + x:], x)
elif x < 0:
return x_sum_rec_Four(nums[::-1], abs(x))
My problem with this recursion is that the finish return should be:我对这个递归的问题是完成返回应该是:
if len(nums) < x:
return nums[0]
but that would pass things like ([2, 3, 6], 5)) -->> 2
when it should be 0.但这会传递([2, 3, 6], 5)) -->> 2
之类的东西,而它应该是 0。
If you really need to do it recursively, you could pop x-1 elements from the list before each call, follow the comments below:如果您确实需要递归执行此操作,您可以在每次调用之前从列表中弹出 x-1 个元素,请遵循以下评论:
def x_sum_recursion(nums, x):
# if x is negative, call the function with positive x and reversed list
if x < 0:
return x_sum_recursion(nums[::-1], abs(x))
# base case for when x is greater than the length of the list
if x > len(nums):
return 0
# otherwise remove the first x-1 items
nums = nums[x-1:]
# sum the first element and remove it from the next call
return nums[0] + x_sum_recursion(nums[1:], x)
print(x_sum_recursion([], 3)) # 0
print(x_sum_recursion([2, 5, 6, 0, 15, 5], 3)) # 11
print(x_sum_recursion([0, 5, 6, -5, -9, 3], 1)) # 0
print(x_sum_recursion([43, 90, 115, 500], -2)) # 158
print(x_sum_recursion([1, 2], -9)) # 0
print(x_sum_recursion([2, 3, 6], 5)) # 0
print(x_sum_recursion([6, 5, 3, 2, 9, 8, 6, 5, 4], 3)) # 15
However, you can do it in a one liner simple and pythonic way:但是,您可以通过一种简单且 Pythonic 的方式来完成它:
print(sum(nums[x-1::x] if x > 0 else nums[x::x]))
Explanation:解释:
you slice the list using nums[start:end:increment]
when you leave the end empty it slices from the starting position till the end of the list and increments by the increment specified您使用nums[start:end:increment]
对列表进行切片,当您将结尾留空时,它将从起始 position 切片到列表末尾,并按指定的增量递增
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.