查找加起来达到特定目标值的所有唯一值



有没有一种更 Python 的方式来查找所有数字组合(每个组合都由唯一数字组成(,这些组合加起来就是一个特定的目标数字。例如:

largest_single_number = 9  # in this example: single digits only
num_of_inputs = 5
target_sum = 30
# expected output:
0 + 6 + 7 + 8 + 9 = 30
1 + 5 + 7 + 8 + 9 = 30
2 + 4 + 7 + 8 + 9 = 30
2 + 5 + 6 + 8 + 9 = 30
3 + 4 + 6 + 8 + 9 = 30
3 + 5 + 6 + 7 + 9 = 30
4 + 5 + 6 + 7 + 8 = 30
number of possibilities:  7

我们拥有的代码如下所示:

counter = 0
largest_single_number = 9  # in this example: single digits only
num_of_inputs = 5  # Not used but dictates number of nested loops
target_sum = 30
for digit_1 in range(largest_single_number + 1):
for digit_2 in range(digit_1 + 1, largest_single_number + 1):
for digit_3 in range(digit_2 + 1, largest_single_number + 1):
for digit_4 in range(digit_3 + 1, largest_single_number + 1):
for digit_5 in range(digit_4 + 1, largest_single_number + 1):
if (
digi_sum := (digit_1 + digit_2 + digit_3 + digit_4 + digit_5)
) == target_sum:
print(
f"{digit_1} + {digit_2} + {digit_3} + {digit_4} + {digit_5} = {target_sum}"
)
counter += 1
elif digi_sum > target_sum:
break
print("number of possibilities: ", counter)

我们将不胜感激,知道什么是实现相同结果的更 Python 方法。

使用itertools.combinations

  • 检查列表推导中的相等性
from itertools import combinations
values = [x for x in combinations(range(10), 5) if sum(x) == 30]
print(len(values))
>>> 7
print(values)
[(0, 6, 7, 8, 9),
(1, 5, 7, 8, 9),
(2, 4, 7, 8, 9),
(2, 5, 6, 8, 9),
(3, 4, 6, 8, 9),
(3, 5, 6, 7, 9),
(4, 5, 6, 7, 8)]

获取预期输出

for x in values:
expected = ' + '.join(map(str, x)) + ' = 30'
print(expected)
0 + 6 + 7 + 8 + 9 = 30
1 + 5 + 7 + 8 + 9 = 30
2 + 4 + 7 + 8 + 9 = 30
2 + 5 + 6 + 8 + 9 = 30
3 + 4 + 6 + 8 + 9 = 30
3 + 5 + 6 + 7 + 9 = 30
4 + 5 + 6 + 7 + 8 = 30

作为函数

def calculation(largest: int, number: int, target: int) -> list:
values = [x for x in combinations(range(largest + 1), number) if sum(x) == target]
print(f'number of possibilites: {len(values)}')
for x in values:
print(' + '.join(map(str, x)) + f' = {target}')

calculation(9, 5, 30)
number of possibilites: 7
0 + 6 + 7 + 8 + 9 = 30
1 + 5 + 7 + 8 + 9 = 30
2 + 4 + 7 + 8 + 9 = 30
2 + 5 + 6 + 8 + 9 = 30
3 + 4 + 6 + 8 + 9 = 30
3 + 5 + 6 + 7 + 9 = 30
4 + 5 + 6 + 7 + 8 = 30

这是一个 1 行(不包括导入(:

from itertools import combinations
print("number of possibilities ",len([s for s in combinations(range(largest_single_number+1), num_of_inputs) if sum(s)== target_sum]))

我将把打印各个集作为练习。

我可能会使用itertools.combinations

possibilities = [combo for combo in 
itertools.combinations(range(largest_single_number+1), num_of_inputs))
if sum(combo) == target_sum]

最新更新