March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
|
|
@ -1,40 +1,41 @@
|
|||
import std.stdio, std.random, std.algorithm, std.conv, std.range,
|
||||
std.traits, std.typecons;
|
||||
std.traits, std.typecons;
|
||||
|
||||
auto bestShuffle(S)(in S orig) if (isSomeString!S) {
|
||||
static if (isNarrowString!S)
|
||||
auto o = to!dstring(orig);
|
||||
else alias orig o;
|
||||
immutable o = orig.dtext;
|
||||
else
|
||||
alias o = orig;
|
||||
|
||||
auto s = o.dup;
|
||||
randomShuffle(s);
|
||||
foreach (i, ref ci; s) {
|
||||
s.randomShuffle;
|
||||
|
||||
foreach (immutable i, ref ci; s) {
|
||||
if (ci != o[i])
|
||||
continue;
|
||||
foreach (j, ref cj; s)
|
||||
foreach (immutable j, ref cj; s)
|
||||
if (ci != cj && ci != o[j] && cj != o[i]) {
|
||||
swap(ci, cj);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return tuple(s, count!"a[0] == a[1]"(zip(s, o)));
|
||||
|
||||
return tuple(s, s.zip(o).count!q{ a[0] == a[1] });
|
||||
} unittest {
|
||||
assert("abracadabra".bestShuffle[1] == 0);
|
||||
assert("immediately".bestShuffle[1] == 0);
|
||||
assert("grrrrrr".bestShuffle[1] == 5);
|
||||
assert("seesaw".bestShuffle[1] == 0);
|
||||
assert("pop".bestShuffle[1] == 1);
|
||||
assert("up".bestShuffle[1] == 0);
|
||||
assert("a".bestShuffle[1] == 1);
|
||||
assert("".bestShuffle[1] == 0);
|
||||
}
|
||||
|
||||
unittest {
|
||||
assert(bestShuffle("abracadabra"d)[1] == 0);
|
||||
assert(bestShuffle("immediately"d)[1] == 0);
|
||||
assert(bestShuffle("grrrrrr"d)[1] == 5);
|
||||
assert(bestShuffle("seesaw"d)[1] == 0);
|
||||
assert(bestShuffle("pop"d)[1] == 1);
|
||||
assert(bestShuffle("up"d)[1] == 0);
|
||||
assert(bestShuffle("a"d)[1] == 1);
|
||||
assert(bestShuffle(""d)[1] == 0);
|
||||
}
|
||||
|
||||
void main(string[] args) {
|
||||
void main(in string[] args) {
|
||||
if (args.length > 1) {
|
||||
string entry = join(args[1 .. $], " ");
|
||||
auto res = bestShuffle(entry);
|
||||
writefln("%s : %s (%s)", entry, res[0], res[1]);
|
||||
immutable entry = args.dropOne.join(" ");
|
||||
const res = entry.bestShuffle;
|
||||
writefln("%s : %s (%d)", entry, res[]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
28
Task/Best-shuffle/Perl/best-shuffle-1.pl
Normal file
28
Task/Best-shuffle/Perl/best-shuffle-1.pl
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use List::Util qw(shuffle);
|
||||
use Algorithm::Permute;
|
||||
|
||||
best_shuffle($_) for qw(abracadabra seesaw elk grrrrrr up a);
|
||||
|
||||
sub best_shuffle {
|
||||
my ($original_word) = @_;
|
||||
my $best_word = $original_word;
|
||||
my $best_score = length $best_word;
|
||||
|
||||
my @shuffled = shuffle split //, $original_word;
|
||||
my $iterator = Algorithm::Permute->new(\@shuffled);
|
||||
|
||||
while( my @array = $iterator->next ) {
|
||||
my $word = join '', @array;
|
||||
# For each letter which is the same in the two words,
|
||||
# there will be a \x00 in the "^" of the two words.
|
||||
# The tr operator is then used to count the "\x00"s.
|
||||
my $score = ($original_word ^ $word) =~ tr/\x00//;
|
||||
next if $score >= $best_score;
|
||||
($best_word, $best_score) = ($word, $score);
|
||||
last if $score == 0;
|
||||
}
|
||||
|
||||
print "$original_word, $best_word, $best_score\n";
|
||||
}
|
||||
27
Task/Best-shuffle/Perl/best-shuffle-2.pl
Normal file
27
Task/Best-shuffle/Perl/best-shuffle-2.pl
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use List::Util qw(shuffle);
|
||||
|
||||
best_shuffle($_) for qw(abracadabra seesaw elk grrrrrr up a);
|
||||
|
||||
sub best_shuffle {
|
||||
my ($original_word) = @_;
|
||||
|
||||
my @s = split //, $original_word;
|
||||
my @t = shuffle @s;
|
||||
|
||||
for my $i ( 0 .. $#s ) {
|
||||
for my $j ( 0 .. $#s ) {
|
||||
next if $j == $i or
|
||||
$t[$i] eq $s[$j] or
|
||||
$t[$j] eq $s[$i];
|
||||
@t[$i,$j] = @t[$j,$i];
|
||||
last;
|
||||
}
|
||||
}
|
||||
|
||||
my $word = join '', @t;
|
||||
|
||||
my $score = ($original_word ^ $word) =~ tr/\x00//;
|
||||
print "$original_word, $word, $score\n";
|
||||
}
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
use strict;
|
||||
use Algorithm::Permute;
|
||||
|
||||
foreach ("abracadabra", "seesaw", "elk", "grrrrrr", "up", "a") {
|
||||
best_shuffle($_);
|
||||
}
|
||||
|
||||
sub score {
|
||||
my ($original_word,$new_word) = @_;
|
||||
my $result = 0;
|
||||
for (my $i = 0 ; $i < length($original_word) ; $i++) {
|
||||
if (substr($original_word,$i,1) eq substr($new_word,$i,1)) {
|
||||
$result++;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
sub best_shuffle {
|
||||
my ($original_word) = @_;
|
||||
my $best_word = $original_word;
|
||||
my $best_score = length($original_word);
|
||||
my @array = split(//,$original_word);
|
||||
|
||||
# The below was adapted from perlfaq4
|
||||
|
||||
my $p_iterator = Algorithm::Permute->new( \@array );
|
||||
|
||||
while (my @array = $p_iterator->next) {
|
||||
if (score($original_word,join("",@array))<$best_score) {
|
||||
$best_score = score($original_word, join("",@array));
|
||||
$best_word = join ("",@array);
|
||||
}
|
||||
last if ($best_score == 0);
|
||||
}
|
||||
|
||||
print "$original_word, $best_word, $best_score\n";
|
||||
}
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
from collections import Counter
|
||||
import random
|
||||
|
||||
def count(w1,wnew):
|
||||
|
|
@ -15,6 +14,7 @@ def best_shuffle(w):
|
|||
for j in rangej:
|
||||
if i != j and wnew[j] != wnew[i] and w[i] != wnew[j] and w[j] != wnew[i]:
|
||||
wnew[j], wnew[i] = wnew[i], wnew[j]
|
||||
break
|
||||
wnew = ''.join(wnew)
|
||||
return wnew, count(w, wnew)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue