Skip to main content

A simple Python library to generate all combinations of possible splits for a stack with given height.

Project description

Stacksplit

A simple Python library to generate all combinations of possible splits for a stack with given height.

Introduction

This library aims to generate all possible combinations to split a given integer num into a given number of parts; the sum of all those parts shall again be the given num.

We wrote this simple library out of lack of such functionality needed in a lecture of our Computer Science BSc-Degree. The original use case was to calculate and solve extended versions of the NIM-game, where towers of coins could also be splitted into multpile smaller towers.

Usage

Be sure to have stacksplit installed.

Import stacksplit library:

import stacksplit

The function provided is split and is implemented as a Python-generator; it takes 2 (optionally 3) arguments:

  • num: the Integer to be split up
  • parts: the number of parts
  • smallest: the smallest part shall be >= the given parameter. This parameter is optional and defaults to 1.
split(num, parts, smallest)

Each call returns a new tuple with a new combination with all elements summing up to num.

look at doc_strings and comments in init and core for help

Examples

Simple usage

import stacksplit

for s in stacksplit.split(50, 3):
  print(s)

Output:

(1, 1, 48)
(1, 2, 47)
...

More options

from stacksplit import split

for i in split(50, 3, 10):
  print(i)

Output:

(10, 10, 30)
(10, 11, 29)
...

Extended usecases

smallest can also be 0 or negative. The results will always sum up to num.

from stacksplit import split

for i in split(5, 3, -1):
  print(i)

Output:

(-1, -1, 7)
(-1, 0, 6)
(-1, 1, 5)
...

Performance

The library uses Python native generators to achieve the fast generation of results; however you have to understand that the problem itself is quite complex and the number of results will increment exponentially with higher values as parameters.

The following graphs visualize this growth of results.

Graph Description
constant parts, increment num y-axis: number of result
x-axis: the num parameter
parts: constant 4
constant num, increment parts y-axis: number of results
x-axis: the parts parameter
num: constant

Installation

This library can be installed via pip install stacksplit.

Arch Linux

The AUR package will be named python-stacksplit.

Testing

To run the tests for stacksplit:

You may then use these:

  • run normal tests: pipenv run python setup.py test
  • run tests with coverage: pipenv run python setup.py test --coverage
  • run tox tests: pipenv run tox (make sure you have interpreters for python - 3.4 to 3.7)

Note: It is possible to use a normal virtual environmet by installing the dev-dependencies from Pipfile by hand with pip. (For exact versions see Pipfile.lock)

Authors

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

stacksplit-0.0.0.tar.gz (46.0 kB view details)

Uploaded Source

Built Distribution

stacksplit-0.0.0-py2.py3-none-any.whl (5.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file stacksplit-0.0.0.tar.gz.

File metadata

  • Download URL: stacksplit-0.0.0.tar.gz
  • Upload date:
  • Size: 46.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for stacksplit-0.0.0.tar.gz
Algorithm Hash digest
SHA256 64eac9383e37e695b07dffec87c0edfae54bd201ab0cdabd00ceb689aa96b5e4
MD5 640ce9bacc9dfd321b6bf2a99da7f833
BLAKE2b-256 d6c193e3beba162d4199f01c37e3328a5a48863fd819fff6bdbdd73b5f8718ff

See more details on using hashes here.

File details

Details for the file stacksplit-0.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: stacksplit-0.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for stacksplit-0.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e2b268bde1fea699664bb9c2798f7a2faa735d1b43eb3217ad0c1c4bd8b393b9
MD5 789ffb57503f7f068cfd0d1b97027677
BLAKE2b-256 32e673c3d028c545a2bc68386fd8fa94ad897eb4138fdae141dbbd3a93afb8f3

See more details on using hashes here.

Supported by

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