Skip to main content

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)

Downloads PyPI - Downloads Crates.io Crates.io (recent) GitHub all releases GitHub Repo stars

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dpss-0.16.0.tar.gz (15.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

dpss-0.16.0-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.whl (985.8 kB view details)

Uploaded PyPymanylinux: glibc 2.5+ x86-64

dpss-0.16.0-cp310-none-win_amd64.whl (143.6 kB view details)

Uploaded CPython 3.10Windows x86-64

dpss-0.16.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl (985.6 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.5+ x86-64

dpss-0.16.0-cp39-none-win_amd64.whl (143.6 kB view details)

Uploaded CPython 3.9Windows x86-64

dpss-0.16.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (985.6 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.5+ x86-64

dpss-0.16.0-cp38-none-win_amd64.whl (143.6 kB view details)

Uploaded CPython 3.8Windows x86-64

dpss-0.16.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (985.4 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.5+ x86-64

dpss-0.16.0-cp37-none-win_amd64.whl (143.5 kB view details)

Uploaded CPython 3.7Windows x86-64

dpss-0.16.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (985.3 kB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.5+ x86-64

File details

Details for the file dpss-0.16.0.tar.gz.

File metadata

  • Download URL: dpss-0.16.0.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for dpss-0.16.0.tar.gz
Algorithm Hash digest
SHA256 7a39664911c719d132711cb420fa86e7240cae7afe8fbe16e36762cb3ffa5a64
MD5 bfe0b892b63aacc329abd63702146e23
BLAKE2b-256 4c931efcdb0f5b3a6ae10468737c77af93851375a57b6aa465fe90eec51069d5

See more details on using hashes here.

File details

Details for the file dpss-0.16.0-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

  • Download URL: dpss-0.16.0-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • Upload date:
  • Size: 985.8 kB
  • Tags: PyPy, manylinux: glibc 2.5+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for dpss-0.16.0-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cd5a5fd09feffe9a22a4c931d3135e58f6aa06177f8243071f76afb9d5f26303
MD5 36e5ee1e75bd3d12a92f2eac8387368a
BLAKE2b-256 a5f2823f3780a8af159c699a8af3cececb6844373598e8282099a09d6471304c

See more details on using hashes here.

File details

Details for the file dpss-0.16.0-cp310-none-win_amd64.whl.

File metadata

  • Download URL: dpss-0.16.0-cp310-none-win_amd64.whl
  • Upload date:
  • Size: 143.6 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for dpss-0.16.0-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 e32214a6cfbd27c0d24815331da690a05f0b91641020681cbfef9dc6714b92a7
MD5 4ad7605618f40c140d74c63586a80c2f
BLAKE2b-256 7edb9e6622dea2ac3cce5943b260e71a5df1e3df4298518acbbcd463233d9ddc

See more details on using hashes here.

File details

Details for the file dpss-0.16.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

  • Download URL: dpss-0.16.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • Upload date:
  • Size: 985.6 kB
  • Tags: CPython 3.10, manylinux: glibc 2.5+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for dpss-0.16.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0057acf4e63ef279254c10048166329ed60cc751710272b6640b89a6754c0558
MD5 2c393526e66c10f86fa81c5ca5f5b02e
BLAKE2b-256 f34608a06abd99e0eede2a73e38a9af62d8972c22fa21b17fbde44f056397ddf

See more details on using hashes here.

File details

Details for the file dpss-0.16.0-cp39-none-win_amd64.whl.

File metadata

  • Download URL: dpss-0.16.0-cp39-none-win_amd64.whl
  • Upload date:
  • Size: 143.6 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for dpss-0.16.0-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 ae502688e82b77640119c88baa25e850fd5dbc82260e4d376ee4a8c64113223e
MD5 278bbc09fe4e42e86b4c4a444b53685c
BLAKE2b-256 978d7998867d8c8dae0a981ad13c0a9ba2e910aa10597f4e8814010d151992b1

See more details on using hashes here.

File details

Details for the file dpss-0.16.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

  • Download URL: dpss-0.16.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • Upload date:
  • Size: 985.6 kB
  • Tags: CPython 3.9, manylinux: glibc 2.5+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for dpss-0.16.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 af7f618b4e9a8c34bbdda39f9497d10c267dd44e2ae1c01fd4935bb9cde597ac
MD5 e508e813def1b11d80c82520d63e5b0e
BLAKE2b-256 de2335c686328c4fdebb51da27faf5adf912a1d25928b3ea3842ca8d9e24b5cd

See more details on using hashes here.

File details

Details for the file dpss-0.16.0-cp38-none-win_amd64.whl.

File metadata

  • Download URL: dpss-0.16.0-cp38-none-win_amd64.whl
  • Upload date:
  • Size: 143.6 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for dpss-0.16.0-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 730a5e2f91c13ea423a797b922fba952d799103ae8dd923841f584f3381eef8e
MD5 53f388fbfe09f27d8b5cb68bff74a0be
BLAKE2b-256 9201ab1059cd02dd6766e1d0c12283b3e465b85c570bd87b4f1c0d5c3ea9e6d0

See more details on using hashes here.

File details

Details for the file dpss-0.16.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

  • Download URL: dpss-0.16.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • Upload date:
  • Size: 985.4 kB
  • Tags: CPython 3.8, manylinux: glibc 2.5+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for dpss-0.16.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2c4eae6fb6bd6ecda0c1b0bdeb201d7238bd7a53c812bea36c4edfca3f30cbff
MD5 1ac35dd418ac5fb138aa90d4181a3800
BLAKE2b-256 2106ae9a765aa05bd9c4dbd76ce68ea4ba0089138a96239c1eb6131081c91c38

See more details on using hashes here.

File details

Details for the file dpss-0.16.0-cp37-none-win_amd64.whl.

File metadata

  • Download URL: dpss-0.16.0-cp37-none-win_amd64.whl
  • Upload date:
  • Size: 143.5 kB
  • Tags: CPython 3.7, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for dpss-0.16.0-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 3b5032656335ff1bc43118ae51d091ca0dfcf5ad34a95c7e1e423b3c8e90ba49
MD5 0c304e7ad984d6cc2e08e8a537f644d8
BLAKE2b-256 db1206cb20695554d6d85516b35b0338026e117819960d48c0aaca01d53e79c4

See more details on using hashes here.

File details

Details for the file dpss-0.16.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

  • Download URL: dpss-0.16.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • Upload date:
  • Size: 985.3 kB
  • Tags: CPython 3.7m, manylinux: glibc 2.5+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for dpss-0.16.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a5fbaecfd8f44270098cdb1de31e26dd149cf0b787acdfb53149ba4733eff7b9
MD5 4068ab55d8f5d50c0650c0e04d3856bf
BLAKE2b-256 ab99fd5d6f6730c88462791bff3fe653e5d9763025632b17da1aa4364db3d963

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page