Best shuffle: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 704:
return 0;
}</lang>
 
=={{header|C++}}==
{{works with|C++|11}}
{{trans|Java}}
<lang cpp>#include <iostream>
#include <sstream>
#include <algorithm>
 
using namespace std;
 
template <class S>
class BestShuffle {
public:
BestShuffle() : rd(), g(rd()) {}
 
S operator()(const S& s1) {
S s2 = s1;
shuffle(s2.begin(), s2.end(), g);
for (unsigned i = 0; i < s2.length(); i++)
if (s2[i] == s1[i])
for (unsigned j = 0; j < s2.length(); j++)
if (s2[i] != s2[j] && s2[i] != s1[j] && s2[j] != s1[i]) {
swap(s2[i], s2[j]);
break;
}
ostringstream os;
os << s1 << endl << s2 << " [" << count(s2, s1) << ']';
return os.str();
}
 
private:
static int count(const S& s1, const S& s2) {
auto count = 0;
for (unsigned i = 0; i < s1.length(); i++)
if (s1[i] == s2[i])
count++;
return count;
}
 
random_device rd;
mt19937 g;
};
 
int main(int argc, char* arguments[]) {
BestShuffle<basic_string<char>> bs;
for (auto i = 1; i < argc; i++)
cout << bs(basic_string<char>(arguments[i])) << endl;
return 0;
}</lang>
{{out}}
<pre>abracadabra
raabadabcar (0)
seesaw
wssaee (0)
grrrrrr
rgrrrrr (5)
pop
opp (1)
up
pu (0)
a
a (1)</pre>
 
=={{header|C sharp|C#}}==
Line 1,050 ⟶ 988:
a, a, (1)
</pre>
 
=={{header|C++}}==
{{works with|C++|11}}
{{trans|Java}}
<lang cpp>#include <iostream>
#include <sstream>
#include <algorithm>
 
using namespace std;
 
template <class S>
class BestShuffle {
public:
BestShuffle() : rd(), g(rd()) {}
 
S operator()(const S& s1) {
S s2 = s1;
shuffle(s2.begin(), s2.end(), g);
for (unsigned i = 0; i < s2.length(); i++)
if (s2[i] == s1[i])
for (unsigned j = 0; j < s2.length(); j++)
if (s2[i] != s2[j] && s2[i] != s1[j] && s2[j] != s1[i]) {
swap(s2[i], s2[j]);
break;
}
ostringstream os;
os << s1 << endl << s2 << " [" << count(s2, s1) << ']';
return os.str();
}
 
private:
static int count(const S& s1, const S& s2) {
auto count = 0;
for (unsigned i = 0; i < s1.length(); i++)
if (s1[i] == s2[i])
count++;
return count;
}
 
random_device rd;
mt19937 g;
};
 
int main(int argc, char* arguments[]) {
BestShuffle<basic_string<char>> bs;
for (auto i = 1; i < argc; i++)
cout << bs(basic_string<char>(arguments[i])) << endl;
return 0;
}</lang>
{{out}}
<pre>abracadabra
raabadabcar (0)
seesaw
wssaee (0)
grrrrrr
rgrrrrr (5)
pop
opp (1)
up
pu (0)
a
a (1)</pre>
 
=={{header|Clojure}}==
Line 2,471:
The output has the same format as the first perl implementation,
but only takes quadratic time per word.
 
=={{header|Perl 6}}==
{{trans|Sidef}}
{{works with|Rakudo Star|2015.12}}
 
<lang perl6>sub best-shuffle(Str $orig) {
 
my @s = $orig.comb;
my @t = @s.pick(*);
 
for ^@s -> $i {
for ^@s -> $j {
if $i != $j and @t[$i] ne @s[$j] and @t[$j] ne @s[$i] {
@t[$i, $j] = @t[$j, $i];
last;
}
}
}
 
my $count = 0;
for @t.kv -> $k,$v {
++$count if $v eq @s[$k]
}
 
return (@t.join, $count);
}
 
printf "%s, %s, (%d)\n", $_, best-shuffle $_
for <abracadabra seesaw elk grrrrrr up a>;</lang>
{{out}}
<pre>
abracadabra, raacarabadb, (0)
seesaw, wssaee, (0)
elk, lke, (0)
grrrrrr, rrrgrrr, (5)
up, pu, (0)
a, a, (1)
</pre>
 
=={{header|Phix}}==
Line 2,840 ⟶ 2,802:
true .
</pre>
 
=={{header|PureBasic}}==
This solution creates cycles of letters of letters that are then rotated to produce the final maximal shuffle. It includes an extra sort step that ensures the original string to be returned if it is repeatedly shuffled.
Line 3,115 ⟶ 3,078:
up, pu, (0)
a, a, (1)
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Sidef}}
{{works with|Rakudo Star|2015.12}}
 
<lang perl6>sub best-shuffle(Str $orig) {
 
my @s = $orig.comb;
my @t = @s.pick(*);
 
for ^@s -> $i {
for ^@s -> $j {
if $i != $j and @t[$i] ne @s[$j] and @t[$j] ne @s[$i] {
@t[$i, $j] = @t[$j, $i];
last;
}
}
}
 
my $count = 0;
for @t.kv -> $k,$v {
++$count if $v eq @s[$k]
}
 
return (@t.join, $count);
}
 
printf "%s, %s, (%d)\n", $_, best-shuffle $_
for <abracadabra seesaw elk grrrrrr up a>;</lang>
{{out}}
<pre>
abracadabra, raacarabadb, (0)
seesaw, wssaee, (0)
elk, lke, (0)
grrrrrr, rrrgrrr, (5)
up, pu, (0)
a, a, (1)
</pre>
 
Line 3,537 ⟶ 3,539:
}
</lang>
 
 
=={{header|Scheme}}==
10,327

edits