How to resolve the algorithm LZW compression step by step in the Seed7 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm LZW compression step by step in the Seed7 programming language
Table of Contents
Problem Statement
The Lempel-Ziv-Welch (LZW) algorithm provides loss-less data compression. You can read a complete description of it in the Wikipedia article on the subject. It was patented, but it entered the public domain in 2004.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm LZW compression step by step in the Seed7 programming language
Source code in the seed7 programming language
$ include "seed7_05.s7i";
const func string: lzwCompress (in string: uncompressed) is func
result
var string: compressed is "";
local
var char: ch is ' ';
var hash [string] char: mydict is (hash [string] char).value;
var string: buffer is "";
var string: xstr is "";
begin
for ch range chr(0) to chr(255) do
mydict @:= [str(ch)] ch;
end for;
for ch range uncompressed do
xstr := buffer & str(ch);
if xstr in mydict then
buffer &:= str(ch)
else
compressed &:= str(mydict[buffer]);
mydict @:= [xstr] chr(length(mydict));
buffer := str(ch);
end if;
end for;
if buffer <> "" then
compressed &:= str(mydict[buffer]);
end if;
end func;
const func string: lzwDecompress (in string: compressed) is func
result
var string: uncompressed is "";
local
var char: ch is ' ';
var hash [char] string: mydict is (hash [char] string).value;
var string: buffer is "";
var string: current is "";
var string: chain is "";
begin
for ch range chr(0) to chr(255) do
mydict @:= [ch] str(ch);
end for;
for ch range compressed do
if buffer = "" then
buffer := mydict[ch];
uncompressed &:= buffer;
elsif ch <= chr(255) then
current := mydict[ch];
uncompressed &:= current;
chain := buffer & current;
mydict @:= [chr(length(mydict))] chain;
buffer := current;
else
if ch in mydict then
chain := mydict[ch];
else
chain := buffer & str(buffer[1]);
end if;
uncompressed &:= chain;
mydict @:= [chr(length(mydict))] buffer & str(chain[1]);
buffer := chain;
end if;
end for;
end func;
const proc: main is func
local
var string: compressed is "";
var string: uncompressed is "";
begin
compressed := lzwCompress("TOBEORNOTTOBEORTOBEORNOT");
writeln(literal(compressed));
uncompressed := lzwDecompress(compressed);
writeln(uncompressed);
end func;
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the Julia programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Smarandache prime-digital sequence step by step in the C programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Align columns step by step in the Raku programming language