How to resolve the algorithm LZW compression step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm LZW compression step by step in the J 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 J programming language

Source code in the j programming language

encodeLZW =: 4 : 0
 d=. ;/x
 r=.0$0
 wc=.w=.{.y
 for_c. }.y do.
   wc=.w,c
   if. d e.~ <wc do. w=.wc else.
     r=. r, d i.<w
     d=.d,<wc
     w=.c
   end.
 end.
 r, d i.<w
)


decodeLZW =: 4 : 0
 d=.;/x
 w=.r=. >d{~{.y
 ds=. #d
 for_c. }.y do.
   select. * c-ds
    case. _1 do. r=.r,e=.>c{d  
    case.  0 do. r=.r,e=.w,{.w
    case.    do. 'error' return.
   end.
   d=.d,< w,{.e
   w=.e
   ds=.ds+1
 end.
 ;r
)


  

You may also check:How to resolve the algorithm Iterated digits squaring step by step in the Oforth programming language
You may also check:How to resolve the algorithm Thue-Morse step by step in the XLISP programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the C programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the Agena programming language
You may also check:How to resolve the algorithm Disarium numbers step by step in the BQN programming language