custom-combination 0.0.1__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.
- custom_combination-0.0.1/.github/workflows/publish-to-pypi.yaml +82 -0
- custom_combination-0.0.1/.github/workflows/test-app.yaml +32 -0
- custom_combination-0.0.1/.gitignore +11 -0
- custom_combination-0.0.1/.python-version +1 -0
- custom_combination-0.0.1/LICENCE +19 -0
- custom_combination-0.0.1/PKG-INFO +113 -0
- custom_combination-0.0.1/README.md +98 -0
- custom_combination-0.0.1/main.py +6 -0
- custom_combination-0.0.1/pyproject.toml +36 -0
- custom_combination-0.0.1/requirements.txt +2 -0
- custom_combination-0.0.1/ruff.toml +5 -0
- custom_combination-0.0.1/src/custom_combination/__init__.py +13 -0
- custom_combination-0.0.1/src/custom_combination/cli.py +58 -0
- custom_combination-0.0.1/src/custom_combination/core.py +76 -0
- custom_combination-0.0.1/tests/__init__.py +3 -0
- custom_combination-0.0.1/tests/test_app.py +50 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
name: Publish Python 🐍 distribution 📦 to PyPI and TestPyPI
|
|
2
|
+
|
|
3
|
+
on: push
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
build:
|
|
7
|
+
name: Build distribution 📦
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
|
|
10
|
+
steps:
|
|
11
|
+
- uses: actions/checkout@v6
|
|
12
|
+
with:
|
|
13
|
+
persist-credentials: false
|
|
14
|
+
- name: Set up Python
|
|
15
|
+
uses: actions/setup-python@v6
|
|
16
|
+
with:
|
|
17
|
+
python-version: "3.x"
|
|
18
|
+
|
|
19
|
+
- name: Install pypa/build
|
|
20
|
+
run: >-
|
|
21
|
+
python3 -m
|
|
22
|
+
pip install
|
|
23
|
+
build
|
|
24
|
+
--user
|
|
25
|
+
- name: Build a binary wheel and a source tarball
|
|
26
|
+
run: python3 -m build
|
|
27
|
+
- name: Store the distribution packages
|
|
28
|
+
uses: actions/upload-artifact@v5
|
|
29
|
+
with:
|
|
30
|
+
name: python-package-distributions
|
|
31
|
+
path: dist/
|
|
32
|
+
|
|
33
|
+
publish-to-pypi:
|
|
34
|
+
name: >-
|
|
35
|
+
Publish Python 🐍 distribution 📦 to PyPI
|
|
36
|
+
if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes
|
|
37
|
+
needs:
|
|
38
|
+
- build
|
|
39
|
+
runs-on: ubuntu-latest
|
|
40
|
+
environment:
|
|
41
|
+
name: pypi
|
|
42
|
+
url: https://pypi.org/p/custom_combination
|
|
43
|
+
permissions:
|
|
44
|
+
id-token: write # IMPORTANT: mandatory for trusted publishing
|
|
45
|
+
|
|
46
|
+
steps:
|
|
47
|
+
- name: Download all the dists
|
|
48
|
+
uses: actions/download-artifact@v6
|
|
49
|
+
with:
|
|
50
|
+
name: python-package-distributions
|
|
51
|
+
path: dist/
|
|
52
|
+
- name: Publish distribution 📦 to PyPI
|
|
53
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
54
|
+
with:
|
|
55
|
+
verbose: true
|
|
56
|
+
skip-existing: true
|
|
57
|
+
|
|
58
|
+
publish-to-testpypi:
|
|
59
|
+
name: Publish Python 🐍 distribution 📦 to TestPyPI
|
|
60
|
+
needs:
|
|
61
|
+
- build
|
|
62
|
+
runs-on: ubuntu-latest
|
|
63
|
+
|
|
64
|
+
environment:
|
|
65
|
+
name: testpypi
|
|
66
|
+
url: https://test.pypi.org/p/custom_combination
|
|
67
|
+
|
|
68
|
+
permissions:
|
|
69
|
+
id-token: write # IMPORTANT: mandatory for trusted publishing
|
|
70
|
+
|
|
71
|
+
steps:
|
|
72
|
+
- name: Download all the dists
|
|
73
|
+
uses: actions/download-artifact@v6
|
|
74
|
+
with:
|
|
75
|
+
name: python-package-distributions
|
|
76
|
+
path: dist/
|
|
77
|
+
- name: Publish distribution 📦 to TestPyPI
|
|
78
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
79
|
+
with:
|
|
80
|
+
repository-url: https://test.pypi.org/legacy/
|
|
81
|
+
verbose: true
|
|
82
|
+
skip-existing: true
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
name: Test Python package
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_dispatch:
|
|
5
|
+
push:
|
|
6
|
+
branches:
|
|
7
|
+
- development
|
|
8
|
+
- main
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
test:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v5
|
|
16
|
+
- name: Set up Python
|
|
17
|
+
uses: actions/setup-python@v5
|
|
18
|
+
with:
|
|
19
|
+
python-version: "3.x"
|
|
20
|
+
cache: "pip"
|
|
21
|
+
|
|
22
|
+
- name: Install check deps
|
|
23
|
+
run: pip install ruff
|
|
24
|
+
|
|
25
|
+
- name: Lint check
|
|
26
|
+
run: ruff check
|
|
27
|
+
|
|
28
|
+
- name: Install test deps
|
|
29
|
+
run: pip install -r requirements.txt
|
|
30
|
+
|
|
31
|
+
- name: Test with pytest
|
|
32
|
+
run: pytest
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.12
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2018 The Python Packaging Authority
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: custom-combination
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A custom combination generator
|
|
5
|
+
Project-URL: Homepage, https://github.com/yilmazhasan/custom_combination
|
|
6
|
+
Project-URL: Issues, https://github.com/yilmazhasan/custom_combination/issues
|
|
7
|
+
Author-email: Hasan YILMAZ <ylmzhsn@proton.me>, Reego Software <reego.software@gmail.com>
|
|
8
|
+
License-File: LICENCE
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Requires-Python: >=3.8
|
|
13
|
+
Requires-Dist: typer
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# Custom Combination
|
|
17
|
+
|
|
18
|
+
> Version 0.0.1
|
|
19
|
+
|
|
20
|
+
Python implementation for Custom Combination.
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Programmatic usage
|
|
25
|
+
|
|
26
|
+
```py
|
|
27
|
+
from custom_combination import CustomCombination
|
|
28
|
+
|
|
29
|
+
CustomCombination.generate(
|
|
30
|
+
items=[("a", 1), ("b", 2), ("c", 3), "d"],
|
|
31
|
+
pick=3,
|
|
32
|
+
min_picks=[("b", 1), ("a", 0)],
|
|
33
|
+
max_picks=[("a", 0)])
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### CLI usage
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
custom_combination --items "a,b,b,c,c,c,d" -pick 3 -minp "b,1" -maxp "a,0"
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Output:
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
['b', 'c', 'd']
|
|
46
|
+
['b', 'c', 'c']
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Set up locally for development
|
|
50
|
+
|
|
51
|
+
1. Clone the repository
|
|
52
|
+
|
|
53
|
+
> `git clone git@github.com:yilmazhasan/custom_combination.git`
|
|
54
|
+
|
|
55
|
+
2. Create and activate the virtural environment
|
|
56
|
+
|
|
57
|
+
> `python -m venv .venv`
|
|
58
|
+
|
|
59
|
+
> `source .venv/bin/activate`
|
|
60
|
+
|
|
61
|
+
3. Install requirements:
|
|
62
|
+
|
|
63
|
+
> `pip install -r requirements.txt`
|
|
64
|
+
|
|
65
|
+
4. Run the app:
|
|
66
|
+
|
|
67
|
+
> `python src/custom_combination/app.py --items a,b,c,d -pick a,0 -minp b,1 -max a,0`
|
|
68
|
+
|
|
69
|
+
5. Test the app:
|
|
70
|
+
|
|
71
|
+
> `pytest`
|
|
72
|
+
|
|
73
|
+
## Args Explained
|
|
74
|
+
|
|
75
|
+
```sh
|
|
76
|
+
--items "a,b,b,c,c,c,d" -pick 3 -min-picks b,1 -max-picks a,0
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
or shortly:
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
--items "a,b,b,c,c,c,d" -p 3 -minp b,1 -maxp a,0
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
This indicates that:
|
|
86
|
+
|
|
87
|
+
- We have four items in the set `["a", "b", "c", "d"]` but with different frequencies.
|
|
88
|
+
- Thus we have the list as `["a", "b", "b", "c", "c", "c", "d"]`
|
|
89
|
+
|
|
90
|
+
- We want to generate combinations that have **3** items, which `-pick 3` specifies.
|
|
91
|
+
|
|
92
|
+
- We want to pick at least:
|
|
93
|
+
- **1** occurrences of `"b"`
|
|
94
|
+
|
|
95
|
+
- We want to pick at max:
|
|
96
|
+
- **0** occurrences of `"a"`
|
|
97
|
+
|
|
98
|
+
This indicates the list to generate from is:
|
|
99
|
+
|
|
100
|
+
`["b", "b", "c", "c", "c", "d"]` out of
|
|
101
|
+
|
|
102
|
+
`["a", "b", "b", "c", "c", "c", "d"]` as `"a"` is excluded.
|
|
103
|
+
|
|
104
|
+
Since `"b"` is to be included once, we need to select 2 more items from the list `["c", "c", "c", "d"]`, so we can select `["c", "c"]` or `["c", "d"]`
|
|
105
|
+
|
|
106
|
+
Expected output:
|
|
107
|
+
|
|
108
|
+
```sh
|
|
109
|
+
[
|
|
110
|
+
["b", "c", "c"],
|
|
111
|
+
["b", "c", "d"]
|
|
112
|
+
]
|
|
113
|
+
```
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# Custom Combination
|
|
2
|
+
|
|
3
|
+
> Version 0.0.1
|
|
4
|
+
|
|
5
|
+
Python implementation for Custom Combination.
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
### Programmatic usage
|
|
10
|
+
|
|
11
|
+
```py
|
|
12
|
+
from custom_combination import CustomCombination
|
|
13
|
+
|
|
14
|
+
CustomCombination.generate(
|
|
15
|
+
items=[("a", 1), ("b", 2), ("c", 3), "d"],
|
|
16
|
+
pick=3,
|
|
17
|
+
min_picks=[("b", 1), ("a", 0)],
|
|
18
|
+
max_picks=[("a", 0)])
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### CLI usage
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
custom_combination --items "a,b,b,c,c,c,d" -pick 3 -minp "b,1" -maxp "a,0"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Output:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
['b', 'c', 'd']
|
|
31
|
+
['b', 'c', 'c']
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Set up locally for development
|
|
35
|
+
|
|
36
|
+
1. Clone the repository
|
|
37
|
+
|
|
38
|
+
> `git clone git@github.com:yilmazhasan/custom_combination.git`
|
|
39
|
+
|
|
40
|
+
2. Create and activate the virtural environment
|
|
41
|
+
|
|
42
|
+
> `python -m venv .venv`
|
|
43
|
+
|
|
44
|
+
> `source .venv/bin/activate`
|
|
45
|
+
|
|
46
|
+
3. Install requirements:
|
|
47
|
+
|
|
48
|
+
> `pip install -r requirements.txt`
|
|
49
|
+
|
|
50
|
+
4. Run the app:
|
|
51
|
+
|
|
52
|
+
> `python src/custom_combination/app.py --items a,b,c,d -pick a,0 -minp b,1 -max a,0`
|
|
53
|
+
|
|
54
|
+
5. Test the app:
|
|
55
|
+
|
|
56
|
+
> `pytest`
|
|
57
|
+
|
|
58
|
+
## Args Explained
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
--items "a,b,b,c,c,c,d" -pick 3 -min-picks b,1 -max-picks a,0
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
or shortly:
|
|
65
|
+
|
|
66
|
+
```sh
|
|
67
|
+
--items "a,b,b,c,c,c,d" -p 3 -minp b,1 -maxp a,0
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
This indicates that:
|
|
71
|
+
|
|
72
|
+
- We have four items in the set `["a", "b", "c", "d"]` but with different frequencies.
|
|
73
|
+
- Thus we have the list as `["a", "b", "b", "c", "c", "c", "d"]`
|
|
74
|
+
|
|
75
|
+
- We want to generate combinations that have **3** items, which `-pick 3` specifies.
|
|
76
|
+
|
|
77
|
+
- We want to pick at least:
|
|
78
|
+
- **1** occurrences of `"b"`
|
|
79
|
+
|
|
80
|
+
- We want to pick at max:
|
|
81
|
+
- **0** occurrences of `"a"`
|
|
82
|
+
|
|
83
|
+
This indicates the list to generate from is:
|
|
84
|
+
|
|
85
|
+
`["b", "b", "c", "c", "c", "d"]` out of
|
|
86
|
+
|
|
87
|
+
`["a", "b", "b", "c", "c", "c", "d"]` as `"a"` is excluded.
|
|
88
|
+
|
|
89
|
+
Since `"b"` is to be included once, we need to select 2 more items from the list `["c", "c", "c", "d"]`, so we can select `["c", "c"]` or `["c", "d"]`
|
|
90
|
+
|
|
91
|
+
Expected output:
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
[
|
|
95
|
+
["b", "c", "c"],
|
|
96
|
+
["b", "c", "d"]
|
|
97
|
+
]
|
|
98
|
+
```
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "custom-combination"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
dependencies = ["typer"]
|
|
9
|
+
|
|
10
|
+
authors = [
|
|
11
|
+
{ name="Hasan YILMAZ", email="ylmzhsn@proton.me" },
|
|
12
|
+
{ name="Reego Software", email="reego.software@gmail.com" }
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
description = "A custom combination generator"
|
|
16
|
+
readme = "README.md"
|
|
17
|
+
requires-python = ">=3.8"
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Operating System :: OS Independent",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://github.com/yilmazhasan/custom_combination"
|
|
26
|
+
Issues = "https://github.com/yilmazhasan/custom_combination/issues"
|
|
27
|
+
|
|
28
|
+
[project.scripts]
|
|
29
|
+
custom_combination = "custom_combination.cli:app"
|
|
30
|
+
|
|
31
|
+
[tool.hatch.version]
|
|
32
|
+
path = "src/custom_combination/__init__.py"
|
|
33
|
+
|
|
34
|
+
[tool.pytest.ini_options]
|
|
35
|
+
pythonpath = ["src"]
|
|
36
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
__version__ = "0.0.1"
|
|
2
|
+
|
|
3
|
+
from . import cli # backward compat
|
|
4
|
+
from .core import generate as _generate
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class CustomCombination:
|
|
8
|
+
@staticmethod
|
|
9
|
+
def generate(items, pick=None, min_picks=None, max_picks=None):
|
|
10
|
+
return _generate(items, pick, min_picks, max_picks)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
__all__ = ["CustomCombination", "cli", "__version__"]
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
from typing import Annotated
|
|
3
|
+
|
|
4
|
+
import typer
|
|
5
|
+
|
|
6
|
+
from .core import generate as _core_generate
|
|
7
|
+
|
|
8
|
+
app = typer.Typer()
|
|
9
|
+
|
|
10
|
+
_SEPARATOR = ","
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@app.command()
|
|
14
|
+
def generate(
|
|
15
|
+
items: Annotated[
|
|
16
|
+
str, typer.Option("--items", help="Comma-separated list, e.g. --items a,b,c")
|
|
17
|
+
] = None,
|
|
18
|
+
min_picks: Annotated[
|
|
19
|
+
list[(str, int)],
|
|
20
|
+
typer.Option("--min-picks", "-minp", help="The items to be picked minimum e.g. -minp b,1"),
|
|
21
|
+
] = None,
|
|
22
|
+
max_picks: Annotated[
|
|
23
|
+
list[(str, int)],
|
|
24
|
+
typer.Option("--max-picks", "-maxp", help="The items to be picked maximum e.g. -maxp b,1"),
|
|
25
|
+
] = None,
|
|
26
|
+
pick: Annotated[
|
|
27
|
+
int,
|
|
28
|
+
typer.Option(
|
|
29
|
+
"--pick", "-p", "-n", help="The number of items to be picked, e.g. -p 3"
|
|
30
|
+
),
|
|
31
|
+
] = None,
|
|
32
|
+
out: Annotated[
|
|
33
|
+
str,
|
|
34
|
+
typer.Option(
|
|
35
|
+
"--out", "-o", help="Write results to a filepath, e.g. --out output.txt"
|
|
36
|
+
),
|
|
37
|
+
] = None,
|
|
38
|
+
):
|
|
39
|
+
|
|
40
|
+
item_list = items.split(_SEPARATOR)
|
|
41
|
+
|
|
42
|
+
min_pick_list = [(item[0], int(item[1])) for item in [pick.split(_SEPARATOR) for pick in min_picks]]
|
|
43
|
+
max_pick_list = [(item[0], int(item[1])) for item in [pick.split(_SEPARATOR) for pick in max_picks]]
|
|
44
|
+
|
|
45
|
+
results = _core_generate(items=item_list, pick=pick, min_picks=min_pick_list, max_picks=max_pick_list)
|
|
46
|
+
|
|
47
|
+
if out:
|
|
48
|
+
with open(out, "w") as f:
|
|
49
|
+
f.write("\n".join([_SEPARATOR.join(perm) for perm in results]))
|
|
50
|
+
else:
|
|
51
|
+
for perm in results:
|
|
52
|
+
print(perm)
|
|
53
|
+
|
|
54
|
+
return results
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
if __name__ == "__main__":
|
|
58
|
+
app()
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from collections import Counter
|
|
2
|
+
from itertools import combinations
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def getDiff(list1, list2):
|
|
6
|
+
return [item1 for item1 in list1 if item1 not in list2]
|
|
7
|
+
|
|
8
|
+
def does_exceed_max(combination_from_remaining, max_picked_list):
|
|
9
|
+
print(combination_from_remaining, max_picked_list)
|
|
10
|
+
combination_from_remaining_freq = Counter(combination_from_remaining)
|
|
11
|
+
max_pick_list_freq = Counter(max_picked_list)
|
|
12
|
+
|
|
13
|
+
for k, v in combination_from_remaining_freq.items():
|
|
14
|
+
if k in max_pick_list_freq and v > max_pick_list_freq[k]:
|
|
15
|
+
return True
|
|
16
|
+
|
|
17
|
+
return False
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def get_result(rest_item_list, min_picked_list, max_picked_list, rest_n):
|
|
21
|
+
|
|
22
|
+
result = []
|
|
23
|
+
visited = {}
|
|
24
|
+
|
|
25
|
+
for combination_from_remaining in combinations(rest_item_list, rest_n):
|
|
26
|
+
if str(combination_from_remaining) not in visited and not does_exceed_max(combination_from_remaining, max_picked_list):
|
|
27
|
+
visited[str(combination_from_remaining)] = True
|
|
28
|
+
new_combination = min_picked_list[:]
|
|
29
|
+
new_combination.extend(combination_from_remaining)
|
|
30
|
+
new_combination.sort()
|
|
31
|
+
result.append(new_combination)
|
|
32
|
+
|
|
33
|
+
result.sort()
|
|
34
|
+
|
|
35
|
+
return result
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def extract_ignored_items(items):
|
|
39
|
+
ignoreList = []
|
|
40
|
+
|
|
41
|
+
if items:
|
|
42
|
+
for item in items:
|
|
43
|
+
if type(item) is tuple:
|
|
44
|
+
if not item[1]:
|
|
45
|
+
ignoreList.append(item[0])
|
|
46
|
+
|
|
47
|
+
return ignoreList
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def extract_items(items):
|
|
51
|
+
item_list = []
|
|
52
|
+
|
|
53
|
+
if items:
|
|
54
|
+
for item in items:
|
|
55
|
+
if type(item) is str:
|
|
56
|
+
item_list.append(item)
|
|
57
|
+
elif type(item) is tuple:
|
|
58
|
+
item_list.extend([item[0]] * item[1])
|
|
59
|
+
|
|
60
|
+
return item_list
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def generate(items, pick=None, min_picks=None, max_picks=None):
|
|
64
|
+
|
|
65
|
+
item_list = extract_items(items)
|
|
66
|
+
min_picked_list = extract_items(min_picks)
|
|
67
|
+
max_picked_list = extract_items(max_picks)
|
|
68
|
+
pick_ignore_list = extract_ignored_items(max_picks)
|
|
69
|
+
|
|
70
|
+
rest_pick = pick - len(min_picked_list)
|
|
71
|
+
|
|
72
|
+
rest_item_list = getDiff(getDiff(item_list, min_picked_list), pick_ignore_list)
|
|
73
|
+
|
|
74
|
+
result = get_result(rest_item_list, min_picked_list, max_picked_list, rest_pick)
|
|
75
|
+
|
|
76
|
+
return result
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from custom_combination import CustomCombination
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def get_combination_count(combination_generator):
|
|
5
|
+
result = set()
|
|
6
|
+
for combination in combination_generator:
|
|
7
|
+
result.add(",".join(combination))
|
|
8
|
+
return len(result)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def test_picks():
|
|
12
|
+
results = CustomCombination.generate(
|
|
13
|
+
items=["a", "a", "b", "b", "c", "c", "c", "d"],
|
|
14
|
+
pick=2
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
assert len(results) == 9
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_public_api():
|
|
21
|
+
results = CustomCombination.generate(
|
|
22
|
+
items=["a", "a", "b", "b", "c", "c", "c", "d"],
|
|
23
|
+
pick=3,
|
|
24
|
+
min_picks=["b"],
|
|
25
|
+
max_picks=[("a", 0)]
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
assert results == [["b", "c", "c"], ["b", "c", "d"]]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_min_picks():
|
|
32
|
+
results = CustomCombination.generate(
|
|
33
|
+
items=["a", "a", "b", "b", "c", "c", "c", "d"],
|
|
34
|
+
pick=3,
|
|
35
|
+
min_picks=["b"]
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
assert results == [["a", "a", "b"], ["a", "b", "c"], ["a", "b", "d"], ["b", "c", "c"], ["b", "c", "d"]]
|
|
39
|
+
|
|
40
|
+
def test_max_picks():
|
|
41
|
+
results = CustomCombination.generate(
|
|
42
|
+
items=["a", "a", "b", "b", "c", "c", "c", "d"],
|
|
43
|
+
pick=3,
|
|
44
|
+
max_picks=["c"]
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
assert results == [
|
|
48
|
+
["a", "a", "b"], ["a", "a", "c"], ["a", "a", "d"], ["a", "b", "b"],
|
|
49
|
+
["a", "b", "c"], ["a", "b", "d"], ["a", "c", "d"], ["b", "b", "c"],
|
|
50
|
+
["b", "b", "d"], ["b", "c", "d"]]
|