September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,49 +1,49 @@
|
|||
#import system.
|
||||
#import system'dynamic.
|
||||
#import extensions.
|
||||
#import system'routines.
|
||||
#import system'collections.
|
||||
import system'dynamic.
|
||||
import extensions.
|
||||
import system'routines.
|
||||
import system'collections.
|
||||
|
||||
#class(extension)algorithmOp
|
||||
extension algorithmOp
|
||||
{
|
||||
#method s_of_n
|
||||
s_of_n
|
||||
[
|
||||
#var counter := Integer new.
|
||||
var counter := Integer new.
|
||||
var n := self.
|
||||
|
||||
^ ArrayList new mix &into:
|
||||
^ ArrayList new; mixInto:
|
||||
{
|
||||
eval : n
|
||||
eval : i
|
||||
[
|
||||
counter += 1.
|
||||
counter append:1.
|
||||
|
||||
(this length < self)
|
||||
? [ this += n. ]
|
||||
! [
|
||||
(randomGenerator eval:counter < self)
|
||||
? [ this@(randomGenerator eval:self) := n. ].
|
||||
if (self length < n)
|
||||
[ self append:i ];
|
||||
[
|
||||
if(randomGenerator eval:counter < n)
|
||||
[ self[randomGenerator eval:n] := i ].
|
||||
].
|
||||
|
||||
^ this array.
|
||||
^ self array
|
||||
]
|
||||
}.
|
||||
]
|
||||
}
|
||||
|
||||
#symbol program =
|
||||
program =
|
||||
[
|
||||
#var bin := Array new:10 set &every:(&index:n) [ Integer new ].
|
||||
0 till:10000 &doEach: trial
|
||||
var bin := Array new:10; populate(:n)( Integer new ).
|
||||
0 till:10000 do(:trial)
|
||||
[
|
||||
#var s_of_n := 3 s_of_n.
|
||||
var s_of_n := 3 s_of_n.
|
||||
|
||||
0 till:10 &doEach:n
|
||||
0 till:10 do(:n)
|
||||
[
|
||||
#var sample := s_of_n eval:n.
|
||||
var sample := s_of_n eval:n.
|
||||
|
||||
(n == 9)
|
||||
? [ sample run &each: i [ bin@i += 1. ]. ].
|
||||
].
|
||||
if (n == 9)
|
||||
[ sample forEach(:i) [ bin[i] append:1 ] ]
|
||||
]
|
||||
].
|
||||
|
||||
console writeLine:bin.
|
||||
console printLine:bin; readChar.
|
||||
].
|
||||
|
|
|
|||
31
Task/Knuths-algorithm-S/Kotlin/knuths-algorithm-s.kotlin
Normal file
31
Task/Knuths-algorithm-S/Kotlin/knuths-algorithm-s.kotlin
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// version 1.1.2
|
||||
|
||||
import java.util.Random
|
||||
|
||||
class SOfN<T>(val n: Int) {
|
||||
private val sample = ArrayList<T>(n)
|
||||
private var i = 0
|
||||
|
||||
private companion object {
|
||||
val rand = Random()
|
||||
}
|
||||
|
||||
fun process(item: T): ArrayList<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)
|
||||
var sample: ArrayList<Int>? = null
|
||||
for (i in 0..9) sample = sOfn.process(i)
|
||||
for (s in sample!!) bin[s]++
|
||||
}
|
||||
println(bin.contentToString())
|
||||
}
|
||||
8
Task/Knuths-algorithm-S/Zkl/knuths-algorithm-s-1.zkl
Normal file
8
Task/Knuths-algorithm-S/Zkl/knuths-algorithm-s-1.zkl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fcn s_of_n_creator(n){
|
||||
fcn(item,ri,N,samples){
|
||||
i:=ri.inc(); // 1,2,3,4,...
|
||||
if(i<=N) samples.append(item);
|
||||
else if ((0).random(i) < N) samples[(0).random(N)] = item;
|
||||
samples
|
||||
}.fp1(Ref(1),n,L())
|
||||
}
|
||||
2
Task/Knuths-algorithm-S/Zkl/knuths-algorithm-s-2.zkl
Normal file
2
Task/Knuths-algorithm-S/Zkl/knuths-algorithm-s-2.zkl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
s3:=s_of_n_creator(3);
|
||||
[0..9].pump(List,s3,"copy").println();
|
||||
6
Task/Knuths-algorithm-S/Zkl/knuths-algorithm-s-3.zkl
Normal file
6
Task/Knuths-algorithm-S/Zkl/knuths-algorithm-s-3.zkl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
dist:=L(0,0,0,0,0,0,0,0,0,0);
|
||||
do(0d100_000){
|
||||
(0).pump(10,Void,s_of_n_creator(3)).apply2('wrap(n){dist[n]=dist[n]+1})
|
||||
}
|
||||
N:=dist.sum();
|
||||
dist.apply('wrap(n){"%.2f%%".fmt(n.toFloat()/N*100)}).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue