Permutations by swapping: Difference between revisions

Content added Content deleted
m (→‎{{header|C}}: Remove vanity tags)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 903: Line 903:
Perm: [2, 1, 3, 4] Sign: -1
Perm: [2, 1, 3, 4] Sign: -1
</pre>
</pre>

=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
See [http://www.rosettacode.org/wiki/Zebra_puzzle#F.23] for an example using this module
See [http://www.rosettacode.org/wiki/Zebra_puzzle#F.23] for an example using this module
Line 957: Line 958:
<null>
<null>
</pre>
</pre>

=={{header|Forth}}==
=={{header|Forth}}==
{{libheader|Forth Scientific Library}}
{{libheader|Forth Scientific Library}}
Line 2,078: Line 2,080:
{{out}}
{{out}}
The output is the same as the first perl solution.
The output is the same as the first perl solution.

=={{header|Perl 6}}==

=== Recursive ===
{{works with|rakudo|2015-09-25}}
<lang perl6>sub insert($x, @xs) { ([flat @xs[0 ..^ $_], $x, @xs[$_ .. *]] for 0 .. +@xs) }
sub order($sg, @xs) { $sg > 0 ?? @xs !! @xs.reverse }
multi perms([]) {
[] => +1
}
multi perms([$x, *@xs]) {
perms(@xs).map({ |order($_.value, insert($x, $_.key)) }) Z=> |(+1,-1) xx *
}
.say for perms([0..2]);</lang>

{{out}}
<pre>[0 1 2] => 1
[1 0 2] => -1
[1 2 0] => 1
[2 1 0] => -1
[2 0 1] => 1
[0 2 1] => -1</pre>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,519: Line 2,496:
1, 0, 2, 3 (-1)
1, 0, 2, 3 (-1)
</pre>
</pre>

=={{header|Raku}}==
(formerly Perl 6)

=== Recursive ===
{{works with|rakudo|2015-09-25}}
<lang perl6>sub insert($x, @xs) { ([flat @xs[0 ..^ $_], $x, @xs[$_ .. *]] for 0 .. +@xs) }
sub order($sg, @xs) { $sg > 0 ?? @xs !! @xs.reverse }
multi perms([]) {
[] => +1
}
multi perms([$x, *@xs]) {
perms(@xs).map({ |order($_.value, insert($x, $_.key)) }) Z=> |(+1,-1) xx *
}
.say for perms([0..2]);</lang>

{{out}}
<pre>[0 1 2] => 1
[1 0 2] => -1
[1 2 0] => 1
[2 1 0] => -1
[2 0 1] => 1
[0 2 1] => -1</pre>


=={{header|REXX}}==
=={{header|REXX}}==
Line 2,700: Line 2,703:
}</lang>
}</lang>
{{Out}}See it in running in your browser by [https://scastie.scala-lang.org/DdM4xnUnQ2aNGP481zwcrw Scastie (JVM)].
{{Out}}See it in running in your browser by [https://scastie.scala-lang.org/DdM4xnUnQ2aNGP481zwcrw Scastie (JVM)].

=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Perl}}
{{trans|Perl}}