Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
26
Task/Move-to-front-algorithm/Perl/move-to-front-algorithm.pl
Normal file
26
Task/Move-to-front-algorithm/Perl/move-to-front-algorithm.pl
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
sub encode {
|
||||
my ($str) = @_;
|
||||
my $table = join '', 'a' .. 'z';
|
||||
map {
|
||||
$table =~ s/(.*?)$_/$_$1/ or die;
|
||||
length($1);
|
||||
} split //, $str;
|
||||
}
|
||||
|
||||
sub decode {
|
||||
my $table = join '', 'a' .. 'z';
|
||||
join "", map {
|
||||
$table =~ s/(.{$_})(.)/$2$1/ or die;
|
||||
$2;
|
||||
} @_;
|
||||
}
|
||||
|
||||
for my $test ( qw(broood bananaaa hiphophiphop) ) {
|
||||
my @encoded = encode($test);
|
||||
print "$test: @encoded\n";
|
||||
my $decoded = decode(@encoded);
|
||||
print "in" x ( $decoded ne $test );
|
||||
print "correctly decoded to $decoded\n";
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue