Advertisement
Selim_042

bwt

May 26th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.72 KB | None | 0 0
  1. local markChar = "\254"
  2.  
  3. function encode(s)
  4.     local s = s..markChar
  5.     local rotations = {}
  6.     for i=1,#s do
  7.         s = string.sub(s,#s)..string.sub(s,1,#s-1)
  8.         print(i..": "..string.sub(s,1,10))
  9.         rotations[i] = s
  10.     end
  11.     table.sort(rotations)
  12.     local returnValue = ""
  13.     for i=1,#rotations do
  14.         returnValue = returnValue..string.sub(rotations[i],#rotations[i])
  15.         coroutine.yield()
  16.     end
  17.     return returnValue
  18. end
  19.  
  20. function decode(s)
  21.     local t = {}
  22.     for k=1,#s do
  23.         for i=1,#s do
  24.             local char = string.sub(s,i,i)
  25.             t[i] = not t[i] and char or char .. t[i]
  26.             coroutine.yield()
  27.         end
  28.         table.sort(t)
  29.     end
  30.     for i=1,#t do
  31.         if (string.sub(t[i],#t[i]) == markChar) then
  32.             return string.sub(t[i],1,#t[i]-1)
  33.         end
  34.     end
  35. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement