How to resolve the algorithm LZW compression step by step in the zkl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm LZW compression step by step in the zkl 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 zkl programming language
Source code in the zkl programming language
fcn lzwCompress(uncompressed){ // text-->list of 12 bit ints
dictionary:=(256).pump(Dictionary(),fcn(n){ return(n.toChar(),n) });
w,compressed:="",List();
foreach c in (uncompressed){
wc:=w+c;
if(dictionary.holds(wc)) w=wc;
else{
compressed.append(dictionary[w]); // 12 bits
dictionary[wc]=dictionary.len();
w=c;
}
}
if(w) compressed.append(dictionary[w]);
compressed
}
fcn lzwUncompress(compressed){ // compressed data-->text
dictionary:=(256).pump(Dictionary(),fcn(n){ return(n,n.toChar()) });
w,decommpressed:=dictionary[compressed[0]],Data(Void,w);
foreach k in (compressed[1,*]){
if(dictionary.holds(k)) entry:=dictionary[k];
else if(k==dictionary.len()) entry:=w+w[0];
else throw(Exception.ValueError("Invalid compressed data"));
decommpressed.append(entry);
dictionary.add(dictionary.len(),w+entry[0]);
w=entry;
}
decommpressed.text
}
compressed:=lzwCompress("TOBEORNOTTOBEORTOBEORNOT");
compressed.toString(*).println();
lzwUncompress(compressed).println();
You may also check:How to resolve the algorithm Intersecting number wheels step by step in the Python programming language
You may also check:How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Topswops step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Faces from a mesh step by step in the Python programming language
You may also check:How to resolve the algorithm Calculating the value of e step by step in the K programming language