Skip to main content

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

Metadata

Release files for stacksplit 0.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stacksplit 0.0.0
File Size Uploaded
stacksplit-0.0.0.tar.gz 46.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stacksplit 0.0.0
File Interpreter ABI Platform
stacksplit-0.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 51.4 kB

Release files / stacksplit-0.0.0.tar.gz

Download URL stacksplit-0.0.0.tar.gz
Size 46.0 kB
Tags Source
SHA-256 checksum
How to use checksums
64eac9383e37e695b07dffec87c0edfae54bd201ab0cdabd00ceb689aa96b5e4
BLAKE2b-256 checksum
How to use checksums
d6c193e3beba162d4199f01c37e3328a5a48863fd819fff6bdbdd73b5f8718ff
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / stacksplit-0.0.0-py2.py3-none-any.whl

Download URL stacksplit-0.0.0-py2.py3-none-any.whl
Size 5.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e2b268bde1fea699664bb9c2798f7a2faa735d1b43eb3217ad0c1c4bd8b393b9
BLAKE2b-256 checksum
How to use checksums
32e673c3d028c545a2bc68386fd8fa94ad897eb4138fdae141dbbd3a93afb8f3
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page