Knuth's algorithm S: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) Rename Perl 6 -> Raku, alphabetize, minor clean-up |
|||
Line 465: | Line 465: | ||
9 30255 |
9 30255 |
||
</lang> |
</lang> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 667: | Line 666: | ||
using an input list -> [|(0, 29936); (1, 29973); (2, 29880); (3, 30160); (4, 30126); (5, 30123); (6, 30062); (7, 30053); (8, 29892); (9, 29795)|] |
using an input list -> [|(0, 29936); (1, 29973); (2, 29880); (3, 30160); (4, 30126); (5, 30123); (6, 30062); (7, 30053); (8, 29892); (9, 29795)|] |
||
</pre> |
</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
<lang go>package main |
<lang go>package main |
||
Line 1,218: | Line 1,218: | ||
;Sample output: |
;Sample output: |
||
<pre>30003 29923 30192 30164 29994 29976 29935 29860 30040 29913</pre> |
<pre>30003 29923 30192 30164 29994 29976 29935 29860 30040 29913</pre> |
||
=={{header|Perl 6}}== |
|||
<lang perl6>sub s_of_n_creator($n) { |
|||
my @sample; |
|||
my $i = 0; |
|||
-> $item { |
|||
if ++$i <= $n { |
|||
push @sample, $item; |
|||
} |
|||
elsif $i.rand < $n { |
|||
@sample[$n.rand] = $item; |
|||
} |
|||
@sample; |
|||
} |
|||
} |
|||
my @items = 0..9; |
|||
my @bin; |
|||
for ^100000 { |
|||
my &s_of_n = s_of_n_creator(3); |
|||
my @sample; |
|||
for @items -> $item { |
|||
@sample = s_of_n($item); |
|||
} |
|||
for @sample -> $s { |
|||
@bin[$s]++; |
|||
} |
|||
} |
|||
say @bin;</lang> |
|||
Output: |
|||
<pre>29975 30028 30246 30056 30004 29983 29836 29967 29924 29981</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 1,494: | Line 1,463: | ||
8 29940 |
8 29940 |
||
9 29777</pre> |
9 29777</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
<lang perl6>sub s_of_n_creator($n) { |
|||
my @sample; |
|||
my $i = 0; |
|||
-> $item { |
|||
if ++$i <= $n { |
|||
push @sample, $item; |
|||
} |
|||
elsif $i.rand < $n { |
|||
@sample[$n.rand] = $item; |
|||
} |
|||
@sample; |
|||
} |
|||
} |
|||
my @items = 0..9; |
|||
my @bin; |
|||
for ^100000 { |
|||
my &s_of_n = s_of_n_creator(3); |
|||
my @sample; |
|||
for @items -> $item { |
|||
@sample = s_of_n($item); |
|||
} |
|||
for @sample -> $s { |
|||
@bin[$s]++; |
|||
} |
|||
} |
|||
say @bin;</lang> |
|||
Output: |
|||
<pre>29975 30028 30246 30056 30004 29983 29836 29967 29924 29981</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 1,584: | Line 1,586: | ||
8 29953 |
8 29953 |
||
9 29852</pre> |
9 29852</pre> |
||
=={{header|Scala}}== |
|||
===Imperative (Ugly and side effects)=== |
|||
{{trans|Java}} |
|||
<lang Scala>import java.util |
|||
import scala.util.Random |
|||
object KnuthsAlgorithmS extends App { |
|||
import scala.collection.JavaConverters._ |
|||
val (n, rand, bin) = (3, Random, new Array[Int](10)) |
|||
for (_ <- 0 until 100000) { |
|||
val sample = new util.ArrayList[Int](n) |
|||
for (item <- 0 until 10) { |
|||
if (item < n) sample.add(item) |
|||
else if (rand.nextInt(item + 1) < n) |
|||
sample.asScala(rand.nextInt(n)) = item |
|||
} |
|||
for (s <- sample.asScala.toList) bin(s) += 1 |
|||
} |
|||
println(bin.mkString("[", ", ", "]")) |
|||
}</lang> |
|||
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/nlldfXD/0 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/WLaee5H9T72cximqK9gECA Scastie (JVM)]. |
|||
=={{header|Sidef}}== |
|||
{{trans|Perl 6}} |
|||
<lang ruby>func s_of_n_creator(n) { |
|||
var i = 0 |
|||
var sample = [] |
|||
{ |item| |
|||
if (++i <= n) { |
|||
sample << item; |
|||
} |
|||
elsif (i.rand < n) { |
|||
sample[n.rand] = item; |
|||
} |
|||
sample; |
|||
} |
|||
} |
|||
var items = 0..9; |
|||
var bin = []; |
|||
100000.times { |
|||
var s_of_n = s_of_n_creator(3); |
|||
var sample = [] |
|||
for item in items { |
|||
sample = s_of_n(item); |
|||
} |
|||
for s in sample { |
|||
bin[s] := 0 ++; |
|||
} |
|||
} |
|||
say bin;</lang> |
|||
{{out}} |
|||
<pre> |
|||
[30056, 29906, 30058, 29986, 30062, 29748, 29989, 29985, 30126, 30084] |
|||
</pre> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 1,709: | Line 1,650: | ||
frequency of 8: 30030 |
frequency of 8: 30030 |
||
frequency of 9: 30003 |
frequency of 9: 30003 |
||
</pre> |
|||
=={{header|Scala}}== |
|||
===Imperative (Ugly and side effects)=== |
|||
{{trans|Java}} |
|||
<lang Scala>import java.util |
|||
import scala.util.Random |
|||
object KnuthsAlgorithmS extends App { |
|||
import scala.collection.JavaConverters._ |
|||
val (n, rand, bin) = (3, Random, new Array[Int](10)) |
|||
for (_ <- 0 until 100000) { |
|||
val sample = new util.ArrayList[Int](n) |
|||
for (item <- 0 until 10) { |
|||
if (item < n) sample.add(item) |
|||
else if (rand.nextInt(item + 1) < n) |
|||
sample.asScala(rand.nextInt(n)) = item |
|||
} |
|||
for (s <- sample.asScala.toList) bin(s) += 1 |
|||
} |
|||
println(bin.mkString("[", ", ", "]")) |
|||
}</lang> |
|||
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/nlldfXD/0 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/WLaee5H9T72cximqK9gECA Scastie (JVM)]. |
|||
=={{header|Sidef}}== |
|||
{{trans|Perl 6}} |
|||
<lang ruby>func s_of_n_creator(n) { |
|||
var i = 0 |
|||
var sample = [] |
|||
{ |item| |
|||
if (++i <= n) { |
|||
sample << item; |
|||
} |
|||
elsif (i.rand < n) { |
|||
sample[n.rand] = item; |
|||
} |
|||
sample; |
|||
} |
|||
} |
|||
var items = 0..9; |
|||
var bin = []; |
|||
100000.times { |
|||
var s_of_n = s_of_n_creator(3); |
|||
var sample = [] |
|||
for item in items { |
|||
sample = s_of_n(item); |
|||
} |
|||
for s in sample { |
|||
bin[s] := 0 ++; |
|||
} |
|||
} |
|||
say bin;</lang> |
|||
{{out}} |
|||
<pre> |
|||
[30056, 29906, 30058, 29986, 30062, 29748, 29989, 29985, 30126, 30084] |
|||
</pre> |
</pre> |
||