sum-helpers 0.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.
- sum_helpers-0.1.0/PKG-INFO +117 -0
- sum_helpers-0.1.0/README.md +97 -0
- sum_helpers-0.1.0/pyproject.toml +41 -0
- sum_helpers-0.1.0/pyproject.toml.orig +40 -0
- sum_helpers-0.1.0/src/sum_helpers/__init__.py +85 -0
- sum_helpers-0.1.0/src/sum_helpers/k_sum.py +416 -0
- sum_helpers-0.1.0/src/sum_helpers/py.typed +0 -0
- sum_helpers-0.1.0/src/sum_helpers/three_sum.py +208 -0
- sum_helpers-0.1.0/src/sum_helpers/triangle.py +163 -0
- sum_helpers-0.1.0/src/sum_helpers/two_sum.py +268 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sum-helpers
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Modular, typed Python utilities for two-sum, three-sum, triangle-sum, and generalized k-sum algorithms.
|
|
5
|
+
Keywords: algorithms,two-sum,three-sum,k-sum,two-pointer,combinatorics
|
|
6
|
+
Author: musleh-khan
|
|
7
|
+
Author-email: musleh-khan <MuslehUddin.khan@exos.systems>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# sum-helpers
|
|
22
|
+
|
|
23
|
+
A modular, strictly-typed Python package providing pure algorithmic solutions for array sum and combination problems.
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install sum-helpers
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Features
|
|
32
|
+
|
|
33
|
+
- **Pure Functions**: Input collections are never mutated in-place.
|
|
34
|
+
- **Strictly Typed**: Full type annotations (`Sequence[int]`, `tuple[int, ...]`, `int | None`) supporting PEP 561 (`py.typed`).
|
|
35
|
+
- **Comprehensive Algorithms**: Includes two-pointer scans, hash-based lookups, bounding-based pruning, and recursive k-sum decomposition.
|
|
36
|
+
|
|
37
|
+
## Overview of Exported Functions
|
|
38
|
+
|
|
39
|
+
### 1. Two-Element Sums (`sum_helpers.two_sum`)
|
|
40
|
+
- `two_sum(nums, target)`: 0-indexed indices via single-pass hash map ($\mathcal{O}(n)$).
|
|
41
|
+
- `two_sum_sorted(numbers, target)`: 1-indexed two-pointer solution on sorted array ($\mathcal{O}(n)$ time, $\mathcal{O}(1)$ space).
|
|
42
|
+
- `two_sum_less_than_k(nums, k)`: Maximum sum strictly less than $k$ ($\mathcal{O}(n \log n)$).
|
|
43
|
+
- `two_sum_closest(nums, target)`: Pair sum closest to target ($\mathcal{O}(n \log n)$).
|
|
44
|
+
- `two_sum_unique_pairs(nums, target)`: All unique sorted pairs summing to target ($\mathcal{O}(n \log n)$).
|
|
45
|
+
|
|
46
|
+
### 2. Three-Element Sums (`sum_helpers.three_sum`)
|
|
47
|
+
- `three_sum(nums, target=0)`: All unique sorted triplets summing to target ($\mathcal{O}(n^2)$).
|
|
48
|
+
- `three_sum_closest(nums, target)`: Triplet sum closest to target ($\mathcal{O}(n^2)$).
|
|
49
|
+
- `three_sum_smaller(nums, target)`: Count index triplets with sum strictly less than target ($\mathcal{O}(n^2)$).
|
|
50
|
+
|
|
51
|
+
### 3. Triangle Sums (`sum_helpers.triangle`)
|
|
52
|
+
- `valid_triangle_number(nums)`: Count index triplets that can form a valid triangle ($\mathcal{O}(n^2)$).
|
|
53
|
+
- `is_valid_triangle(a, b, c)`: Geometric triangle inequality validator ($\mathcal{O}(1)$).
|
|
54
|
+
- `find_valid_triangles(nums)`: All unique sorted value triplets forming valid triangles ($\mathcal{O}(n^2)$).
|
|
55
|
+
|
|
56
|
+
### 4. Generalized K-Sum & Four Sum (`sum_helpers.k_sum`)
|
|
57
|
+
- `k_sum(nums, target, k)`: Generalized recursive two-pointer k-sum for arbitrary $k \ge 2$ ($\mathcal{O}(n^{k-1})$).
|
|
58
|
+
- `k_sum_closest(nums, target, k)`: Sum of $k$ integers closest to target ($\mathcal{O}(n^{k-1})$).
|
|
59
|
+
- `k_sum_smaller(nums, target, k)`: Count index $k$-tuples with sum strictly less than target ($\mathcal{O}(n^{k-1})$).
|
|
60
|
+
- `four_sum(nums, target=0)`: All unique 4-tuples summing to target ($\mathcal{O}(n^3)$).
|
|
61
|
+
- `four_sum_closest(nums, target)`: Sum of 4 integers closest to target ($\mathcal{O}(n^3)$).
|
|
62
|
+
- `four_sum_smaller(nums, target)`: Count index quadruplets with sum strictly less than target ($\mathcal{O}(n^3)$).
|
|
63
|
+
- `four_sum_ii(nums1, nums2, nums3, nums4)`: Count quadruplets across four separate arrays summing to 0 ($\mathcal{O}(n^2)$).
|
|
64
|
+
|
|
65
|
+
## Quickstart
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from sum_helpers import (
|
|
69
|
+
two_sum,
|
|
70
|
+
two_sum_sorted,
|
|
71
|
+
two_sum_less_than_k,
|
|
72
|
+
two_sum_closest,
|
|
73
|
+
three_sum,
|
|
74
|
+
three_sum_closest,
|
|
75
|
+
three_sum_smaller,
|
|
76
|
+
valid_triangle_number,
|
|
77
|
+
k_sum,
|
|
78
|
+
k_sum_closest,
|
|
79
|
+
k_sum_smaller,
|
|
80
|
+
four_sum,
|
|
81
|
+
four_sum_closest,
|
|
82
|
+
four_sum_smaller,
|
|
83
|
+
four_sum_ii,
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
# Generalized K-Sum (k=4)
|
|
87
|
+
quadruplets = four_sum([1, 0, -1, 0, -2, 2], target=0)
|
|
88
|
+
assert quadruplets == [
|
|
89
|
+
(-2, -1, 1, 2),
|
|
90
|
+
(-2, 0, 0, 2),
|
|
91
|
+
(-1, 0, 0, 1),
|
|
92
|
+
]
|
|
93
|
+
|
|
94
|
+
# Generalized K-Sum Closest (k=4)
|
|
95
|
+
assert four_sum_closest([1, 0, -1, 0, -2, 2], target=0) == 0
|
|
96
|
+
|
|
97
|
+
# Generalized K-Sum Smaller (k=4)
|
|
98
|
+
assert four_sum_smaller([1, 1, 1, 1], target=5) == 1
|
|
99
|
+
|
|
100
|
+
# Four Sum II across four independent lists
|
|
101
|
+
assert four_sum_ii([1, 2], [-2, -1], [-1, 2], [0, 2]) == 2
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Running Tests
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
uv run pytest -v
|
|
108
|
+
uv run pytest --doctest-modules src/
|
|
109
|
+
uv run --with mypy mypy src/ tests/
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Building for PyPI
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
uv build
|
|
116
|
+
uv publish
|
|
117
|
+
```
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# sum-helpers
|
|
2
|
+
|
|
3
|
+
A modular, strictly-typed Python package providing pure algorithmic solutions for array sum and combination problems.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install sum-helpers
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Pure Functions**: Input collections are never mutated in-place.
|
|
14
|
+
- **Strictly Typed**: Full type annotations (`Sequence[int]`, `tuple[int, ...]`, `int | None`) supporting PEP 561 (`py.typed`).
|
|
15
|
+
- **Comprehensive Algorithms**: Includes two-pointer scans, hash-based lookups, bounding-based pruning, and recursive k-sum decomposition.
|
|
16
|
+
|
|
17
|
+
## Overview of Exported Functions
|
|
18
|
+
|
|
19
|
+
### 1. Two-Element Sums (`sum_helpers.two_sum`)
|
|
20
|
+
- `two_sum(nums, target)`: 0-indexed indices via single-pass hash map ($\mathcal{O}(n)$).
|
|
21
|
+
- `two_sum_sorted(numbers, target)`: 1-indexed two-pointer solution on sorted array ($\mathcal{O}(n)$ time, $\mathcal{O}(1)$ space).
|
|
22
|
+
- `two_sum_less_than_k(nums, k)`: Maximum sum strictly less than $k$ ($\mathcal{O}(n \log n)$).
|
|
23
|
+
- `two_sum_closest(nums, target)`: Pair sum closest to target ($\mathcal{O}(n \log n)$).
|
|
24
|
+
- `two_sum_unique_pairs(nums, target)`: All unique sorted pairs summing to target ($\mathcal{O}(n \log n)$).
|
|
25
|
+
|
|
26
|
+
### 2. Three-Element Sums (`sum_helpers.three_sum`)
|
|
27
|
+
- `three_sum(nums, target=0)`: All unique sorted triplets summing to target ($\mathcal{O}(n^2)$).
|
|
28
|
+
- `three_sum_closest(nums, target)`: Triplet sum closest to target ($\mathcal{O}(n^2)$).
|
|
29
|
+
- `three_sum_smaller(nums, target)`: Count index triplets with sum strictly less than target ($\mathcal{O}(n^2)$).
|
|
30
|
+
|
|
31
|
+
### 3. Triangle Sums (`sum_helpers.triangle`)
|
|
32
|
+
- `valid_triangle_number(nums)`: Count index triplets that can form a valid triangle ($\mathcal{O}(n^2)$).
|
|
33
|
+
- `is_valid_triangle(a, b, c)`: Geometric triangle inequality validator ($\mathcal{O}(1)$).
|
|
34
|
+
- `find_valid_triangles(nums)`: All unique sorted value triplets forming valid triangles ($\mathcal{O}(n^2)$).
|
|
35
|
+
|
|
36
|
+
### 4. Generalized K-Sum & Four Sum (`sum_helpers.k_sum`)
|
|
37
|
+
- `k_sum(nums, target, k)`: Generalized recursive two-pointer k-sum for arbitrary $k \ge 2$ ($\mathcal{O}(n^{k-1})$).
|
|
38
|
+
- `k_sum_closest(nums, target, k)`: Sum of $k$ integers closest to target ($\mathcal{O}(n^{k-1})$).
|
|
39
|
+
- `k_sum_smaller(nums, target, k)`: Count index $k$-tuples with sum strictly less than target ($\mathcal{O}(n^{k-1})$).
|
|
40
|
+
- `four_sum(nums, target=0)`: All unique 4-tuples summing to target ($\mathcal{O}(n^3)$).
|
|
41
|
+
- `four_sum_closest(nums, target)`: Sum of 4 integers closest to target ($\mathcal{O}(n^3)$).
|
|
42
|
+
- `four_sum_smaller(nums, target)`: Count index quadruplets with sum strictly less than target ($\mathcal{O}(n^3)$).
|
|
43
|
+
- `four_sum_ii(nums1, nums2, nums3, nums4)`: Count quadruplets across four separate arrays summing to 0 ($\mathcal{O}(n^2)$).
|
|
44
|
+
|
|
45
|
+
## Quickstart
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from sum_helpers import (
|
|
49
|
+
two_sum,
|
|
50
|
+
two_sum_sorted,
|
|
51
|
+
two_sum_less_than_k,
|
|
52
|
+
two_sum_closest,
|
|
53
|
+
three_sum,
|
|
54
|
+
three_sum_closest,
|
|
55
|
+
three_sum_smaller,
|
|
56
|
+
valid_triangle_number,
|
|
57
|
+
k_sum,
|
|
58
|
+
k_sum_closest,
|
|
59
|
+
k_sum_smaller,
|
|
60
|
+
four_sum,
|
|
61
|
+
four_sum_closest,
|
|
62
|
+
four_sum_smaller,
|
|
63
|
+
four_sum_ii,
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
# Generalized K-Sum (k=4)
|
|
67
|
+
quadruplets = four_sum([1, 0, -1, 0, -2, 2], target=0)
|
|
68
|
+
assert quadruplets == [
|
|
69
|
+
(-2, -1, 1, 2),
|
|
70
|
+
(-2, 0, 0, 2),
|
|
71
|
+
(-1, 0, 0, 1),
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
# Generalized K-Sum Closest (k=4)
|
|
75
|
+
assert four_sum_closest([1, 0, -1, 0, -2, 2], target=0) == 0
|
|
76
|
+
|
|
77
|
+
# Generalized K-Sum Smaller (k=4)
|
|
78
|
+
assert four_sum_smaller([1, 1, 1, 1], target=5) == 1
|
|
79
|
+
|
|
80
|
+
# Four Sum II across four independent lists
|
|
81
|
+
assert four_sum_ii([1, 2], [-2, -1], [-1, 2], [0, 2]) == 2
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Running Tests
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
uv run pytest -v
|
|
88
|
+
uv run pytest --doctest-modules src/
|
|
89
|
+
uv run --with mypy mypy src/ tests/
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Building for PyPI
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
uv build
|
|
96
|
+
uv publish
|
|
97
|
+
```
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "sum-helpers"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Modular, typed Python utilities for two-sum, three-sum, triangle-sum, and generalized k-sum algorithms."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
requires-python = ">=3.10"
|
|
8
|
+
keywords = [
|
|
9
|
+
"algorithms",
|
|
10
|
+
"two-sum",
|
|
11
|
+
"three-sum",
|
|
12
|
+
"k-sum",
|
|
13
|
+
"two-pointer",
|
|
14
|
+
"combinatorics",
|
|
15
|
+
]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
25
|
+
"Typing :: Typed",
|
|
26
|
+
]
|
|
27
|
+
dependencies = []
|
|
28
|
+
|
|
29
|
+
[[project.authors]]
|
|
30
|
+
name = "musleh-khan"
|
|
31
|
+
email = "MuslehUddin.khan@exos.systems"
|
|
32
|
+
|
|
33
|
+
[build-system]
|
|
34
|
+
requires = ["uv_build>=0.13.8,<0.14.0"]
|
|
35
|
+
build-backend = "uv_build"
|
|
36
|
+
|
|
37
|
+
[dependency-groups]
|
|
38
|
+
dev = [
|
|
39
|
+
"pytest>=9.2.0",
|
|
40
|
+
"mypy>=1.15.0",
|
|
41
|
+
]
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "sum-helpers"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Modular, typed Python utilities for two-sum, three-sum, triangle-sum, and generalized k-sum algorithms."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = [
|
|
8
|
+
{ name = "musleh-khan", email = "MuslehUddin.khan@exos.systems" }
|
|
9
|
+
]
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
keywords = [
|
|
12
|
+
"algorithms",
|
|
13
|
+
"two-sum",
|
|
14
|
+
"three-sum",
|
|
15
|
+
"k-sum",
|
|
16
|
+
"two-pointer",
|
|
17
|
+
"combinatorics",
|
|
18
|
+
]
|
|
19
|
+
classifiers = [
|
|
20
|
+
"Development Status :: 4 - Beta",
|
|
21
|
+
"Intended Audience :: Developers",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3.10",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Programming Language :: Python :: 3.13",
|
|
27
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
28
|
+
"Typing :: Typed",
|
|
29
|
+
]
|
|
30
|
+
dependencies = []
|
|
31
|
+
|
|
32
|
+
[build-system]
|
|
33
|
+
requires = ["uv_build>=0.13.8,<0.14.0"]
|
|
34
|
+
build-backend = "uv_build"
|
|
35
|
+
|
|
36
|
+
[dependency-groups]
|
|
37
|
+
dev = [
|
|
38
|
+
"pytest>=9.2.0",
|
|
39
|
+
"mypy>=1.15.0",
|
|
40
|
+
]
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""sum_helpers: Clean, modular, well-typed Python helpers for sum and combination problems.
|
|
2
|
+
|
|
3
|
+
Core modules:
|
|
4
|
+
- two_sum:
|
|
5
|
+
- two_sum (LC 1)
|
|
6
|
+
- two_sum_sorted (LC 167)
|
|
7
|
+
- two_sum_less_than_k (LC 1099)
|
|
8
|
+
- two_sum_closest
|
|
9
|
+
- two_sum_unique_pairs
|
|
10
|
+
|
|
11
|
+
- three_sum:
|
|
12
|
+
- three_sum (LC 15)
|
|
13
|
+
- three_sum_closest (LC 16)
|
|
14
|
+
- three_sum_smaller (LC 259)
|
|
15
|
+
|
|
16
|
+
- triangle:
|
|
17
|
+
- valid_triangle_number (LC 611)
|
|
18
|
+
- is_valid_triangle
|
|
19
|
+
- find_valid_triangles
|
|
20
|
+
|
|
21
|
+
- k_sum:
|
|
22
|
+
- k_sum (Generalized arbitrary k >= 2)
|
|
23
|
+
- k_sum_closest
|
|
24
|
+
- k_sum_smaller
|
|
25
|
+
- four_sum (LC 18)
|
|
26
|
+
- four_sum_closest
|
|
27
|
+
- four_sum_smaller
|
|
28
|
+
- four_sum_ii (LC 454)
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
from __future__ import annotations
|
|
32
|
+
|
|
33
|
+
from sum_helpers.k_sum import (
|
|
34
|
+
four_sum,
|
|
35
|
+
four_sum_closest,
|
|
36
|
+
four_sum_ii,
|
|
37
|
+
four_sum_smaller,
|
|
38
|
+
k_sum,
|
|
39
|
+
k_sum_closest,
|
|
40
|
+
k_sum_smaller,
|
|
41
|
+
)
|
|
42
|
+
from sum_helpers.three_sum import (
|
|
43
|
+
three_sum,
|
|
44
|
+
three_sum_closest,
|
|
45
|
+
three_sum_smaller,
|
|
46
|
+
)
|
|
47
|
+
from sum_helpers.triangle import (
|
|
48
|
+
find_valid_triangles,
|
|
49
|
+
is_valid_triangle,
|
|
50
|
+
valid_triangle_number,
|
|
51
|
+
)
|
|
52
|
+
from sum_helpers.two_sum import (
|
|
53
|
+
two_sum,
|
|
54
|
+
two_sum_closest,
|
|
55
|
+
two_sum_less_than_k,
|
|
56
|
+
two_sum_sorted,
|
|
57
|
+
two_sum_unique_pairs,
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
__all__ = [
|
|
61
|
+
# Two sum
|
|
62
|
+
"two_sum",
|
|
63
|
+
"two_sum_sorted",
|
|
64
|
+
"two_sum_less_than_k",
|
|
65
|
+
"two_sum_closest",
|
|
66
|
+
"two_sum_unique_pairs",
|
|
67
|
+
# Three sum
|
|
68
|
+
"three_sum",
|
|
69
|
+
"three_sum_closest",
|
|
70
|
+
"three_sum_smaller",
|
|
71
|
+
# Triangle sum
|
|
72
|
+
"valid_triangle_number",
|
|
73
|
+
"is_valid_triangle",
|
|
74
|
+
"find_valid_triangles",
|
|
75
|
+
# K-sum & Four sum
|
|
76
|
+
"k_sum",
|
|
77
|
+
"k_sum_closest",
|
|
78
|
+
"k_sum_smaller",
|
|
79
|
+
"four_sum",
|
|
80
|
+
"four_sum_closest",
|
|
81
|
+
"four_sum_smaller",
|
|
82
|
+
"four_sum_ii",
|
|
83
|
+
]
|
|
84
|
+
|
|
85
|
+
__version__ = "0.1.0"
|