RosettaCodeData/Task/Knuths-algorithm-S/Elena/knuths-algorithm-s.elena

53 lines
1.1 KiB
Text
Raw Permalink Normal View History

2019-09-12 10:33:56 -07:00
import system'dynamic;
import extensions;
import system'routines;
import system'collections;
2016-12-05 22:15:40 +01:00
2017-09-23 10:01:46 +02:00
extension algorithmOp
2016-12-05 22:15:40 +01:00
{
2019-09-12 10:33:56 -07:00
s_of_n()
{
var counter := new Integer();
var n := self;
2020-02-17 23:21:07 -08:00
^ new ArrayList().mixInto(new::
2019-09-12 10:33:56 -07:00
{
eval(i)
2016-12-05 22:15:40 +01:00
{
2019-09-12 10:33:56 -07:00
counter.append:1;
if (__target.Length < n)
{
__target.append:i
}
else
{
if(randomGenerator.nextInt:counter < n)
{ __target[randomGenerator.nextInt:n] := i }
};
^ __target.Value
}
})
}
2016-12-05 22:15:40 +01:00
}
2019-09-12 10:33:56 -07:00
public program()
{
var bin := Array.allocate(10).populate:(n => new Integer());
for(int trial := 0, trial < 10000, trial += 1)
{
var s_of_n := 3.s_of_n();
2016-12-05 22:15:40 +01:00
2019-09-12 10:33:56 -07:00
for(int n := 0, n < 10, n += 1)
{
var sample := s_of_n.eval:n;
2016-12-05 22:15:40 +01:00
2017-09-23 10:01:46 +02:00
if (n == 9)
2019-09-12 10:33:56 -07:00
{ sample.forEach:(i){ bin[i].append:1 } }
}
};
2016-12-05 22:15:40 +01:00
2019-09-12 10:33:56 -07:00
console.printLine:bin.readChar()
}