Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
7
Task/Run-length-encoding/Perl/run-length-encoding-1.pl
Normal file
7
Task/Run-length-encoding/Perl/run-length-encoding-1.pl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
sub encode {
|
||||
shift =~ s/(.)\1*/length($&).$1/grse;
|
||||
}
|
||||
|
||||
sub decode {
|
||||
shift =~ s/(\d+)(.)/$2 x $1/grse;
|
||||
}
|
||||
7
Task/Run-length-encoding/Perl/run-length-encoding-2.pl
Normal file
7
Task/Run-length-encoding/Perl/run-length-encoding-2.pl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
sub encode {
|
||||
shift =~ s/(.)\1{0,254}/pack("C", length($&)).$1/grse;
|
||||
}
|
||||
|
||||
sub decode {
|
||||
shift =~ s/(.)(.)/$2 x unpack("C", $1)/grse;
|
||||
}
|
||||
32
Task/Run-length-encoding/Perl/run-length-encoding-3.pl
Normal file
32
Task/Run-length-encoding/Perl/run-length-encoding-3.pl
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
sub encode {
|
||||
my $str = shift;
|
||||
my $ret = "";
|
||||
my $nonrep = "";
|
||||
while ($str =~ m/(.)\1{0,127}|\z/gs) {
|
||||
my $len = length($&);
|
||||
if (length($nonrep) && (length($nonrep) == 127 || $len != 1)) {
|
||||
$ret .= pack("C", 128 + length($nonrep)) . $nonrep;
|
||||
$nonrep = "";
|
||||
}
|
||||
if ($len == 1) { $nonrep .= $1 }
|
||||
elsif ($len > 1) { $ret .= pack("C", $len) . $1 }
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
sub decode {
|
||||
my $str = shift;
|
||||
my $ret = "";
|
||||
for (my $i = 0; $i < length($str);) {
|
||||
my $len = unpack("C", substr($str, $i, 1));
|
||||
if ($len <= 128) {
|
||||
$ret .= substr($str, $i + 1, 1) x $len;
|
||||
$i += 2;
|
||||
}
|
||||
else {
|
||||
$ret .= substr($str, $i + 1, $len - 128);
|
||||
$i += 1 + $len - 128;
|
||||
}
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
4
Task/Run-length-encoding/Perl/run-length-encoding-4.pl
Normal file
4
Task/Run-length-encoding/Perl/run-length-encoding-4.pl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
use Data::Dump qw(dd);
|
||||
dd my $str = "XXXXXABCDEFGHIoooooooooooooooooooooooooAAAAAA";
|
||||
dd my $enc = encode($str);
|
||||
dd decode($enc);
|
||||
Loading…
Add table
Add a link
Reference in a new issue