检查一个数字是否是另外两个数字的和

时间:2019-01-01 11:15:30

标签: python

问题陈述

给出一个数字列表和一个数字 k ,返回列表中是否有两个数字加起来为 k

示例

给出[1, 2, 3]k = 5return True,从2 + 3 = 5开始。

这是我尝试做的事情:

def pairs(n):
    for i in range(len(n)):
        for j in range(i+1, len()):
            yield n[i], n[j]


def ListCheck():
    number = input("Give me a number:")
    val = int(number)
    nums = [1,2,3]
    for i, j in pairs(nums):
        if j + i == val:
            print(True)
            break


ListCheck()

运行时出现错误,我不明白为什么。

5 个答案:

答案 0 :(得分:5)

您还可以做itertools.combinations,比@bitto的解决方案短一点:

import itertools
def f(lst,num):
    for x,y in itertools.combinations(lst,2):
        if x+y==num:
            return True
    return False
lst=[1,2,3]
num=int(input("Give me a number: "))
print(f(lst,num))

答案 1 :(得分:2)

> program @myfile

输出

# myfile
a
b
c

答案 2 :(得分:1)

您错过了n内的len()。错误

TypeError: len() takes exactly one argument (0 given)

完全正确地告诉您出了什么问题(如果您解决了上面代码发布的缩进问题)。


您可以使用itertools.combinations简化代码。如果在函数中添加一些参数,则也可以概括问题的搜索范围-从列表中获得 n 个数字的 all 个组合,这些数字加起来就等于目标值。< / p>

from itertools import combinations 

def is_sum_of_n_numbers(data ,target_value, num_elem):
    """Returns 'True' if any combinatin of 'num_elem'ents 
    from 'data' sums to 'target_value'"""
    return any(sum(x)==target_value for x in combinations(data, num_elem))

def find_sum_in_combination(data, target_value, num_elem):
    """Returns all combinations of 'num_elem'ent-tuples from 'data' 
    that sums to 'target_value'"""
    return [x for x in combinations(data,num_elem) if sum(x) == target_value]

全部获取:

d = [1,2,3,4,5]
for numbers in range(1,6):
    for s in range(1,sum(d)+1):
        result = find_sum_in_combination(d,s,numbers)
        if result:
            print(f"Sum {s} from {d} with {numbers} numbers: ", result)

输出:

Sum 1 from [1, 2, 3, 4, 5] with 1 numbers:  [(1,)]
Sum 2 from [1, 2, 3, 4, 5] with 1 numbers:  [(2,)]
Sum 3 from [1, 2, 3, 4, 5] with 1 numbers:  [(3,)]
Sum 4 from [1, 2, 3, 4, 5] with 1 numbers:  [(4,)]
Sum 5 from [1, 2, 3, 4, 5] with 1 numbers:  [(5,)]
Sum 3 from [1, 2, 3, 4, 5] with 2 numbers:  [(1, 2)]
Sum 4 from [1, 2, 3, 4, 5] with 2 numbers:  [(1, 3)]
Sum 5 from [1, 2, 3, 4, 5] with 2 numbers:  [(1, 4), (2, 3)]
Sum 6 from [1, 2, 3, 4, 5] with 2 numbers:  [(1, 5), (2, 4)]
Sum 7 from [1, 2, 3, 4, 5] with 2 numbers:  [(2, 5), (3, 4)]
Sum 8 from [1, 2, 3, 4, 5] with 2 numbers:  [(3, 5)]
Sum 9 from [1, 2, 3, 4, 5] with 2 numbers:  [(4, 5)]
Sum 6 from [1, 2, 3, 4, 5] with 3 numbers:  [(1, 2, 3)]
Sum 7 from [1, 2, 3, 4, 5] with 3 numbers:  [(1, 2, 4)]
Sum 8 from [1, 2, 3, 4, 5] with 3 numbers:  [(1, 2, 5), (1, 3, 4)]
Sum 9 from [1, 2, 3, 4, 5] with 3 numbers:  [(1, 3, 5), (2, 3, 4)]
Sum 10 from [1, 2, 3, 4, 5] with 3 numbers:  [(1, 4, 5), (2, 3, 5)]
Sum 11 from [1, 2, 3, 4, 5] with 3 numbers:  [(2, 4, 5)]
Sum 12 from [1, 2, 3, 4, 5] with 3 numbers:  [(3, 4, 5)]
Sum 10 from [1, 2, 3, 4, 5] with 4 numbers:  [(1, 2, 3, 4)]
Sum 11 from [1, 2, 3, 4, 5] with 4 numbers:  [(1, 2, 3, 5)]
Sum 12 from [1, 2, 3, 4, 5] with 4 numbers:  [(1, 2, 4, 5)]
Sum 13 from [1, 2, 3, 4, 5] with 4 numbers:  [(1, 3, 4, 5)]
Sum 14 from [1, 2, 3, 4, 5] with 4 numbers:  [(2, 3, 4, 5)]
Sum 15 from [1, 2, 3, 4, 5] with 5 numbers:  [(1, 2, 3, 4, 5)]

Doku:

答案 3 :(得分:0)

您的代码的逻辑是正确的(通过与上述答案进行比较,您可以看到它不必要地复杂,但仍然是正确的;)....)
只需检查您的indentations并将for j in range(i+1, len(n))放在代码的第三行即可...您忘记了'n'!您需要至少给len一个参数。

答案 4 :(得分:0)

您可以通过这种方式解决您的问题

n=[3,2,1]
number=int(input("Please enter nubmer"))
for i in n:
    num=number - i
    if num in n:
        print(num,i)
        break

这是解决方案,但需要根据需要进行自定义