Jump to content

Move-to-front algorithm: Difference between revisions

Added Easylang
m (→‎{{header|Wren}}: Minor tidy)
(Added Easylang)
 
Line 1,041:
'bananaaa' encodes to [1, 1, 13, 1, 1, 1, 0, 0], which decodes back to 'bananaaa'
'hiphophiphop' encodes to [7, 8, 15, 2, 15, 2, 2, 3, 2, 2, 3, 2], which decodes back to 'hiphophiphop'</pre>
 
=={{header|EasyLang}}==
{{trans|Ring}}
<syntaxhighlight>
subr init
symt$[] = strchars "abcdefghijklmnopqrstuvwxyz"
.
proc rot k . .
c$ = symt$[k]
for j = k downto 2
symt$[j] = symt$[j - 1]
.
symt$[1] = c$
.
func[] encode s$ .
init
for c$ in strchars s$
k = 1
while symt$[k] <> c$
k += 1
.
res[] &= k - 1
rot k
.
return res[]
.
func$ decode s[] .
init
for k in s[]
k += 1
c$ = symt$[k]
res$ &= c$
rot k
.
return res$
.
for word$ in [ "broood" "babanaaa" "hiphophiphop" ]
enc[] = encode word$
print word$ & " -> " & enc[]
if decode enc[] <> word$
print "error"
.
.
</syntaxhighlight>
{{out}}
<pre>
broood -> [ 1 17 15 0 0 5 ]
babanaaa -> [ 1 1 1 1 13 1 0 0 ]
hiphophiphop -> [ 7 8 15 2 15 2 2 3 2 2 3 2 ]
</pre>
 
=={{header|Elixir}}==
2,041

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.