tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
13
Task/Run-length-encoding/Perl/run-length-encoding-1.pl
Normal file
13
Task/Run-length-encoding/Perl/run-length-encoding-1.pl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# functional approach (return the encoded or decoded string)
|
||||
sub encode {
|
||||
(my $str = shift) =~ s {(.)(\1*)} {length($&).$1}gse;
|
||||
return $str; }
|
||||
sub decode {
|
||||
(my $str = shift) =~ s {(\d+)(.)} {$2 x $1}gse;
|
||||
return $str;}
|
||||
|
||||
# procedural approach (modify the argument in place)
|
||||
sub encode {
|
||||
$_[0] =~ s {(.)(\1*)} {length($&).$1}gse; }
|
||||
sub decode {
|
||||
$_[0] =~ s {(\d+)(.)} {$2 x $1}gse; }
|
||||
14
Task/Run-length-encoding/Perl/run-length-encoding-2.pl
Normal file
14
Task/Run-length-encoding/Perl/run-length-encoding-2.pl
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
sub encode
|
||||
{my $str = shift;
|
||||
$str =~ s {(.)(\1{0,254})} {pack("C",(length($2) + 1)) . $1 }gse;
|
||||
return $str;}
|
||||
|
||||
sub decode
|
||||
{
|
||||
my @str = split //, shift;
|
||||
my $r = "";
|
||||
foreach my $i (0 .. scalar(@str)/2-1) {
|
||||
$r .= $str[2*$i + 1] x unpack("C", $str[2*$i]);
|
||||
}
|
||||
return $r;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue