From 5baa569e5b335e3f87367b70ac51d4c68090fd6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Wed, 24 Jul 2024 09:03:32 -0700 Subject: [PATCH] Add run-length-encoding --- config.json | 8 +++ .../run-length-encoding/.docs/instructions.md | 20 ++++++ .../run-length-encoding/.meta/config.json | 19 +++++ .../run-length-encoding/.meta/example.coffee | 12 ++++ .../run-length-encoding/.meta/tests.toml | 49 +++++++++++++ .../run-length-encoding.coffee | 6 ++ .../run-length-encoding.spec.coffee | 70 +++++++++++++++++++ 7 files changed, 184 insertions(+) create mode 100644 exercises/practice/run-length-encoding/.docs/instructions.md create mode 100644 exercises/practice/run-length-encoding/.meta/config.json create mode 100644 exercises/practice/run-length-encoding/.meta/example.coffee create mode 100644 exercises/practice/run-length-encoding/.meta/tests.toml create mode 100644 exercises/practice/run-length-encoding/run-length-encoding.coffee create mode 100644 exercises/practice/run-length-encoding/run-length-encoding.spec.coffee diff --git a/config.json b/config.json index d91d72e..0cdd39e 100644 --- a/config.json +++ b/config.json @@ -589,6 +589,14 @@ "prerequisites": [], "difficulty": 5 }, + { + "slug": "run-length-encoding", + "name": "Run-Length Encoding", + "uuid": "b496f1b1-c04e-4d13-95bf-507bf3e616a7", + "practices": [], + "prerequisites": [], + "difficulty": 5 + }, { "slug": "spiral-matrix", "name": "Spiral Matrix", diff --git a/exercises/practice/run-length-encoding/.docs/instructions.md b/exercises/practice/run-length-encoding/.docs/instructions.md new file mode 100644 index 0000000..fc8ce05 --- /dev/null +++ b/exercises/practice/run-length-encoding/.docs/instructions.md @@ -0,0 +1,20 @@ +# Instructions + +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. + +```text +"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" -> "12WB12W3B24WB" +``` + +RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression. + +```text +"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. diff --git a/exercises/practice/run-length-encoding/.meta/config.json b/exercises/practice/run-length-encoding/.meta/config.json new file mode 100644 index 0000000..db702c6 --- /dev/null +++ b/exercises/practice/run-length-encoding/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "run-length-encoding.coffee" + ], + "test": [ + "run-length-encoding.spec.coffee" + ], + "example": [ + ".meta/example.coffee" + ] + }, + "blurb": "Implement run-length encoding and decoding.", + "source": "Wikipedia", + "source_url": "https://en.wikipedia.org/wiki/Run-length_encoding" +} diff --git a/exercises/practice/run-length-encoding/.meta/example.coffee b/exercises/practice/run-length-encoding/.meta/example.coffee new file mode 100644 index 0000000..71f070e --- /dev/null +++ b/exercises/practice/run-length-encoding/.meta/example.coffee @@ -0,0 +1,12 @@ +class RunLengthEncoding + @encode: (string) -> + consecutiveChars = /([\w\s])\1*/g + string.replace consecutiveChars, (match) -> + if match.length > 1 then match.length + match[0] else match[0] + + @decode: (string) -> + countAndChar = /(\d+)(\w|\s)/g + string.replace countAndChar, (_, count, char) -> + char.repeat count + +module.exports = RunLengthEncoding diff --git a/exercises/practice/run-length-encoding/.meta/tests.toml b/exercises/practice/run-length-encoding/.meta/tests.toml new file mode 100644 index 0000000..7bdb808 --- /dev/null +++ b/exercises/practice/run-length-encoding/.meta/tests.toml @@ -0,0 +1,49 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[ad53b61b-6ffc-422f-81a6-61f7df92a231] +description = "run-length encode a string -> empty string" + +[52012823-b7e6-4277-893c-5b96d42f82de] +description = "run-length encode a string -> single characters only are encoded without count" + +[b7868492-7e3a-415f-8da3-d88f51f80409] +description = "run-length encode a string -> string with no single characters" + +[859b822b-6e9f-44d6-9c46-6091ee6ae358] +description = "run-length encode a string -> single characters mixed with repeated characters" + +[1b34de62-e152-47be-bc88-469746df63b3] +description = "run-length encode a string -> multiple whitespace mixed in string" + +[abf176e2-3fbd-40ad-bb2f-2dd6d4df721a] +description = "run-length encode a string -> lowercase characters" + +[7ec5c390-f03c-4acf-ac29-5f65861cdeb5] +description = "run-length decode a string -> empty string" + +[ad23f455-1ac2-4b0e-87d0-b85b10696098] +description = "run-length decode a string -> single characters only" + +[21e37583-5a20-4a0e-826c-3dee2c375f54] +description = "run-length decode a string -> string with no single characters" + +[1389ad09-c3a8-4813-9324-99363fba429c] +description = "run-length decode a string -> single characters with repeated characters" + +[3f8e3c51-6aca-4670-b86c-a213bf4706b0] +description = "run-length decode a string -> multiple whitespace mixed in string" + +[29f721de-9aad-435f-ba37-7662df4fb551] +description = "run-length decode a string -> lowercase string" + +[2a762efd-8695-4e04-b0d6-9736899fbc16] +description = "encode and then decode -> encode followed by decode gives original string" diff --git a/exercises/practice/run-length-encoding/run-length-encoding.coffee b/exercises/practice/run-length-encoding/run-length-encoding.coffee new file mode 100644 index 0000000..7460ca8 --- /dev/null +++ b/exercises/practice/run-length-encoding/run-length-encoding.coffee @@ -0,0 +1,6 @@ +class RunLengthEncoding + @encode: (string) -> + + @decode: (string) -> + +module.exports = RunLengthEncoding diff --git a/exercises/practice/run-length-encoding/run-length-encoding.spec.coffee b/exercises/practice/run-length-encoding/run-length-encoding.spec.coffee new file mode 100644 index 0000000..6999d59 --- /dev/null +++ b/exercises/practice/run-length-encoding/run-length-encoding.spec.coffee @@ -0,0 +1,70 @@ +RLE = require './run-length-encoding' + +describe 'Run Length Encoding', -> + describe 'encode', -> + it 'empty string', -> + input = '' + expected = '' + expect(RLE.encode input).toEqual expected + + xit 'single characters only are encoded without count', -> + input = 'XYZ' + expected = 'XYZ' + expect(RLE.encode input).toEqual expected + + xit 'string with no single characters', -> + input = 'AABBBCCCC' + expected = '2A3B4C' + expect(RLE.encode input).toEqual expected + + xit 'string with single characters mixed with repeated characters', -> + input = 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB' + expected = '12WB12W3B24WB' + expect(RLE.encode input).toEqual expected + + xit 'string with multiple whitespace mixed', -> + input = ' hsqq qww ' + expected = '2 hs2q q2w2 ' + expect(RLE.encode input).toEqual expected + + xit 'string with lowercase characters', -> + input = 'aabbbcccc' + expected = '2a3b4c' + expect(RLE.encode input).toEqual expected + + describe 'decode', -> + xit 'empty string', -> + input = '' + expected = '' + expect(RLE.decode input).toEqual expected + + xit 'string with single characters only', -> + input = 'XYZ' + expected = 'XYZ' + expect(RLE.decode input).toEqual expected + + xit 'string with no single characters', -> + input = '2A3B4C' + expected = 'AABBBCCCC' + expect(RLE.decode input).toEqual expected + + xit 'string with single characters mixed with repeated characters', -> + input = '12WB12W3B24WB' + expected = 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB' + expect(RLE.decode input).toEqual expected + + xit 'string with multiple whitespaces', -> + input = '2 hs2q q2w2 ' + expected = ' hsqq qww ' + expect(RLE.decode input).toEqual expected + + xit 'string with lowercase characters', -> + input = '2a3b4c' + expected = 'aabbbcccc' + expect(RLE.decode input).toEqual expected + + xit 'encode followed by decode gives original string', -> + plainText = 'zzz ZZ zZ' + input = RLE.encode plainText + expected = 'zzz ZZ zZ' + expect(RLE.decode input).toEqual expected