|
| 1 | +package utils |
| 2 | + |
| 3 | +// https://en.wikipedia.org/wiki/Composition_(combinatorics) |
| 4 | + |
| 5 | +/** |
| 6 | + * In mathematics, a composition of an integer n is a way of writing n as the sum of a sequence of (strictly) |
| 7 | + * positive integers. |
| 8 | + * @param n the number to be composed |
| 9 | + * @param minParts the minimum number of parts for the compositions created |
| 10 | + * @param maxParts the maximum number of parts for the compositions created |
| 11 | + */ |
| 12 | +fun compositionsOf(n: Int, minParts: Int = 0, maxParts: Int = Int.MAX_VALUE): Sequence<List<Int>> = |
| 13 | + when { |
| 14 | + minParts > maxParts || maxParts <= 0 || minParts < 0 -> emptySequence() |
| 15 | + n <= 0 -> emptySequence() |
| 16 | + n < minParts -> emptySequence() |
| 17 | + maxParts == 1 -> sequenceOf(listOf(n)) |
| 18 | + else -> sequence { |
| 19 | + val newMin = (minParts - 1).coerceAtLeast(0) |
| 20 | + val newMax = maxParts - 1 |
| 21 | + (n downTo 1).forEach { v -> |
| 22 | + if (n == v && newMin == 0) |
| 23 | + yield(listOf(n)) |
| 24 | + else |
| 25 | + compositionsOf(n - v, newMin, newMax).forEach { yield(listOf(v) + it) } |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | +fun kCompositionsOf(n: Int, k: Int): Sequence<List<Int>> = compositionsOf(n, k, k) |
| 31 | + |
| 32 | +fun restrictedCompositionsOf( |
| 33 | + n: Int, |
| 34 | + restriction: IntRange, |
| 35 | + minParts: Int = 0, |
| 36 | + maxParts: Int = Int.MAX_VALUE |
| 37 | +): Sequence<List<Int>> = |
| 38 | + when { |
| 39 | + minParts > maxParts || maxParts <= 0 || minParts < 0 -> emptySequence() |
| 40 | + n <= 0 -> emptySequence() |
| 41 | + n < minParts * restriction.first.coerceAtLeast(1) -> emptySequence() |
| 42 | + maxParts == 1 -> if (n in restriction) sequenceOf(listOf(n)) else emptySequence() |
| 43 | + else -> sequence { |
| 44 | + val newMin = (minParts - 1).coerceAtLeast(0) |
| 45 | + val newMax = maxParts - 1 |
| 46 | + (n.coerceAtMost(restriction.last) downTo restriction.first.coerceAtLeast(1)).forEach { v -> |
| 47 | + if (v == n && newMin == 0) |
| 48 | + yield(listOf(n)) |
| 49 | + else |
| 50 | + restrictedCompositionsOf(n - v, restriction, newMin, newMax).forEach { yield(listOf(v) + it) } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | +fun restrictedCompositionsOf(n: Int, parts: List<Int>): Sequence<List<Int>> { |
| 56 | + val availableParts = parts.toMutableList().apply { sortDescending() } |
| 57 | + |
| 58 | + fun restrictedCompositions(n: Int): Sequence<List<Int>> = when { |
| 59 | + n <= 0 -> emptySequence() |
| 60 | + availableParts.isEmpty() -> emptySequence() |
| 61 | + availableParts.sum() < n -> emptySequence() |
| 62 | + else -> sequence { |
| 63 | + val possible = availableParts.filter { it <= n } |
| 64 | + possible.forEach { v -> |
| 65 | + if (v == n) |
| 66 | + yield(listOf(n)) |
| 67 | + else { |
| 68 | + availableParts -= v |
| 69 | + restrictedCompositions(n - v).forEach { yield(listOf(v) + it) } |
| 70 | + availableParts += v |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return restrictedCompositions(n) |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * In mathematics, a composition of an integer n is a way of writing n as the sum of a sequence of (strictly) |
| 81 | + * positive integers. Two sequences that differ in the order of their terms define different compositions of |
| 82 | + * their sum, while they are considered to define the same partition of that number. |
| 83 | + * @param n the number to be composed |
| 84 | + * @param maxParts the maximum number of parts for the partitions created |
| 85 | + * @param maxN limit the highest used number to maxN |
| 86 | + */ |
| 87 | +fun partitionsOf(n: Int, minParts: Int = 0, maxParts: Int = Int.MAX_VALUE, maxN: Int = n): Sequence<Collection<Int>> = |
| 88 | + when { |
| 89 | + minParts > maxParts || maxParts <= 0 || minParts < 0 -> emptySequence() |
| 90 | + n <= 0 -> emptySequence() |
| 91 | + n < minParts -> emptySequence() |
| 92 | + maxParts == 1 -> if (n <= maxN) sequenceOf(listOf(n)) else emptySequence() |
| 93 | + else -> sequence { |
| 94 | + val newMin = (minParts - 1).coerceAtLeast(0) |
| 95 | + val newMax = maxParts - 1 |
| 96 | + (n.coerceAtMost(maxN) downTo 1).forEach { v -> |
| 97 | + if (v == n && newMin == 0) |
| 98 | + yield(listOf(n)) |
| 99 | + else { |
| 100 | + partitionsOf(n - v, newMin, newMax, v).forEach { yield(listOf(v) + it) } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +fun kPartitionsOf(n: Int, k: Int): Sequence<Collection<Int>> = partitionsOf(n, k, k) |
| 107 | + |
| 108 | +fun restrictedPartitionsOf(n: Int, parts: List<Int>): Sequence<List<Int>> { |
| 109 | + |
| 110 | + fun restrictedPartitions(n: Int, availableParts: List<Int>): Sequence<List<Int>> = when { |
| 111 | + n <= 0 -> sequenceOf(emptyList()) |
| 112 | + availableParts.sum() < n -> emptySequence() |
| 113 | + availableParts.last() > n -> emptySequence() |
| 114 | + else -> sequence { |
| 115 | + val firstFitting = availableParts.indexOfFirst { it <= n } |
| 116 | + (firstFitting until availableParts.size).forEach { idx -> |
| 117 | + val v = availableParts[idx] |
| 118 | + restrictedPartitions(n - v, availableParts.subList(idx + 1, availableParts.size)).forEach { |
| 119 | + yield(listOf(v) + it) |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return restrictedPartitions(n, parts.sortedDescending()) |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * A weak composition of an integer n is similar to a composition of n, but allowing terms of the sequence |
| 130 | + * to be zero: it is a way of writing n as the sum of a sequence of non-negative integers. |
| 131 | + * @param n the number to be composed |
| 132 | + * @param k the number of parts for the weak compositions created |
| 133 | + * @param maxN limit the highest used number to maxN |
| 134 | + */ |
| 135 | +fun weakCompositionsOf(n: Int, k: Int, maxN: Int = n): Sequence<List<Int>> = |
| 136 | + when { |
| 137 | + k <= 0 -> emptySequence() |
| 138 | + k == 1 -> if (n <= maxN) sequenceOf(listOf(n)) else emptySequence() |
| 139 | + else -> sequence { |
| 140 | + (n.coerceAtMost(maxN) downTo 0).forEach { v -> |
| 141 | + weakCompositionsOf(n - v, k - 1, maxN).forEach { yield(listOf(v) + it) } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
0 commit comments