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

53 lines
1.1 KiB
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
import system'dynamic;
import extensions;
import system'routines;
import system'collections;
extension algorithmOp
{
s_of_n()
{
var counter := new Integer();
var n := self;
^ new ArrayList().mixInto(new
{
eval(i)
{
2024-03-06 22:25:12 -08:00
counter.append(1);
2023-07-01 11:58:00 -04:00
2023-12-16 21:33:55 -08:00
if (weak self.Length < n)
2023-07-01 11:58:00 -04:00
{
2024-03-06 22:25:12 -08:00
weak self.append(i)
2023-07-01 11:58:00 -04:00
}
else
{
2024-03-06 22:25:12 -08:00
if(randomGenerator.nextInt(counter) < n)
{ weak self[randomGenerator.nextInt(n)] := i }
2023-07-01 11:58:00 -04:00
};
2023-12-16 21:33:55 -08:00
^ weak self.Value
2023-07-01 11:58:00 -04:00
}
})
}
}
public program()
{
2024-03-06 22:25:12 -08:00
var bin := Array.allocate(10).populate::(n => new Integer());
for(int trial := 0; trial < 10000; trial += 1)
2023-07-01 11:58:00 -04:00
{
var s_of_n := 3.s_of_n();
2024-03-06 22:25:12 -08:00
for(int n := 0; n < 10; n += 1)
2023-07-01 11:58:00 -04:00
{
2024-03-06 22:25:12 -08:00
var sample := s_of_n.eval(n);
2023-07-01 11:58:00 -04:00
if (n == 9)
2024-03-06 22:25:12 -08:00
{ sample.forEach::(i){ bin[i].append(1) } }
2023-07-01 11:58:00 -04:00
}
};
2024-03-06 22:25:12 -08:00
console.printLine(bin).readChar()
2023-07-01 11:58:00 -04:00
}