Jump to content

Move-to-front algorithm: Difference between revisions

Added 11l
m (added clarifying word.)
(Added 11l)
Line 108:
<small>(Note the misspellings in the above strings.)</small>
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>V symboltable = Array(‘a’..‘z’)
 
F move2front_encode(strng)
[Int] sequence
V pad = copy(:symboltable)
L(char) strng
V indx = pad.index(char)
sequence.append(indx)
pad = [pad.pop(indx)] [+] pad
R sequence
 
F move2front_decode(sequence)
[Char] chars
V pad = copy(:symboltable)
L(indx) sequence
V char = pad[indx]
chars.append(char)
pad = [pad.pop(indx)] [+] pad
R chars.join(‘’)
 
L(s) [‘broood’, ‘bananaaa’, ‘hiphophiphop’]
V encode = move2front_encode(s)
print(‘#14 encodes to #.’.format(s, encode), end' ‘, ’)
V decode = move2front_decode(encode)
print(‘which decodes back to #.’.format(decode))
assert(s == decode, ‘Whoops!’)</lang>
 
{{out}}
<pre>
broood encodes to [1, 17, 15, 0, 0, 5], which decodes back to broood
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|Ada}}==
1,481

edits

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