Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 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