Create run length encoding exercise
This commit is contained in:
parent
01b22927cf
commit
fda74621cb
28
14_run_length_encoding.lua
Normal file
28
14_run_length_encoding.lua
Normal file
@ -0,0 +1,28 @@
|
||||
-- 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())
|
Loading…
x
Reference in New Issue
Block a user