Skip to content
DeepForgeby svx
All problems
MediumInformation Theoryinfo-283

Move-to-Front Rank

Problem

Apply move-to-front coding to a sequence using a given initial alphabet order. For each symbol, output its current index in the list and then move it to the front. Assume every symbol in the sequence appears in the alphabet and that alphabet entries are distinct. Return the list of ranks.

Hint

Frequently used symbols drift toward rank 0, making later ranks cheap.

Test cases

case 1
in: [[1,1,0],[0,1]]
expected: [1,0,1]
case 2
in: [[0,1,2,2],[0,1,2]]
expected: [0,1,2,0]
case 3
in: [[],[0,1]]
expected: []
case 4
in: [["b","a","b"],["a","b"]]
expected: [1,1,1]
Starter codePython
def mtf_ranks(seq, alphabet):
    # Your code here
    pass

4 test cases · run your code against them in the editor