29 lines
895 B
Python
29 lines
895 B
Python
from __future__ import print_function
|
|
from string import ascii_lowercase
|
|
|
|
SYMBOLTABLE = list(ascii_lowercase)
|
|
|
|
def move2front_encode(strng, symboltable):
|
|
sequence, pad = [], symboltable[::]
|
|
for char in strng:
|
|
indx = pad.index(char)
|
|
sequence.append(indx)
|
|
pad = [pad.pop(indx)] + pad
|
|
return sequence
|
|
|
|
def move2front_decode(sequence, symboltable):
|
|
chars, pad = [], symboltable[::]
|
|
for indx in sequence:
|
|
char = pad[indx]
|
|
chars.append(char)
|
|
pad = [pad.pop(indx)] + pad
|
|
return ''.join(chars)
|
|
|
|
if __name__ == '__main__':
|
|
for s in ['broood', 'bananaaa', 'hiphophiphop']:
|
|
encode = move2front_encode(s, SYMBOLTABLE)
|
|
print('%14r encodes to %r' % (s, encode), end=', ')
|
|
decode = move2front_decode(encode, SYMBOLTABLE)
|
|
print('which decodes back to %r' % decode)
|
|
assert s == decode, 'Whoops!'
|