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
local function bintohex(s)
return (s:gsub('(.)', function(c)
return string.format('%02x', string.byte(c))
end))
end
function encode(str)
gb = {str:byte(1,-1)}
for i = 1, #gb do
gb[i] = (gb[i] - 100) % 256
end
return "{"..table.concat(gb, ",").."}"
end
local function stringtohex (str)
return (string.gsub(str, '.', function (c)
return string.format('%02X', string.byte(c))
end))
end
local data = gg.prompt({"Add Script"},{"/sdcard/Enc"},{"file"})[1]
if data == nil then return end
io.open(data, "r"):read("*a")
-----local source = io.read("*a")
----io.close()
local menu = gg.choice({
"🔢 Function Enc",
"🔠 String to Hex",
"🔢 Bin To Hex ",
}, nil, '')
local func = menu == 1 and encode or menu == 2 and stringtohex or menu == 3 and bintohex or os.exit()
io.open(data..".🗂Hex.lua","w"):write(func(data)):close()
return (s:gsub('(.)', function(c)
return string.format('%02x', string.byte(c))
end))
end
function encode(str)
gb = {str:byte(1,-1)}
for i = 1, #gb do
gb[i] = (gb[i] - 100) % 256
end
return "{"..table.concat(gb, ",").."}"
end
local function stringtohex (str)
return (string.gsub(str, '.', function (c)
return string.format('%02X', string.byte(c))
end))
end
local data = gg.prompt({"Add Script"},{"/sdcard/Enc"},{"file"})[1]
if data == nil then return end
io.open(data, "r"):read("*a")
-----local source = io.read("*a")
----io.close()
local menu = gg.choice({
"🔢 Function Enc",
"🔠 String to Hex",
"🔢 Bin To Hex ",
}, nil, '')
local func = menu == 1 and encode or menu == 2 and stringtohex or menu == 3 and bintohex or os.exit()
io.open(data..".🗂Hex.lua","w"):write(func(data)):close()
Forwarded from Channel 🇮🇩 (XA404 [Pensi] 2020,06,12 - 2021,01,08)
China V4 Encryption.lua
2.9 KB
Join for more fak : @OfficialEncryptLuaGG