Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
28
Task/Knuths-algorithm-S/Kotlin/knuths-algorithm-s-1.kotlin
Normal file
28
Task/Knuths-algorithm-S/Kotlin/knuths-algorithm-s-1.kotlin
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// version 1.2.51
|
||||
|
||||
import java.util.Random
|
||||
|
||||
val rand = Random()
|
||||
|
||||
class SOfN<T>(val n: Int) {
|
||||
private val sample = ArrayList<T>(n)
|
||||
private var i = 0
|
||||
|
||||
fun process(item: T): List<T> {
|
||||
if (++i <= n)
|
||||
sample.add(item)
|
||||
else if (rand.nextInt(i) < n)
|
||||
sample[rand.nextInt(n)] = item
|
||||
return sample
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val bin = IntArray(10)
|
||||
(1..100_000).forEach {
|
||||
val sOfn = SOfN<Int>(3)
|
||||
for (d in 0..8) sOfn.process(d)
|
||||
for (s in sOfn.process(9)) bin[s]++
|
||||
}
|
||||
println(bin.contentToString())
|
||||
}
|
||||
27
Task/Knuths-algorithm-S/Kotlin/knuths-algorithm-s-2.kotlin
Normal file
27
Task/Knuths-algorithm-S/Kotlin/knuths-algorithm-s-2.kotlin
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
// version 1.2.51
|
||||
|
||||
import java.util.Random
|
||||
|
||||
val rand = Random()
|
||||
|
||||
fun <T> SOfNCreator(n: Int): (T) -> List<T> {
|
||||
val sample = ArrayList<T>(n)
|
||||
var i = 0
|
||||
return {
|
||||
if (++i <= n)
|
||||
sample.add(it)
|
||||
else if (rand.nextInt(i) < n)
|
||||
sample[rand.nextInt(n)] = it
|
||||
sample
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val bin = IntArray(10)
|
||||
(1..100_000).forEach {
|
||||
val sOfn = SOfNCreator<Int>(3)
|
||||
for (d in 0..8) sOfn(d)
|
||||
for (s in sOfn(9)) bin[s]++
|
||||
}
|
||||
println(bin.contentToString())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue