Move-to-front algorithm: Difference between revisions

m
→‎version 2: reverted to use of smaller symbol table.
(→‎version 2: expanded the symbol table to include all possible (input) characters (symbols).)
m (→‎version 2: reverted to use of smaller symbol table.)
Line 1,205:
 
===version 2===
This REXX version uses a fully-defined (all possible characters) symbol table, not just a lowercase alphabet.
 
Programming note: &nbsp; the two REXX statements that add/subtract &nbsp; <big> '''one''' </big>&nbsp; deal with the task's requirement that the symbol table be ''zero-indexed'' &nbsp; (the REXX language uses unity-based tables).
<lang rexx>/*REXX pgm demonstrates move─to─front algorithm encode/decode sym table.*/
Line 1,212 ⟶ 1,210:
one=1 /*for task's requirement. */
do j=1 for words(xxx); x=word(xxx,j) /*process one word at a time*/
@=xrange() 'abcdefghijklmnopqrstuvwxyz' /*symbolsym table: is alllower charsalphabet.*/
$= /*set decode string to null.*/
do k=1 for length(x); z=substr(x,k,1) /*encrypt a char in word. */
Line 1,219 ⟶ 1,217:
end /*k*/ /* [↑] move─to─front encode*/
 
@=xrange() 'abcdefghijklmnopqrstuvwxyz' /*symbolsym table: is alllower charsalphabet.*/
!= /*set encode string to null.*/
do m=1 for words($); n=word($,m)+one /*decode the sequence table.*/
Line 1,231 ⟶ 1,229:
'''output''' using the default input:
<pre>
word: broood encoding: 981 11417 11215 0 0 1025 OK
word: bananaaa encoding: 981 981 11013 1 1 1 0 0 OK
word: hiphophiphop encoding: 1047 1058 11215 2 11215 2 2 3 2 2 3 2 OK
</pre>