Avatar of JaeHyoLee

JaeHyoLee's solution

to Run Length Encoding in the Lua Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Implement run-length encoding and decoding.

Run-length encoding (RLE) is a simple form of data compression, where runs (consecutive data elements) are replaced by just one data value and count.

For example we can represent the original 53 characters with only 13.

"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"  ->  "12WB12W3B24WB"

RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression.

"AABCCCDEEEE"  ->  "2AB3CD4E"  ->  "AABCCCDEEEE"

For simplicity, you can assume that the unencoded string will only contain the letters A through Z (either lower or upper case) and whitespace. This way data to be encoded will never contain any numbers and numbers inside data to be decoded always represent the count for the following character.

Running the tests

To run the tests, run the command busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

Source

Wikipedia https://en.wikipedia.org/wiki/Run-length_encoding

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

run-length-encoding_spec.lua

local rle = require('run-length-encoding')

describe('run-length-encoding', function()
  it('should encode simple strings', function()
    assert.equal('2A3B4C', rle.encode('AABBBCCCC'))
  end)

  it('should encode strings with multi-digit run lengths', function()
    assert.equal('2A10B4C', rle.encode('AABBBBBBBBBBCCCC'))
  end)

  it('should decode simple strings', function()
    assert.equal('AABBBCCCC', rle.decode('2A3B4C'))
  end)

  it('should decode strings with multi-digit run lengths', function()
    assert.equal('AABBBBBBBBBBCCCC', rle.decode('2A10B4C'))
  end)

  it('should not encode characters with a run length of 1', function()
    assert.equal('2AB4CD', rle.encode('AABCCCCD'))
  end)

  it('should successfully decode characters with a run length of 1', function()
    assert.equal('AABCCCCD', rle.decode('2AB4CD'))
  end)

  it('should decode an encoded string back to the original string', function()
    assert.equal('zzz ZZ  zZ', rle.decode(rle.encode('zzz ZZ  zZ')))
  end)
end)
local function encode(raw)
  local encoded = ''
  local count = 1

  for i = 2, #raw + 1 do
    if raw:sub(i-1,i-1) == raw:sub(i,i) then
      count = count + 1
    else
      encoded = encoded .. (count == 1 and '' or tostring(count)).. raw:sub(i -1, i -1)
      count = 1
    end
  end
  return encoded
end

local function decode(encoded)
  local decoded = ''
  for i = 1, #encoded do
    if tonumber(encoded:sub(i,i)) == nil then decoded =  decoded .. encoded:sub(i,i)
    else
        decoded = decoded .. encoded:sub(i + 1,i + 1):rep(tonumber(encoded:sub(i,i)) - 1)
    end
  end

  return decoded
end

return {
  encode = encode,
  decode = decode
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?