partitions(array)

partitions(array)

Generate all set partitions of the elements of an array, represented as arrays of arrays. Because the number of partitions can be very large, this function returns an iterator object. Use collect(partitions(array)) to get an array of all partitions. The number of partitions to generate can be efficiently computed using length(partitions(array)).

Examples

  1. Generate all partitions of n with m integers:

    julia> partitions(5, 3)
    15-element PartitionIterator{3,Int64}:
    [0, 0, 5]
    [0, 1, 4]
    [0, 2, 3]
    [0, 3, 2]
    [0, 4, 1]
    [0, 5, 0]
    [1, 0, 4]
    [1, 1, 3]
    [1, 2, 2]
    [1, 3, 1]
    [1, 4, 0]
    [2, 0, 3]
    [2, 1, 2]
    [2, 2, 1]
    [2, 3, 0]

    This example generates all arrays of 3 integers that sum to 5.

  2. Collect all partitions into an array:

    julia> collect(partitions(4, 2))
    5-element Array{Array{Int64,1},1}:
    [0, 4]
    [1, 3]
    [2, 2]
    [3, 1]
    [4, 0]

    It collects all partitions of 2 integers that sum to 4 into an array.

  3. Get the number of partitions:
    julia> length(partitions(3, 2))
    4

    It efficiently computes the number of partitions of 2 integers that sum to 3.

Common mistake example:

julia> partitions(2, 3)
ERROR: ArgumentError: The number of integers (m) cannot be greater than the sum (n).

In this example, the function throws an error because the number of integers (m) is greater than the sum (n). It's essential to ensure that the number of integers does not exceed the sum to avoid this error.

See Also

Array, broadcast, cat, combinations, conj!, digits!, fieldnames, fill, fill!, last, length, maximum, minimum, ones, parent, parentindexes, partitions, permutations, pointer, pointer_to_array, promote_shape, rand!, reshape, scale, similar, sum, sum_kbn, takebuf_array, transpose!, vec, zeros,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: