How to resolve the algorithm Move-to-front algorithm step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Move-to-front algorithm step by step in the zkl programming language

Table of Contents

Problem Statement

Given a symbol table of a zero-indexed array of all possible input symbols this algorithm reversibly transforms a sequence of input symbols into an array of output numbers (indices). The transform in many cases acts to give frequently repeated input symbols lower indices which is useful in some compression algorithms.

Encoding the string of character symbols 'broood' using a symbol table of the lowercase characters   a-to-z

Decoding the indices back to the original symbol order:

The strings are:

(Note the misspellings in the above strings.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Move-to-front algorithm step by step in the zkl programming language

Source code in the zkl programming language

fcn encode(text){ //-->List
   st:=["a".."z"].pump(Data);	//"abcd..z" as byte array
   text.reduce(fcn(st,c,sink){
      n:=st.index(c); sink.write(n); st.del(n).insert(0,c); },st,sink:=L());
   sink;
}

fcn decode(list){ //-->String
   st:=["a".."z"].pump(String);		//"abcd..z"
   sink:=Sink(String);
   list.reduce('wrap(st,n){ c:=st[n]; sink.write(c); c+st.del(n); },st);
   sink.close();
}

texts:=T("broood","bananaaa","hiphophiphop");
out:=texts.apply(encode);
texts.zipWith(fcn(t,e){ println(t,"-->",e) },out);

out.apply(decode).println();
texts.zipWith('==,out.apply(decode)).println();

  

You may also check:How to resolve the algorithm Symmetric difference step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm String matching step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the AWK programming language
You may also check:How to resolve the algorithm Empty program step by step in the Oberon-2 programming language
You may also check:How to resolve the algorithm Elementary cellular automaton step by step in the EchoLisp programming language