Skip to content

shiguruikai/combinatoricskt

Repository files navigation

Maven Central MIT License

combinatoricskt

A combinatorics library for Kotlin.

Generate the following sequence from Iterable or Array.
Iterable または Array から以下のシーケンスを生成する。

Download

Gradle Groovy DSL

implementation 'com.github.shiguruikai:combinatoricskt:1.6.0'

Gradle Kotlin DSL

implementation("com.github.shiguruikai:combinatoricskt:1.6.0")

Apache Maven

<dependency>
  <groupId>com.github.shiguruikai</groupId>
  <artifactId>combinatoricskt</artifactId>
  <version>1.6.0</version>
</dependency>

Permutations

listOf(1, 2, 3).permutations()   // [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
listOf(1, 2, 3).permutations(0)  // [[]]
listOf(1, 2, 3).permutations(1)  // [[1], [2], [3]]
listOf(1, 2, 3).permutations(2)  // [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
listOf(1, 2, 3).permutations(3)  // [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
listOf(1, 2, 3).permutations(4)  // []
Combinatorics.permutations(listOf(1, 2, 3))  // [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
PermutationsGenerator.indices(3, 2)          // [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]]

Permutations with Repetition

listOf(1, 2, 3).permutationsWithRepetition(0)  // [[]]
listOf(1, 2, 3).permutationsWithRepetition(1)  // [[1], [2], [3]]
listOf(1, 2, 3).permutationsWithRepetition(2)  // [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
listOf(1, 2, 3).permutationsWithRepetition(3)  // [[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1, 3, 1], [1, 3, 2], [1, 3, 3], [2, 1, 1], [2, 1, 2], [2, 1, 3], [2, 2, 1], [2, 2, 2], [2, 2, 3], [2, 3, 1], [2, 3, 2], [2, 3, 3], [3, 1, 1], [3, 1, 2], [3, 1, 3], [3, 2, 1], [3, 2, 2], [3, 2, 3], [3, 3, 1], [3, 3, 2], [3, 3, 3]]
Combinatorics.permutationsWithRepetition(listOf(1, 2, 3), 2)  // [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
PermutationsWithRepetitionGenerator.indices(3, 2)             // [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]

Derangements

listOf(1).derangements()           // []
listOf(1, 2).derangements()        // [[2, 1]]
listOf(1, 2, 3).derangements()     // [[2, 3, 1], [3, 1, 2]]
listOf(1, 2, 3, 4).derangements()  // [[2, 1, 4, 3], [2, 3, 4, 1], [2, 4, 1, 3], [3, 1, 4, 2], [3, 4, 1, 2], [3, 4, 2, 1], [4, 1, 2, 3], [4, 3, 1, 2], [4, 3, 2, 1]]
Combinatorics.derangements(listOf(1, 2, 3))  // [[2, 3, 1], [3, 1, 2]]
DerangementGenerator.indices(3)              // [[1, 2, 0], [2, 0, 1]]

Combinations

listOf(1, 2, 3).combinations(0)  // [[]]
listOf(1, 2, 3).combinations(1)  // [[1], [2], [3]]
listOf(1, 2, 3).combinations(2)  // [[1, 2], [1, 3], [2, 3]]
listOf(1, 2, 3).combinations(3)  // [[1, 2, 3]]
listOf(1, 2, 3).combinations(4)  // []
Combinatorics.combinations(listOf(1, 2, 3), 2)  // [[1, 2], [1, 3], [2, 3]]
CombinationsGenerator.indices(3, 2)             // [[0, 1], [0, 2], [1, 2]]

Combinations with Repetition

listOf(1, 2, 3).combinationsWithRepetition(0)  // [[]]
listOf(1, 2, 3).combinationsWithRepetition(1)  // [[1], [2], [3]]
listOf(1, 2, 3).combinationsWithRepetition(2)  // [[1, 1], [1, 2], [1, 3], [2, 2], [2, 3], [3, 3]]
listOf(1, 2, 3).combinationsWithRepetition(3)  // [[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3]]
listOf(1, 2, 3).combinationsWithRepetition(4)  // [[1, 1, 1, 1], [1, 1, 1, 2], [1, 1, 1, 3], [1, 1, 2, 2], [1, 1, 2, 3], [1, 1, 3, 3], [1, 2, 2, 2], [1, 2, 2, 3], [1, 2, 3, 3], [1, 3, 3, 3], [2, 2, 2, 2], [2, 2, 2, 3], [2, 2, 3, 3], [2, 3, 3, 3], [3, 3, 3, 3]]
Combinatorics.combinationsWithRepetition(listOf(1, 2, 3), 2) // [[1, 1], [1, 2], [1, 3], [2, 2], [2, 3], [3, 3]]
CombinationsWithRepetitionGenerator.indices(3, 2)            // [[0, 0], [0, 1], [0, 2], [1, 1], [1, 2], [2, 2]]

Cartesian Product

listOf(1, 2).cartesianProduct(listOf(3, 4, 5), listOf(6))        // [[1, 3, 6], [1, 4, 6], [1, 5, 6], [2, 3, 6], [2, 4, 6], [2, 5, 6]]
listOf('A', 'B').cartesianProduct(listOf('X', 'Y'))              // [[A, X], [A, Y], [B, X], [B, Y]]
listOf('A', 'B').cartesianProduct(listOf('X', 'Y'), repeat = 2)  // [[A, X, A, X], [A, X, A, Y], [A, X, B, X], [A, X, B, Y], [A, Y, A, X], [A, Y, A, Y], [A, Y, B, X], [A, Y, B, Y], [B, X, A, X], [B, X, A, Y], [B, X, B, X], [B, X, B, Y], [B, Y, A, X], [B, Y, A, Y], [B, Y, B, X], [B, Y, B, Y]]
Combinatorics.cartesianProduct(listOf("Java"), listOf(8, 11))    // [[Java, 8], [Java, 11]]
CartesianProductGenerator.indices(2, 2, repeat = 1)              // [[0, 0], [0, 1], [1, 0], [1, 1]]

Power Set

listOf(1, 2, 3).powerset()                // [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
Combinatorics.powerset(listOf('A', 'B'))  // [[], [A], [B], [A, B]]
PowerSetGenerator.indices(3)              // [[], [0], [1], [0, 1], [2], [0, 2], [1, 2], [0, 1, 2]]

License

MIT

About

A combinatorics library for Kotlin

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published