31 lines
796 B
Text
31 lines
796 B
Text
program move_to_front;
|
|
tests := ["broood", "bananaaa", "hiphophiphop"];
|
|
alph := "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
loop for test in tests do
|
|
e := encode(test, alph);
|
|
d := decode(e, alph);
|
|
v := if test = d then "(ok)" else "(fail)" end;
|
|
print(test,"->",e,"->",d,v);
|
|
end loop;
|
|
|
|
proc encode(s, alph);
|
|
enc := [];
|
|
loop for c in s do
|
|
[i, j] := mark(alph, c);
|
|
alph := c + alph(..i-1) + alph(j+1..);
|
|
enc with:= i-1;
|
|
end loop;
|
|
return enc;
|
|
end proc;
|
|
|
|
proc decode(ls, alph);
|
|
s := "";
|
|
loop for i in ls do
|
|
c := alph(i + 1);
|
|
s +:= c;
|
|
alph := c + alph(..i) + alph(i+2..);
|
|
end loop;
|
|
return s;
|
|
end proc;
|
|
end program;
|