This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package permutations | |
import java.util.* | |
interface Circular<T> : Iterable<T> { | |
fun state(): T | |
fun inc() | |
fun isZero(): Boolean // `true` in exactly one state | |
fun hasNext(): Boolean // `false` if the next state `isZero()` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Circular<T> : Iterable<T> { | |
fun state(): T | |
fun inc() | |
fun isZero(): Boolean // `true` in exactly one state | |
fun hasNext(): Boolean // `false` if the next state `isZero()` | |
override fun iterator() : Iterator<T> { | |
return object : Iterator<T> { | |
var started = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ring(val size: Int) : Circular<Int> { | |
private var state = 0 | |
override fun state() = state | |
override fun inc() {state = (1 + state) % size} | |
override fun isZero() = (state == 0) | |
override fun hasNext() = (state != size - 1) | |
init { | |
assert(size > 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class CircularList<E, H: Circular<E>>(val size: Int) : Circular<List<E>> { | |
protected abstract val state: List<H> // state.size == size | |
override fun inc() { | |
state.forEach { | |
it.inc() | |
if(! it.isZero()) return | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class IntCombinations(size: Int) : CircularList<Int, Ring>(size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BinaryBits(N: Int) : IntCombinations(N) { | |
override val state = Array(N, {Ring(2)}).toList() | |
override fun state() = state.map {it.state()}.reversed() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.* | |
class Permutations(N: Int) : IntCombinations(N) { | |
override val state = mutableListOf<Ring>() | |
init { | |
for(i in N downTo 1) { | |
state += Ring(i) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args: Array<String>) { | |
val n = 100 | |
val sumNumbers = Ring(n).sum() | |
println("In theory, the sum of numbers 0..${n - 1} is ${n * (n - 1) / 2}.") | |
println("Which is consistent with a practical result of $sumNumbers.\n") | |
BinaryBits(5).asSequence().filter {it.sum() == 2}.take(5).forEach { println(it) } | |
println("\npermutations of 3 elements:") | |
for(configuration in Permutations(3)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Circular<T> { | |
fun state(): T | |
fun inc() | |
fun isZero(): Boolean // `true` in exactly one state | |
fun hasNext(): Boolean // `false` if the next state `isZero()` | |
} |