Fix = gg.prompt({"Select File To Fix","Remove os.exit"},{"/sdcard/FixTool"},{"file","checkbox"})
if Fix == nil then return end
Lua = io.open(Fix[1],"r"):read("*a")
if Fix[2] then
S = Lua:gsub("exit","time")
io.open(Fix[1].."Fix.lua","w"):write(string.dump(load(S),true)):close()
end
--- Make Code ( Remove os.exit() ) 😆
if Fix == nil then return end
Lua = io.open(Fix[1],"r"):read("*a")
if Fix[2] then
S = Lua:gsub("exit","time")
io.open(Fix[1].."Fix.lua","w"):write(string.dump(load(S),true)):close()
end
--- Make Code ( Remove os.exit() ) 😆
https://exercism.io/tracks/lua/exercises/run-length-encoding/solutions/1bd86fef3c9a42cab0e967bc635655d9
-- clever community solution: use
--
------ Encoding
local rle = {}
function rle.encode(decoded)
for character in decoded:gmatch(".") do
decoded = decoded:gsub(character:rep(2) .. "+", function(match)
return tostring(#match) .. character
end)
end
return decoded
end
------ Decoding
function rle.decode(encoded)
return encoded:gsub("(%d+)(.)", function(length, character)
return character:rep(length)
end)
end
-- clever community solution: use
gsub for runs of 2 or more--
------ Encoding
local rle = {}
function rle.encode(decoded)
for character in decoded:gmatch(".") do
decoded = decoded:gsub(character:rep(2) .. "+", function(match)
return tostring(#match) .. character
end)
end
return decoded
end
------ Decoding
function rle.decode(encoded)
return encoded:gsub("(%d+)(.)", function(length, character)
return character:rep(length)
end)
end
Exercism
aweidner's solution to Run Length Encoding on the Lua track | Exercism
See how aweidner solved the Run Length Encoding exercise on the Lua track
local function encode(txt)
local pos =1
local result = ''
while pos <= #txt do
local nxt = string.find(txt, '[^' .. string.sub(txt,pos,pos) .. ']', pos) or #txt+1
result = result .. (((nxt - pos) > 1) and (nxt -pos) or '') .. string.sub(txt, pos, pos)
pos = nxt
end
return result
end
local function decode(txt)
local result = ''
for d,w in string.gmatch(txt, '(%d*)(.)') do
if d =='' then d =1 end
result = result .. string.rep(w,d)
end
return result
end
local pos =1
local result = ''
while pos <= #txt do
local nxt = string.find(txt, '[^' .. string.sub(txt,pos,pos) .. ']', pos) or #txt+1
result = result .. (((nxt - pos) > 1) and (nxt -pos) or '') .. string.sub(txt, pos, pos)
pos = nxt
end
return result
end
local function decode(txt)
local result = ''
for d,w in string.gmatch(txt, '(%d*)(.)') do
if d =='' then d =1 end
result = result .. string.rep(w,d)
end
return result
end
------- Encoding
function encode(str)
local encoding = ""
local counter = 1
local flags = {}
for i = 1, #str do
local char = str:sub(i, i)
local nextChar = str:sub(i + 1, i + 1)
if char == nextChar then
counter = counter + 1
else
local n = counter > 1 and tostring(counter) or ""
encoding = encoding .. n .. char
counter = 1
end
end
return encoding
end
------- Decoded
function decode(str)
local decoded = ""
local flag = 1
for i = 1, #str do
if not tonumber(str:sub(i, i)) then
local temp = str:sub(flag, i)
if #temp > 1 then
local num = temp:sub(1, #temp - 1)
local char = temp:sub(#temp, #temp)
decoded = decoded .. char:rep(tonumber(num))
else
decoded = decoded .. temp
end
flag = i + 1
end
end
return decoded
end
function encode(str)
local encoding = ""
local counter = 1
local flags = {}
for i = 1, #str do
local char = str:sub(i, i)
local nextChar = str:sub(i + 1, i + 1)
if char == nextChar then
counter = counter + 1
else
local n = counter > 1 and tostring(counter) or ""
encoding = encoding .. n .. char
counter = 1
end
end
return encoding
end
------- Decoded
function decode(str)
local decoded = ""
local flag = 1
for i = 1, #str do
if not tonumber(str:sub(i, i)) then
local temp = str:sub(flag, i)
if #temp > 1 then
local num = temp:sub(1, #temp - 1)
local char = temp:sub(#temp, #temp)
decoded = decoded .. char:rep(tonumber(num))
else
decoded = decoded .. temp
end
flag = i + 1
end
end
return decoded
end