给定的数字列表,检查是否存在添加到100的数字。将所有这些数字列出为列表列表

发布于 2025-02-01 01:25:53 字数 694 浏览 1 评论 0原文

Input : L=[1,50,16,34,8,42,99]

Output : [[50,8,42],[16,34,50],[99,1],[16,34,42,8]]

创建一个函数check_sum_100(l)

我能够找到当元素形成子阵列时总计100的元素的总和。但是我找不到[99,1]之类的元素列表。

这是我尝试实现的方法:

def check_sum_100(L):
    out=[]
    for i in range(0, len(L)):             
        for j in range(i, len(L)): 
            sum = 0 
            temp = []                       
            for k in range (i, j+1):
                sum = sum + L[k]            
                temp.append(L[k])
            if sum == 100:
                out.append(temp)
    return out


L = [1,50,16,34,8,42,99]

o = check_sum_100(L)

print(o)
Input : L=[1,50,16,34,8,42,99]

Output : [[50,8,42],[16,34,50],[99,1],[16,34,42,8]]

Create a function check_sum_100(L)

I am able to find the sum of elements that add up to 100 when the elements form sub-array. But I am not able to find list of elements like [99,1].

Here is what I tried to implement:

def check_sum_100(L):
    out=[]
    for i in range(0, len(L)):             
        for j in range(i, len(L)): 
            sum = 0 
            temp = []                       
            for k in range (i, j+1):
                sum = sum + L[k]            
                temp.append(L[k])
            if sum == 100:
                out.append(temp)
    return out


L = [1,50,16,34,8,42,99]

o = check_sum_100(L)

print(o)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心欲静而疯不止 2025-02-08 01:25:53

使用itertools.combinations获取不同长度的sublists并检查总和:

import itertools

def check_sum_100(L):
    output = list()
    for l in range(len(L)+1):
        for sublist in itertools.combinations(L, l):
            if sum(sublist)==100:
                output.append(sublist)         
    return output

>>> check_sum([1,50,16,34,8,42,99])
[(1, 99), (50, 16, 34), (50, 8, 42), (16, 34, 8, 42)]

Use itertools.combinations to get sublists of varying length and check for the sum:

import itertools

def check_sum_100(L):
    output = list()
    for l in range(len(L)+1):
        for sublist in itertools.combinations(L, l):
            if sum(sublist)==100:
                output.append(sublist)         
    return output

>>> check_sum([1,50,16,34,8,42,99])
[(1, 99), (50, 16, 34), (50, 8, 42), (16, 34, 8, 42)]
杀手六號 2025-02-08 01:25:53

您可以将递归用于此类问题:

def check_sum_100(L):
    # inner function looks for a specific sum target and generates combinations
    def check_sum(L,target):
        for i,v in enumerate(L):
            if v == target:       # base condition, a number in L is the target
                yield [v]
            else:
                # check the sublist after this number for sums that equal the remainder
                for o in check_sum(L[i + 1:], target - v):
                    yield [v] + o
    return list(check_sum(L, 100))

L = [1,50,16,34,8,42,99]
print(check_sum_100(L))

输出:

[[1, 99], [50, 16, 34], [50, 8, 42], [16, 34, 8, 42]]

You can use recursion for this sort of problem:

def check_sum_100(L):
    # inner function looks for a specific sum target and generates combinations
    def check_sum(L,target):
        for i,v in enumerate(L):
            if v == target:       # base condition, a number in L is the target
                yield [v]
            else:
                # check the sublist after this number for sums that equal the remainder
                for o in check_sum(L[i + 1:], target - v):
                    yield [v] + o
    return list(check_sum(L, 100))

L = [1,50,16,34,8,42,99]
print(check_sum_100(L))

Output:

[[1, 99], [50, 16, 34], [50, 8, 42], [16, 34, 8, 42]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文