Best shuffle: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (→‎{{header|Raku}}: use 'X' to avoid a nested loop)
Line 3,972: Line 3,972:
(formerly Perl 6)
(formerly Perl 6)
{{trans|Sidef}}
{{trans|Sidef}}
{{works with|Rakudo Star|2015.12}}


<syntaxhighlight lang=raku line>sub best-shuffle(Str $orig) {
<syntaxhighlight lang=raku line>sub best-shuffle(Str $orig) {

my @s = $orig.comb;
my @s = $orig.comb;
my @t = @s.pick(*);
my @t = @s.pick(*);


for ^@s -> $i {
for flat ^@s X ^@s -> \i,\j {
for ^@s -> $j {
if i != j and @t[i] ne @s[j] and @t[j] ne @s[i] {
if $i != $j and @t[$i] ne @s[$j] and @t[$j] ne @s[$i] {
@t[i,j] = @t[j,i] and last
@t[$i, $j] = @t[$j, $i];
last;
}
}
}
}
}
Line 3,993: Line 3,988:
}
}


return (@t.join, $count);
@t.join, $count;
}
}


printf "%s, %s, (%d)\n", $_, best-shuffle $_
printf "%s, %s, (%d)\n", $_, best-shuffle $_ for <abracadabra seesaw elk grrrrrr up a>;</syntaxhighlight>
for <abracadabra seesaw elk grrrrrr up a>;</syntaxhighlight>
{{out}}
{{out}}
<pre>abracadabra, raacarabadb, (0)
<pre>
abracadabra, raacarabadb, (0)
seesaw, wssaee, (0)
seesaw, wssaee, (0)
elk, lke, (0)
elk, lke, (0)
grrrrrr, rrrgrrr, (5)
grrrrrr, rrrgrrr, (5)
up, pu, (0)
up, pu, (0)
a, a, (1)
a, a, (1)</pre>
</pre>


=={{header|Rascal}}==
=={{header|Rascal}}==