diff options
| author | dcw <d.white@imperial.ac.uk> | 2019-08-28 15:37:09 +0100 |
|---|---|---|
| committer | dcw <d.white@imperial.ac.uk> | 2019-08-28 15:37:09 +0100 |
| commit | 7902278bfd0d5e9e3ba5fbb6f6bce69cec4832f7 (patch) | |
| tree | b1b7dbba9964ab21d695d32c6b5f3d4105fd1b9a | |
| parent | 968a2011df76057b4b231cd59e188060bde0f042 (diff) | |
| download | perlweeklychallenge-club-7902278bfd0d5e9e3ba5fbb6f6bce69cec4832f7.tar.gz perlweeklychallenge-club-7902278bfd0d5e9e3ba5fbb6f6bce69cec4832f7.tar.bz2 perlweeklychallenge-club-7902278bfd0d5e9e3ba5fbb6f6bce69cec4832f7.zip | |
minor fixes to lzw_decode to increase clarity; tweaked to output format in testlzw
| -rw-r--r-- | challenge-022/duncan-c-white/perl5/LZW.pm | 30 | ||||
| -rwxr-xr-x | challenge-022/duncan-c-white/perl5/testlzw.pl | 4 |
2 files changed, 12 insertions, 22 deletions
diff --git a/challenge-022/duncan-c-white/perl5/LZW.pm b/challenge-022/duncan-c-white/perl5/LZW.pm index 5dac1ca36e..b2ff41a4e0 100644 --- a/challenge-022/duncan-c-white/perl5/LZW.pm +++ b/challenge-022/duncan-c-white/perl5/LZW.pm @@ -180,11 +180,9 @@ fun lzw_decode( $binstr ) my $ndict = @newdict; print "decoding first $b, pos=$pos, ndict=$ndict\n" if $debug; - die "LZW_decode: first bin $b (pos $pos) not in dict, $ndict entries\n" unless $pos<$ndict; my $prevf = $newdict[$pos]; # previous text fragment - my $result = $prevf; while( $binstr ) @@ -199,30 +197,22 @@ fun lzw_decode( $binstr ) my $ndict = @newdict; - my $f; # current decoded text fragment + die "decode: bad pos $pos (dictionary has $ndict ". + "entries) result=$result, b=$b, ". + "binstr=$binstr)\n" if $pos > $ndict; - # if pos in dict? - if( $pos < $ndict ) - { - $f = $newdict[$pos]; - } elsif( $pos == $ndict ) - { - $f = $prevf . substr($prevf,0,1); - } else - { - die "decode: bad pos $pos (dictionary has $ndict ". - "entries) result=$result, b=$b, ". - "binstr=$binstr)\n"; - } + my $f = $pos < $ndict ? # current decoded text fragment + $newdict[$pos] : + $prevf . substr($prevf,0,1); - # ok, so $b represents text frag $f + # ok, so encoded $b represents decoded text frag $f print "b $b, pos=$pos, prevf=$prevf, f=$f\n" if $debug; $result .= $f; - # the next entry in the dictionary must be the WHOLE of - # the previous fragment plus the FIRST letter of the - # current fragment $f + # the next entry in the dictionary is the WHOLE of + # the previous fragment $prevf, plus the FIRST letter + # of the current fragment $f my $new = $prevf . substr($f,0,1); $pos = @newdict; diff --git a/challenge-022/duncan-c-white/perl5/testlzw.pl b/challenge-022/duncan-c-white/perl5/testlzw.pl index efb86814fc..ad80a147bf 100755 --- a/challenge-022/duncan-c-white/perl5/testlzw.pl +++ b/challenge-022/duncan-c-white/perl5/testlzw.pl @@ -50,11 +50,11 @@ foreach (1..$n) my $text = make_random_string( $len ); my $binstr = lzw_encode( $text ); $text .= '#'; - print "\ntext: $text\n"; + print "text: $text\n"; #print " encodes to: $binstr\n"; my $text2 = lzw_decode( $binstr ); #print " which decodes to: $text2\n"; - print " encodes+decodes to: $text2\n" if 0; + #print " encodes+decodes to: $text2\n"; die "text $text -> encodes to $binstr -> decodes to $text2\n" unless $text eq $text2; } |
