Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
46
Task/Move-to-front-algorithm/Lua/move-to-front-algorithm.lua
Normal file
46
Task/Move-to-front-algorithm/Lua/move-to-front-algorithm.lua
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
-- Return table of the alphabet in lower case
|
||||
function getAlphabet ()
|
||||
local letters = {}
|
||||
for ascii = 97, 122 do table.insert(letters, string.char(ascii)) end
|
||||
return letters
|
||||
end
|
||||
|
||||
-- Move the table value at ind to the front of tab
|
||||
function moveToFront (tab, ind)
|
||||
local toMove = tab[ind]
|
||||
for i = ind - 1, 1, -1 do tab[i + 1] = tab[i] end
|
||||
tab[1] = toMove
|
||||
end
|
||||
|
||||
-- Perform move-to-front encoding on input
|
||||
function encode (input)
|
||||
local symbolTable, output, index = getAlphabet(), {}
|
||||
for pos = 1, #input do
|
||||
for k, v in pairs(symbolTable) do
|
||||
if v == input:sub(pos, pos) then index = k end
|
||||
end
|
||||
moveToFront(symbolTable, index)
|
||||
table.insert(output, index - 1)
|
||||
end
|
||||
return table.concat(output, " ")
|
||||
end
|
||||
|
||||
-- Perform move-to-front decoding on input
|
||||
function decode (input)
|
||||
local symbolTable, output = getAlphabet(), ""
|
||||
for num in input:gmatch("%d+") do
|
||||
output = output .. symbolTable[num + 1]
|
||||
moveToFront(symbolTable, num + 1)
|
||||
end
|
||||
return output
|
||||
end
|
||||
|
||||
-- Main procedure
|
||||
local testCases, output = {"broood", "bananaaa", "hiphophiphop"}
|
||||
for _, case in pairs(testCases) do
|
||||
output = encode(case)
|
||||
print("Original string: " .. case)
|
||||
print("Encoded: " .. output)
|
||||
print("Decoded: " .. decode(output))
|
||||
print()
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue