forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouredev.py
30 lines (22 loc) · 813 Bytes
/
mouredev.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def find_sums(numbers: list, target: int) -> list:
def find_sum(start: int, target: int, combination: list):
# Solución encontrada
if target == 0:
result.append(combination[:])
return
# No posee solución
if target < 0 or start == len(numbers):
return
# Búsqueda
for index in range(start, len(numbers)):
if index > start and numbers[index] == numbers[index - 1]:
continue
combination.append(numbers[index])
find_sum(index + 1, target - numbers[index], combination)
combination.pop()
numbers.sort()
result = []
find_sum(0, target, [])
return result
print(find_sums([1, 5, 3, 2], 6))
print(find_sums([1, 2, 1, 1, 1, 1, 2, 1], 6))