Anonymous user
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===
Programming note: the two REXX statements that add/subtract <big> '''one''' </big> deal with the task's requirement that the symbol table be ''zero-indexed'' (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*/
@=
$= /*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*/
@=
!= /*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:
word: bananaaa encoding:
word: hiphophiphop encoding:
</pre>
|