Solves subset sum problem and returns a set of decomposed integers. It also can match corresponding numbers from two vectors and be used for Account reconciliation.
Project description
Subset Sum(dpss)
This is a Rust implementation that calculates subset sum problem using dynamic programming. It solves subset sum problem and returns a set of decomposed integers. It also can match corresponding numbers from two vectors and be used for Account reconciliation.
Any feedback is welcome!
There are three ways to use this program.
Here is an out of the box example you can run now in google colab. https://colab.research.google.com/github/europeanplaice/subset_sum/blob/main/python/python_subset_sum.ipynb
And it has three methods.
find_subset
- It finds a subset from an array.
find_subset_fast_only_positive
- It finds a subset from an array. It can't accept negative values but relatively faster.
Sequence Matcher
- It finds subset sum relationships with two arrays.
dpss
is short for dynamic programming subset sum
.
Links
Name | URL |
---|---|
github | https://github.com/europeanplaice/subset_sum |
crates.io | https://crates.io/crates/subset_sum |
docs.rs | https://docs.rs/subset_sum/latest/dpss/ |
pypi | https://pypi.org/project/dpss/ |
CLI
Installation
Binary files are provided on the Releases page. When you download one of these, please add it to your PATH manually.
Usage
Subset sum
First, you need to prepare a text file containing a set of integers like this
1
2
-3
4
5
and save it at any place.
Second, call subset_sum
with the path of the text file and the target sum.
Example
Call subset_sum.exe num_set.txt 3 3
The executable's name subset_sum.exe
would be different from your choice. Change this example along with your environment.
The second argument is the target sum.
The third argument is the maximum length of the combination.
In this example, the output is
[[2, 1], [4, -3, 2], [5, -3, 1]]
Sequence Matcher
arr1.txt
1980
2980
3500
4000
1050
arr2.txt
1950
2900
30
80
3300
200
3980
1050
20
Call subset_sum.exe arr1.txt arr2.txt 100 100 10
In this example, the output is
[([1050], [1050]), ([1980], [30, 1950]), ([2980], [80, 2900]), ([3500], [200, 3300]), ([4000], [20, 3980])]
[([1050], [1050]), ([1980], [30, 1950]), ([2980], [80, 2900]), ([3500, 4000], [20, 200, 3300, 3980])]
[([1050], [1050]), ([1980], [30, 1950]), ([3500], [200, 3300]), ([2980, 4000], [20, 80, 2900, 3980])]
...
Use in Python
installation
pip install dpss
Usage
find_subset
import inspect
import dpss
help(dpss.find_subset)
>>> find_subset(arr, value, max_length, /)
>>> Finds subsets sum of a target value. It can accept negative values.
>>> # Arguments
>>> * `arr` - An array.
>>> * `value` - The value to the sum of the subset comes.
>>> * `max_length` - The maximum length of combinations of the answer.
print(dpss.find_subset([1, -2, 3, 4, 5], 2, 3))
>>> [[4, -2], [3, -2, 1]]
find_subset_fast_only_positive
help(dpss.find_subset_fast_only_positive)
>>> find_subset_fast_only_positive(arr, value, max_length, /)
>>> Finds subsets sum of a target value. It can't accept negative values but relatively faster.
>>> # Arguments
>>> * `arr` - An array.
>>> * `value` - The value to the sum of the subset comes.
>>> * `max_length` - The maximum length of combinations of the answer.
print(dpss.find_subset_fast_only_positive([1, 2, 3, 4, 5], 10, 4))
>>> [[4, 3, 2, 1], [5, 3, 2], [5, 4, 1]]
sequence_matcher
help(dpss.sequence_matcher)
>>> sequence_matcher(keys, targets, max_key_length, max_target_length /)
>>> Finds the integers from two vectors that sum to the same value.
>>> This method assumes that the two vectors have Many-to-Many relationships.
>>> Each integer of the `keys` vector corresponds to the multiple integers of the `targets` vector.
>>> With this method, we can find some combinations of the integers.
>>> # Arguments
>>> * `keys` - An array.
>>> * `targets` - An array.
>>> * `max_key_length` - An integer.
>>> * `max_target_length` - An integer.
>>> * `n_candidates` - An integer.
print(dpss.sequence_matcher([1980, 2980, 3500, 4000, 1050], [1950, 2900, 30, 80, 3300, 200, 3980, 1050, 20], 10, 10, 10))
>>> [([1050], [1050]), ([1980], [30, 1950]), ([2980], [80, 2900]), ([3500], [200, 3300]), ([4000], [20, 3980])]
>>> [([1050], [1050]), ([1980], [30, 1950]), ([2980], [80, 2900]), ([3500, 4000], [20, 200, 3300, 3980])]
>>> [([1050], [1050]), ([1980], [30, 1950]), ([3500], [200, 3300]), ([2980, 4000], [20, 80, 2900, 3980])]
...
Use in Rust
Please check https://crates.io/crates/subset_sum.
Cargo.toml
[dependencies]
dpss = { version = "(version)", package = "subset_sum" }
Find subset
main.rs
use dpss::dp::find_subset;
fn main() {
let result = find_subset(&mut vec![1, 2, 3, 4, 5], 6, 3);
println!("{:?}", result);
}
Output
[[3, 2, 1], [4, 2], [5, 1]]
Sequence Matcher
main.rs
use dpss::dp::sequence_matcher;
fn main() {
let result = sequence_matcher(&mut vec![1980, 2980, 3500, 4000, 1050], &mut vec![1950, 2900, 30, 80, 3300, 200, 3980, 1050, 20], 10, 10, 10);
println!("{:?}", result);
}
Output
[([1050], [1050]), ([1980], [30, 1950]), ([2980], [80, 2900]), ([3500], [200, 3300]), ([4000], [20, 3980])]
[([1050], [1050]), ([1980], [30, 1950]), ([2980], [80, 2900]), ([3500, 4000], [20, 200, 3300, 3980])]
[([1050], [1050]), ([1980], [30, 1950]), ([3500], [200, 3300]), ([2980, 4000], [20, 80, 2900, 3980])]
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for dpss-0.17.1-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7df158e6318b88ed33e7254e8862fa8c4ff0628e5e68ec36ac5dc69e5615dbdb |
|
MD5 | f8159fb13ce0c1005afd0deac709d685 |
|
BLAKE2b-256 | 0cb62ce40696d2cb40be2a0ef363cde633c50caa5723fb7676b4b7b3f0c4dc00 |
Hashes for dpss-0.17.1-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e76841a67d1ccb82e493c3d9e51d5e8dfb335adef4466db28caf83d705a888d1 |
|
MD5 | 3a00b7812e541ddf5d4cba9b791b2513 |
|
BLAKE2b-256 | 64547bf6cda2ab508e81d1f26246ee0d7348213bc5054c8c1edc58163a4c14ec |
Hashes for dpss-0.17.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2350891df0cd94718f8e8689069803836d074757a876d8eb2beac55c424ebd37 |
|
MD5 | a08d811f9923c4589d270aa58c38d6e3 |
|
BLAKE2b-256 | b99e35948b36a8547979b5a99cf59d649ac3d6815dd0a71aefa438aa068f82b3 |
Hashes for dpss-0.17.1-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1dc61d166612b5fd3e45a227d2571faa97af2baadad55af78a95dc4948b2904 |
|
MD5 | 85c1bd956f0ea15f3a1fb4bc1389c7cc |
|
BLAKE2b-256 | afd56fe494639644a66e1134095f153a96da0dfc33a283207d04e348d48a72d2 |
Hashes for dpss-0.17.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9609d713b6866b4a6b2622e6969d39d892bad8fdab63ebb6020a4aaa1ea4d609 |
|
MD5 | 4647927e839831af2d2676bf374b6642 |
|
BLAKE2b-256 | 61e1e2c320be8965275187690061224be6c072c6253108eacfcc0b2e49569c15 |
Hashes for dpss-0.17.1-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73098084b50956956bdd98c833eaa03b25fedc365f46ee68247bcf538549252a |
|
MD5 | c109cbe0bf7cbb021540058df4869063 |
|
BLAKE2b-256 | ba025f32e13cf0100166bfccb2c9de40a22da3f0622298cf22ccbfef24adb074 |
Hashes for dpss-0.17.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eea7658e045c88e537e02acb91a41bbdb7b7f09f91e288ec993de9396c41a798 |
|
MD5 | 95bb325a718a0828960f2564bd45067a |
|
BLAKE2b-256 | 4528772dd8e6cc81d4f1235fed82a659585f24d2453e394ad17245e1284e0fec |
Hashes for dpss-0.17.1-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef33e477f2f59c48d221bd4dfae73bb4dc7dddc99e4ee4d3c39885626a71a9ce |
|
MD5 | b640e6dd1316ea60029855cb8d5618a2 |
|
BLAKE2b-256 | d41f3c619625b8a1e2e415f0011ba98ac7e301b130ab13adace1772951235c03 |
Hashes for dpss-0.17.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1702ffe9c2bff4d57ee53df69253c7e7b2f0bfdbbf2110ce653de0c3b5d9ec44 |
|
MD5 | ec7e4032873065f4fdfce2c376b2bae7 |
|
BLAKE2b-256 | 0b268c2f5c3dad86fc2a31caac7d543a0861d5e068b65e659aa0cfcea5e9c509 |