【每日一题20220213】局部汇总

:woman_mage: 给定一个纯数字列表,请编写一个函数,从索引0的位置开始,每个元素向后汇总计算和值,以此类推,直至汇总结果为0。返回所有汇总值组成的列表。

示例:
输入:[0, 1, 3, 6, 10]
过程:[0, 1, 3, 6, 10],和值:20
过程:[1, 3, 6, 10],和值:20
过程:[3, 6, 10],和值:19
过程:[6, 10],和值:16
过程:[10],和值:10
过程:[],和值:0

题目难度:简单
题目来源:CodeWars:Sums of Parts

def solution(nums: list) -> list:
    # your code here

assert solution([1, 2, 3, 4, 5, 6]) == [21, 20, 18, 15, 11, 6, 0]
assert solution([0, 1, 3, 6, 10]) == [20, 20, 19, 16, 10, 0]
assert solution([744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358]) == [10037855, 9293730, 9292795, 9292388, 9291934, 9291504, 9291414, 9291270, 2581057, 2580168, 2579358, 0]
def solution(nums: list) -> list:
    return [sum(ls[i:]) for i in range(len(ls)+1)]
1 Like
def solution(nums: list) -> list:
    # your code here
    list1 = [0]
    for i in range(0, len(nums) - 1):
        list1.append(list1[i] + nums[i])
    return list1.reverse()
def solution(nums: list) -> list:
    return [sum(nums[i:]) for i in range(len(nums) + 1)]


assert solution([1, 2, 3, 4, 5, 6]) == [21, 20, 18, 15, 11, 6, 0]
assert solution([0, 1, 3, 6, 10]) == [20, 20, 19, 16, 10, 0]
assert solution([744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358]) == [10037855, 9293730, 9292795,
                                                                                       9292388, 9291934, 9291504,
                                                                                       9291414, 9291270, 2581057,
                                                                                       2580168, 2579358, 0]
def solution(nums: list) -> list:
    return [sum(nums[i:len(nums)])  for i in range(len(nums) + 1) ]


assert solution([1, 2, 3, 4, 5, 6]) == [21, 20, 18, 15, 11, 6, 0]
assert solution([0, 1, 3, 6, 10]) == [20, 20, 19, 16, 10, 0]
assert solution([744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358]) == [10037855, 9293730, 9292795,
                                                                                       9292388, 9291934, 9291504,
                                                                                       9291414, 9291270, 2581057,
                                                                                       2580168, 2579358, 0]

def solution(nums: list) -> list:
    res = []
    for i in range(len(nums)):
        res.append(sum(nums[i:]))
    else:
        res.append(0)
    return res

用生成器试试

def solution(nums: list) -> list:
    def sum_a(nums):
        sum_a = 0
        yield sum_a
        len_n = len(nums)
        for i in range(len_n):
            sum_a = sum_a + nums[len_n - i - 1]
            yield sum_a
    new_list = []
    for j in sum_a(nums):
        print(j)
        new_list.insert(0, j)
    return new_list


assert solution([1, 2, 3, 4, 5, 6]) == [21, 20, 18, 15, 11, 6, 0]
assert solution([0, 1, 3, 6, 10]) == [20, 20, 19, 16, 10, 0]
assert solution([744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358]) == [10037855, 9293730, 9292795, 9292388, 9291934, 9291504, 9291414, 9291270, 2581057, 2580168, 2579358, 0]
def solution(nums: list) -> list:
    # 0213 局部汇总
    res = []
    while len(nums) > 0:
        res.append(sum(nums))
        nums.remove(nums[0])
    res.append(0)
    print(res)
    return res

assert solution([1, 2, 3, 4, 5, 6]) == [21, 20, 18, 15, 11, 6, 0]
assert solution([0, 1, 3, 6, 10]) == [20, 20, 19, 16, 10, 0]
assert solution([744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358]) == [10037855, 9293730, 9292795, 9292388, 9291934, 9291504, 9291414, 9291270, 2581057, 2580168, 2579358, 0]

def solution(nums: list,) -> list:
    res_list = []
    for i in range(len(nums)+1):
        res = 0
        for j in range(i, len(nums)):
            res += nums[j]
            if i == len(nums):
                res = 0
        res_list.append(res)
    return res_list


assert solution([1, 2, 3, 4, 5, 6]) == [21, 20, 18, 15, 11, 6, 0]
assert solution([0, 1, 3, 6, 10]) == [20, 20, 19, 16, 10, 0]
assert solution([744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358]) == [10037855, 9293730, 9292795,
                                                                                       9292388, 9291934, 9291504,
                                                                                       9291414, 9291270, 2581057,
                                                                                       2580168, 2579358, 0]
def solution(nums: list) -> list:
    # your code here
    list_sum = []
    for i in range(len(nums)):
        list_sum.append(sum(nums[i:]))
    list_sum.append(0)
    return list_sum


assert solution([1, 2, 3, 4, 5, 6]) == [21, 20, 18, 15, 11, 6, 0]
assert solution([0, 1, 3, 6, 10]) == [20, 20, 19, 16, 10, 0]
assert solution([744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358]) == [10037855, 9293730, 9292795,
                                                                                       9292388, 9291934, 9291504,
                                                                                       9291414, 9291270, 2581057,
                                                                                       2580168, 2579358, 0]
    def solution(nums: list) -> list:
        ret_nums = []
        for i in range(len(nums)+1):
            ret_nums.append(sum(nums[i:]))
        return ret_nums
def solution(nums: list) -> list:
    return [sum(nums[i:]) for i in range(len(nums)+1)]

assert solution([1, 2, 3, 4, 5, 6]) == [21, 20, 18, 15, 11, 6, 0]
assert solution([0, 1, 3, 6, 10]) == [20, 20, 19, 16, 10, 0]
assert solution([744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358]) == [10037855, 9293730, 9292795, 9292388, 9291934, 9291504, 9291414, 9291270, 2581057, 2580168, 2579358, 0]