0

In this question Set partitions in Python the making of the partitions of a set is done.

I need the partitions of a set in k parts. For example, the partitions of 4 elements [1,2,3,4] are:

    1 [[1, 2, 3, 4]]
    2 [[1], [2, 3, 4]]
    3 [[1, 2], [3, 4]]
    4 [[1, 3, 4], [2]]
    5 [[1], [2], [3, 4]]
    6 [[1, 2, 3], [4]]
    7 [[1, 4], [2, 3]]
    8 [[1], [2, 3], [4]]
    9 [[1, 3], [2, 4]]
    10 [[1, 2, 4], [3]]
    11 [[1], [2, 4], [3]]
    12 [[1, 2], [3], [4]]
    13 [[1, 3], [2], [4]]
    14 [[1, 4], [2], [3]]
    15 [[1], [2], [3], [4]]

but only these have 3 parts:

    11 [[1], [2, 4], [3]]
    12 [[1, 2], [3], [4]]
    13 [[1, 3], [2], [4]]
    14 [[1, 4], [2], [3]]

any idea to do this, without making all partitions?

partitions of a set in k parts

petezurich
  • 9,280
  • 9
  • 43
  • 57
  • 1
    "but only these have 3 parts:" - what do you mean by that? What code have you used to perform the partition? Please provide a [mre] with input and expected output. – h0r53 Jun 05 '23 at 17:02
  • 1
    You can use more_itertools with k=3 https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.set_partitions – crissal Jun 05 '23 at 17:05
  • a googleable term that describes this problem is "power set" – Joran Beasley Jun 05 '23 at 17:23
  • thanks. h0r53, the example is taken from the link. crissal, your solution is perfect, but I don't have a chain 'abcdef', but a list. – Ramon Masià Jun 05 '23 at 20:07
  • https://stackoverflow.com/questions/1851134/generate-all-binary-strings-of-length-n-with-k-bits-set << use this, interpreting set bits as partitions. – Dave Jun 06 '23 at 13:04
  • If I'm searching for partitions with k parts, but only those parts that have from n to m elements. – Ramon Masià Jun 08 '23 at 18:37

0 Answers0