aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-08-23 09:17:04 +0100
committerGitHub <noreply@github.com>2019-08-23 09:17:04 +0100
commit44c3242ab4c762442d95fe5c722174ed78b64421 (patch)
tree1df328800b3a063e491d708f0e867e42d301a0d2
parentfbc388b27d2ccaa17e167429a597a9d0313b5474 (diff)
parente8f5f22117482c7ee8fd035a6b9d3e3d20bb1acc (diff)
downloadperlweeklychallenge-club-44c3242ab4c762442d95fe5c722174ed78b64421.tar.gz
perlweeklychallenge-club-44c3242ab4c762442d95fe5c722174ed78b64421.tar.bz2
perlweeklychallenge-club-44c3242ab4c762442d95fe5c722174ed78b64421.zip
Merge pull request #543 from Doomtrain14/master
compressed data now saved to file in perl5 ch2
-rw-r--r--challenge-022/yet-ebreo/perl5/ch-2.pl59
1 files changed, 50 insertions, 9 deletions
diff --git a/challenge-022/yet-ebreo/perl5/ch-2.pl b/challenge-022/yet-ebreo/perl5/ch-2.pl
index 85ecc2925b..e2af41a6f0 100644
--- a/challenge-022/yet-ebreo/perl5/ch-2.pl
+++ b/challenge-022/yet-ebreo/perl5/ch-2.pl
@@ -5,7 +5,7 @@
use strict;
use bytes;
use constant INIT_DICT_SIZE => 255;
-
+use constant LOG2 => log(2);
sub encode {
my $string = pop;
my $len = length $string;
@@ -37,11 +37,42 @@ sub encode {
$curr = "";
}
push @buff_out, $dictionary{$prev};
- return @buff_out;
+
+ #output as string
+ my $bin_string = "";
+ my $chr_string = "";
+ my $dict_index = INIT_DICT_SIZE;
+ for my $bytes (@buff_out) {
+ my $width = 0|1+log(++$dict_index)/LOG2;
+ $bin_string .= sprintf "%0${width}b",$bytes;
+
+ }
+
+ while ($bin_string=~s/.{8}//) {
+ $chr_string .= chr oct "b$&";
+ }
+
+ $chr_string .= chr oct "b".$bin_string.(0 x (8 - length $bin_string));
+ return $chr_string;
}
sub decode {
- my @buff_in = @{+pop};
+ my $chr_string = pop;
+ my $bin_string = "";
+ my @buff_in = ();
+
+ #decode the string passed
+ for (0..~-length $chr_string) {
+ $bin_string .= sprintf "%08b", ord (substr $chr_string, $_,1)
+ }
+
+ my $dict_index = INIT_DICT_SIZE+1;
+ my $width = 9;
+ while ($bin_string=~s/.{$width}//) {
+ push @buff_in, oct "b$&";
+ $width = 0|1+log(++$dict_index)/LOG2;
+ }
+ $buff_in[-1]+= oct "b$bin_string";
#Initialize dictionary using numbers as keys
#Using array would work too
@@ -54,7 +85,6 @@ sub decode {
my $c = substr $s, 0, 1;
my $ret = $s;
-
my $count = INIT_DICT_SIZE;
#Go through the codes in @buff_in
@@ -89,12 +119,23 @@ my $string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ei
print ("Original String has length ". (length $string)."\n");
print "$string\n\n";
-my @compressed = encode($string);
-print ("Compressed String has length ". (@compressed)."\n");
-print (("@compressed "=~s/\d+ /chr $&/gre)."\n\n");
+my $filename = "./compressed.z";
+my $fh;
+open( $fh, '>', $filename);
+print $fh encode($string);
+close $fh;
-my $uncompressed = decode(\@compressed);
+my $comp_cfile_size = -s $filename;
+my $comp_ratio = (length ($string)/ $comp_cfile_size) * 100;
+
+printf "Compresed file size: ".(-s $filename)." Bytes (Ratio: %0.2f %)\n\n", $comp_ratio;
+
+open( $fh, '<', $filename);
+my $compressed = do { local $/; <$fh> };
+close $fh;
+
+my $uncompressed = decode($compressed);
print ("Uncompressed String has length ". (length $uncompressed)."\n");
print "$uncompressed\n\n";
-
+#
print "Uncompressed string matches original\n\n" if $uncompressed eq $string;