Move-to-front algorithm: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
(added Arturo)
Line 547: Line 547:
hiphophiphop encodes to [ 7 8 15 2 15 2 2 3 2 2 3 2 ] which correctly decodes to "hiphophiphop"
hiphophiphop encodes to [ 7 8 15 2 15 2 2 3 2 2 3 2 ] which correctly decodes to "hiphophiphop"
</pre>
</pre>

=={{header|Arturo}}==
<syntaxhighlight lang="arturo">
symbolTable: @`a`..`z`

encodeWord: function [s][
symt: new symbolTable
result: []
loop s 'c [
idx: index symt c
'result ++ idx
symt: (rotate symt\[0..idx] 1) ++ symt\[(idx+1)..dec size symt]
]
return result
]

decodeWord: function [s][
symt: new symbolTable
result: []
loop s 'idx [
'result ++ symt\[idx]
symt: (rotate symt\[0..idx] 1) ++ symt\[(idx+1)..dec size symt]
]
return join result
]

loop ["broood", "babanaaa", "hiphophiphop"] 'word [
encoded: encodeWord word
decoded: decodeWord encoded
print ["'"++word++"'" "encodes to" encoded "which correctly decodes to" "'"++decoded++"'"]
]</syntaxhighlight>

{{out}}

<pre>'broood' encodes to [1 17 15 0 0 5] which correctly decodes to 'broood'
'babanaaa' encodes to [1 1 1 1 13 1 0 0] which correctly decodes to 'babanaaa'
'hiphophiphop' encodes to [7 8 15 2 15 2 2 3 2 2 3 2] which correctly decodes to 'hiphophiphop'</pre>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==