19
1
Background
The move-to-front transform (MTF) is a data encoding algorithm designed to improve the performance of entropy encoding techniques.
In the bzip2 compression algorithm, it is applied after the Burrows–Wheeler transform (as seen in Burrows, Wheeler and Back), with the objective of turning groups of repeated characters into small, easily compressible non-negative integers.
Definition
For the purpose of this challenge, we'll define the printable ASCII version of the MTF as follows:
Given an input string s, take an empty array r, the string d of all printable ASCII characters (0x20 to 0x7E) and repeat the following for each character c of s:
Append the index of c in d to r.
Move c to the front of d, i.e., remove c from d and prepend it to the remainder.
Finally, we take the elements of r as indexes in the original d and fetch the corresponding characters.
Step-by-step example
INPUT: "CODEGOLF"
0. s = "CODEGOLF"
d = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
r = []
1. s = "ODEGOLF"
d = "C !\"#$%&'()*+,-./0123456789:;<=>?@ABDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
r = [35]
2. s = "DEGOLF"
d = "OC !\"#$%&'()*+,-./0123456789:;<=>?@ABDEFGHIJKLMNPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
r = [35 47]
3. s = "EGOLF"
d = "DOC !\"#$%&'()*+,-./0123456789:;<=>?@ABEFGHIJKLMNPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
r = [35 47 37]
4. s = "GOLF"
d = "EDOC !\"#$%&'()*+,-./0123456789:;<=>?@ABFGHIJKLMNPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
r = [35 47 37 38]
5. s = "OLF"
d = "GEDOC !\"#$%&'()*+,-./0123456789:;<=>?@ABFHIJKLMNPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
r = [35 47 37 38 40]
6. s = "LF"
d = "OGEDC !\"#$%&'()*+,-./0123456789:;<=>?@ABFHIJKLMNPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
r = [35 47 37 38 40 3]
7. s = "F"
d = "LOGEDC !\"#$%&'()*+,-./0123456789:;<=>?@ABFHIJKMNPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
r = [35 47 37 38 40 3 45]
8. s = ""
d = "FLOGEDC !\"#$%&'()*+,-./0123456789:;<=>?@ABHIJKMNPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
r = [35 47 37 38 40 3 45 41]
OUTPUT: "COEFH#MI"
Task
Write a program or function that implements the printable ASCII MTF (as defined above).
Test cases
Input: Programming Puzzles & Code Golf
Output: Prpi"do lp%((uz rnu&3!P/o&$U$(p
Input: NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN BATMAN!
Output: Na! !! !! !! !! !! !! !! !! !! !! !! !! !! !! !!"DDUP"%'
Input: Two more questions and I have bzip2 in less than 100 bytes!
Output: Twp#o"si$sv#uvq(u$(l#o#W!r%w+$pz,xF%#,"x(. #0--'$GG ".z(**:
Additional rules
You cannot use any built-in operator that computes the MTF of a string.
Your code may print a trailing newline if you choose STDOUT for output.
Your code has to work for any input of 1000 or less printable ASCII characters (0x20 to 0x7E).
Standard code golf rules apply. The shortest submission in bytes wins.
1"Nanananana DDUP!" just isn't as catchy as "Batman!"... – Doorknob – 2015-07-03T22:12:34.527
8@Doorknob: But Batman isn't easily compressible. – Dennis – 2015-07-03T22:13:56.460
Can we output the result in a function return instead of printing it to STDOUT? – Fatalize – 2015-07-03T22:39:54.453
@Fatalize: That's the most natural form of output for functions, so yes. By the way, we have defaults for I/O, so unless the question explicitly says otherwise, that's always allowed.
– Dennis – 2015-07-03T22:43:09.760