2015-02-20 09:02:09 -05:00
|
|
|
sub encode ( Str $word ) {
|
|
|
|
|
my @sym = 'a' .. 'z';
|
|
|
|
|
gather for $word.comb -> $c {
|
|
|
|
|
die "Symbol '$c' not found in @sym" if $c eq @sym.none;
|
2015-11-18 06:14:39 +00:00
|
|
|
@sym[0 .. take (@sym ... $c).end] .= rotate(-1);
|
2015-02-20 09:02:09 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub decode ( @enc ) {
|
|
|
|
|
my @sym = 'a' .. 'z';
|
|
|
|
|
[~] gather for @enc -> $pos {
|
|
|
|
|
take @sym[$pos];
|
|
|
|
|
@sym[0..$pos] .= rotate(-1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use Test;
|
|
|
|
|
plan 3;
|
|
|
|
|
for <broood bananaaa hiphophiphop> -> $word {
|
|
|
|
|
my $enc = encode($word);
|
|
|
|
|
my $dec = decode($enc);
|
|
|
|
|
is $word, $dec, "$word.fmt('%-12s') ($enc[])";
|
|
|
|
|
}
|