简体   繁体   English

如果给定一个整数列表和一个名为 x 的数字,如何递归返回列表中每个第 x 个数字的总和

[英]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 = 2nums = [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.

相关问题 如何递归地对列表中的每个第 n 个数字求和? - How can i sum every nth number from list recursively? 如何编写带列表和数字 X 的 function。如果 X 存在于索引“m”的列表中,它应该从索引“m”返回列表元素的总和 - How to write function that take a list and number X .If X exist in list in index ‘m’, it should return sum of elements of list from index ‘m’ 如何在满足给定条件的字符串列表列表中返回计算字符串总数的整数列表? - How to return a list of integers that count total number of strings in a list of list of list of strings that satisfy a given condition? 对数字列表求和,直到找到数字X - Sum a list of numbers until a number X is found 如何返回给定字符串中的(x)个字符? - How to return (x) number of characters in a given string? 返回给定数字的列表 - return a list for the given number 如何在列表中找到不是 X 数的最大数? - How to find the largest number in a list that is not X number? 给定一个包含 X 个浮点数的列表,返回中间 3 个浮点数的平均值和该列表的最低浮点数的元组 - Given a list of X number of floats, return a tuple of the average of the middle 3 floats and the lowest float of that list 如果给定数字是列表中存在的两个不同数字的总和,如何仅一次返回“True”? - How to return 'True' if given number is the sum of two different number present in the list , in one pass only? 如何将列表中的每第 10 个数字除以 10? - How to divide every 10th number by 10 in a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM