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


Release history Release notifications

This version

0.0.0

Download files

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

Files for stacksplit, version 0.0.0
Filename, size File type Python version Upload date Hashes
Filename, size stacksplit-0.0.0-py2.py3-none-any.whl (5.4 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size stacksplit-0.0.0.tar.gz (46.0 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page