(phixonline)-->
with javascript_semantics
function encode(string s)
string symtab = "abcdefghijklmnopqrstuvwxyz"
sequence res = {}
for i=1 to length(s) do
integer ch = s[i], k = find(ch,symtab)
res &= k-1
-- for j=k to 2 by -1 do
-- symtab[j] = symtab[j-1]
-- end for
-- symtab[1] = ch
symtab[1..k] = ch&symtab[1..k-1]
end for
return res
end function
function decode(sequence s)
string symtab = "abcdefghijklmnopqrstuvwxyz"
string res = ""
for i=1 to length(s) do
integer k = s[i]+1
integer ch = symtab[k]
res &= ch
-- for j=k to 2 by -1 do
-- symtab[j] = symtab[j-1]
-- end for
-- symtab[1] = ch
symtab[1..k] = ch&symtab[1..k-1]
end for
return res
end function
procedure test(string s)
sequence e = encode(s)
string d = decode(e)
?{s,e,d,{"**ERROR**","ok"}[(s=d)+1]}
end procedure
test("broood")
test("bananaaa")
test("hiphophiphop")