39 lines
884 B
Plaintext
39 lines
884 B
Plaintext
function encode(string s)
|
|
string symtab = "abcdefghijklmnopqrstuvwxyz"
|
|
sequence res = {}
|
|
for i=1 to length(s) do
|
|
integer ch = s[i]
|
|
integer 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
|
|
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
|
|
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")
|