diff --git a/14_run_length_encoding.lua b/14_run_length_encoding.lua new file mode 100644 index 0000000..5402420 --- /dev/null +++ b/14_run_length_encoding.lua @@ -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 "#:" 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())