Giter VIP home page Giter VIP logo

combinatoricskt's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.