Skip to content

Finding all the possible partitions of a list maintaining in the partition the order of the elements in the original list, and all the elements in the original list must be in some partition.

License

Notifications You must be signed in to change notification settings

anselmobattisti/komby

Repository files navigation

komby

The Problem

Finding all the possible partitions of a list maintaining in the partition the order of the elements in the original list, and all the elements in the original list must be in some partition.

A list with 3 elements, for example [1, 2, 3], will produce 2n-1 = 4 possible partitions:

1 - [[1, 2, 3]]
2 - [[1, 2], [3]]
3 - [[1], [2, 3]]
4 - [[1], [2], [3]]

Usage

Code:

    from komby.komby import Komby

    data = [1, 2, 3]

    print("Number of possible partitions: {}.\n".format(Komby.total_partitions(data)))

    partitions = Komby.partitions(data, True)

    print("Partitions:")
    for p in partitions:
        print("\t - {}".format(p))

Output:

Number of possible partitions: 4.

Partitions:
	 - [[1, 2, 3]]
	 - [[1, 2], [3]]
	 - [[1], [2, 3]]
	 - [[1], [2], [3]]

Install

You can install Komby easily via pip:

pip install -U komby

Tests

Tests

About

Finding all the possible partitions of a list maintaining in the partition the order of the elements in the original list, and all the elements in the original list must be in some partition.

Resources

License

Stars

Watchers

Forks

Packages

No packages published