combinations 1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- combinations-1.0/PKG-INFO +7 -0
- combinations-1.0/combinations/__init__.py +1 -0
- combinations-1.0/combinations/main.py +41 -0
- combinations-1.0/combinations.egg-info/PKG-INFO +7 -0
- combinations-1.0/combinations.egg-info/SOURCES.txt +9 -0
- combinations-1.0/combinations.egg-info/dependency_links.txt +1 -0
- combinations-1.0/combinations.egg-info/not-zip-safe +1 -0
- combinations-1.0/combinations.egg-info/top_level.txt +1 -0
- combinations-1.0/setup.cfg +4 -0
- combinations-1.0/setup.py +8 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from main import find_combinations
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
def find_combinations(numbers, target):
|
|
2
|
+
results = []
|
|
3
|
+
# Сортируем для оптимизации и исключения дубликатов
|
|
4
|
+
numbers.sort()
|
|
5
|
+
|
|
6
|
+
def backtrack(start, current_sum, path):
|
|
7
|
+
# Если нашли нужную сумму, сохраняем копию пути
|
|
8
|
+
if current_sum == target:
|
|
9
|
+
results.append(list(path))
|
|
10
|
+
return
|
|
11
|
+
# Если перешагнули лимит, останавливаем ветку
|
|
12
|
+
if current_sum > target:
|
|
13
|
+
return
|
|
14
|
+
|
|
15
|
+
for i in range(start, len(numbers)):
|
|
16
|
+
# Пропускаем одинаковые числа на одном уровне рекурсии, чтобы избежать дубликатов в ответе
|
|
17
|
+
if i > start and numbers[i] == numbers[i - 1]:
|
|
18
|
+
continue
|
|
19
|
+
|
|
20
|
+
# Шаг вперед
|
|
21
|
+
path.append(numbers[i])
|
|
22
|
+
# Рекурсивный запуск со следующего элемента
|
|
23
|
+
backtrack(i + 1, current_sum + numbers[i], path)
|
|
24
|
+
# Откат назад (backtrack)
|
|
25
|
+
path.pop()
|
|
26
|
+
|
|
27
|
+
backtrack(0, 0, [])
|
|
28
|
+
return results
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# --- Пример использования ---
|
|
32
|
+
my_numbers = [10, 1, 2, 7, 6, 1, 5]
|
|
33
|
+
target_sum = 8
|
|
34
|
+
|
|
35
|
+
combinations = find_combinations(my_numbers, target_sum)
|
|
36
|
+
|
|
37
|
+
print(f"Исходный список: {my_numbers}")
|
|
38
|
+
print(f"Искомая сумма: {target_sum}")
|
|
39
|
+
print("Найденные комбинации:")
|
|
40
|
+
for combo in combinations:
|
|
41
|
+
print(combo)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
combinations
|