lua-exercises/15_run_length_encoding.lua

29 lines
1.2 KiB
Lua

-- Run length encoding is a simple data compression technique where a
-- "run" of the same value is replaced with a count and a single copy.
-- In this exercise we will use the notation "#<count>:<value>" to
-- indicate such a run; however, this should only be put into the
-- output if it is strictly shorter than the string it is replacing.
-- The input will consist only of alphanumeric characters.
--
-- Solution --------------------------------------------------------------------
function run_length_encode(input)
-- Your implementation here
end
-- Tests -----------------------------------------------------------------------
local luaunit = require("luaunit.luaunit")
function test_encoding_of_aaaaaaaa22bbbbbbssssfffffff67eee66666asdfff()
local input = "aaaaaaaa22bbbbbbssssfffffff67eee66666asdfff"
local expected = "#8:a22#6:bssss#7:f67eee#5:6asdfff"
luaunit.assertEquals(run_length_encode(input), expected)
end
function test_encoding_of_wwwwwfffff1223566666666bbbbbbbbbbb78b99asasdfbnfasdafffffff99()
local input = "wwwwwfffff1223566666666bbbbbbbbbbb78b99asasdfbnfasdafffffff99"
local expected = "#5:w#5:f12235#8:6#11:b78b99asasdfbnfasda#7:f99"
luaunit.assertEquals(run_length_encode(input), expected)
end
os.exit(luaunit.LuaUnit.run())