diff --git a/config.json b/config.json index 5e5ef8d4..7ae6ee82 100644 --- a/config.json +++ b/config.json @@ -743,6 +743,14 @@ "practices": [], "prerequisites": [], "difficulty": 1 + }, + { + "slug": "run-length-encoding", + "name": "Run-Length Encoding", + "uuid": "8997f6f2-a8c3-42b5-94fc-12d0d386a64e", + "practices": [], + "prerequisites": [], + "difficulty": 1 } ] }, 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 00000000..fc8ce056 --- /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 00000000..c0fb5ec2 --- /dev/null +++ b/exercises/practice/run-length-encoding/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "habere-et-dispertire" + ], + "files": { + "solution": [ + "lib/RunLengthEncoding.rakumod" + ], + "test": [ + "t/run-length-encoding.rakutest" + ], + "example": [ + ".meta/solutions/lib/RunLengthEncoding.rakumod" + ] + }, + "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/solutions/lib/RunLengthEncoding.rakumod b/exercises/practice/run-length-encoding/.meta/solutions/lib/RunLengthEncoding.rakumod new file mode 100644 index 00000000..c40111e1 --- /dev/null +++ b/exercises/practice/run-length-encoding/.meta/solutions/lib/RunLengthEncoding.rakumod @@ -0,0 +1,26 @@ +unit module RunLengthEncoding; + +grammar RLE { + token TOP { + } + token pair { ? } + token tally { + } + token element { | ' ' } +} +class RLE::Decode { + method TOP ($/) { make $.map( *.made ).join } + method pair ($/) { make $.made x ( $.made // 1 ) } + method tally ($/) { make $/.Int } + method element ($/) { make $/.Str } +} +sub rle-decode ($compressed) is export { + RLE.parse( $compressed, actions => RLE::Decode ).made +} +sub rle-encode ($raw) is export { + given $raw.comb( / ( [+]? ) [ | ' ']+ % / ) + .map( { .chars == 1 ?? $_ !! .chars ~ .comb.head } ) + .join + -> $compressed { + fail unless $raw eq rle-decode($compressed); + return $compressed + } +} diff --git a/exercises/practice/run-length-encoding/.meta/solutions/t/run-length-encoding.rakutest b/exercises/practice/run-length-encoding/.meta/solutions/t/run-length-encoding.rakutest new file mode 120000 index 00000000..3511cfd2 --- /dev/null +++ b/exercises/practice/run-length-encoding/.meta/solutions/t/run-length-encoding.rakutest @@ -0,0 +1 @@ +../../../t/run-length-encoding.rakutest \ No newline at end of file diff --git a/exercises/practice/run-length-encoding/.meta/template-data.yaml b/exercises/practice/run-length-encoding/.meta/template-data.yaml new file mode 100644 index 00000000..b9ea9f9c --- /dev/null +++ b/exercises/practice/run-length-encoding/.meta/template-data.yaml @@ -0,0 +1,64 @@ +properties: + encode: + test: |- + sprintf(q :to 'END', %case.Str.raku, %case.Str.raku, %case.raku); + cmp-ok( + rle-encode(%s), + "eq", + %s, + %s, + ); + END + decode: + test: |- + sprintf(q :to 'END', %case.Str.raku, %case.Str.raku, %case.raku); + cmp-ok( + rle-decode(%s), + "eq", + %s, + %s, + ); + END + consistency: + test: |- + sprintf(q :to 'END', %case.Str.raku, %case.Str.raku, %case.raku); + cmp-ok( + rle-decode(rle-encode(%s)), + "eq", + %s, + %s, + ); + END + +unit: module +example: |- + grammar RLE { + token TOP { + } + token pair { ? } + token tally { + } + token element { | ' ' } + } + class RLE::Decode { + method TOP ($/) { make $.map( *.made ).join } + method pair ($/) { make $.made x ( $.made // 1 ) } + method tally ($/) { make $/.Int } + method element ($/) { make $/.Str } + } + sub rle-decode ($compressed) is export { + RLE.parse( $compressed, actions => RLE::Decode ).made + } + sub rle-encode ($raw) is export { + given $raw.comb( / ( [+]? ) [ | ' ']+ % / ) + .map( { .chars == 1 ?? $_ !! .chars ~ .comb.head } ) + .join + -> $compressed { + fail unless $raw eq rle-decode($compressed); + return $compressed + } + } + +stub: |- + sub rle-encode ($raw) is export { + } + sub rle-decode ($compressed) is export { + } 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 00000000..7bdb8086 --- /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/lib/RunLengthEncoding.rakumod b/exercises/practice/run-length-encoding/lib/RunLengthEncoding.rakumod new file mode 100644 index 00000000..d92da70d --- /dev/null +++ b/exercises/practice/run-length-encoding/lib/RunLengthEncoding.rakumod @@ -0,0 +1,6 @@ +unit module RunLengthEncoding; + +sub rle-encode ($raw) is export { +} +sub rle-decode ($compressed) is export { +} diff --git a/exercises/practice/run-length-encoding/t/run-length-encoding.rakutest b/exercises/practice/run-length-encoding/t/run-length-encoding.rakutest new file mode 100755 index 00000000..86b1576c --- /dev/null +++ b/exercises/practice/run-length-encoding/t/run-length-encoding.rakutest @@ -0,0 +1,97 @@ +#!/usr/bin/env raku +use Test; +use lib $?FILE.IO.parent(2).add('lib'); +use RunLengthEncoding; + +cmp-ok( # begin: ad53b61b-6ffc-422f-81a6-61f7df92a231 + rle-encode(""), + "eq", + "", + "run-length encode a string: empty string", +); # end: ad53b61b-6ffc-422f-81a6-61f7df92a231 + +cmp-ok( # begin: 52012823-b7e6-4277-893c-5b96d42f82de + rle-encode("XYZ"), + "eq", + "XYZ", + "run-length encode a string: single characters only are encoded without count", +); # end: 52012823-b7e6-4277-893c-5b96d42f82de + +cmp-ok( # begin: b7868492-7e3a-415f-8da3-d88f51f80409 + rle-encode("AABBBCCCC"), + "eq", + "2A3B4C", + "run-length encode a string: string with no single characters", +); # end: b7868492-7e3a-415f-8da3-d88f51f80409 + +cmp-ok( # begin: 859b822b-6e9f-44d6-9c46-6091ee6ae358 + rle-encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"), + "eq", + "12WB12W3B24WB", + "run-length encode a string: single characters mixed with repeated characters", +); # end: 859b822b-6e9f-44d6-9c46-6091ee6ae358 + +cmp-ok( # begin: 1b34de62-e152-47be-bc88-469746df63b3 + rle-encode(" hsqq qww "), + "eq", + "2 hs2q q2w2 ", + "run-length encode a string: multiple whitespace mixed in string", +); # end: 1b34de62-e152-47be-bc88-469746df63b3 + +cmp-ok( # begin: abf176e2-3fbd-40ad-bb2f-2dd6d4df721a + rle-encode("aabbbcccc"), + "eq", + "2a3b4c", + "run-length encode a string: lowercase characters", +); # end: abf176e2-3fbd-40ad-bb2f-2dd6d4df721a + +cmp-ok( # begin: 7ec5c390-f03c-4acf-ac29-5f65861cdeb5 + rle-decode(""), + "eq", + "", + "run-length decode a string: empty string", +); # end: 7ec5c390-f03c-4acf-ac29-5f65861cdeb5 + +cmp-ok( # begin: ad23f455-1ac2-4b0e-87d0-b85b10696098 + rle-decode("XYZ"), + "eq", + "XYZ", + "run-length decode a string: single characters only", +); # end: ad23f455-1ac2-4b0e-87d0-b85b10696098 + +cmp-ok( # begin: 21e37583-5a20-4a0e-826c-3dee2c375f54 + rle-decode("2A3B4C"), + "eq", + "AABBBCCCC", + "run-length decode a string: string with no single characters", +); # end: 21e37583-5a20-4a0e-826c-3dee2c375f54 + +cmp-ok( # begin: 1389ad09-c3a8-4813-9324-99363fba429c + rle-decode("12WB12W3B24WB"), + "eq", + "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB", + "run-length decode a string: single characters with repeated characters", +); # end: 1389ad09-c3a8-4813-9324-99363fba429c + +cmp-ok( # begin: 3f8e3c51-6aca-4670-b86c-a213bf4706b0 + rle-decode("2 hs2q q2w2 "), + "eq", + " hsqq qww ", + "run-length decode a string: multiple whitespace mixed in string", +); # end: 3f8e3c51-6aca-4670-b86c-a213bf4706b0 + +cmp-ok( # begin: 29f721de-9aad-435f-ba37-7662df4fb551 + rle-decode("2a3b4c"), + "eq", + "aabbbcccc", + "run-length decode a string: lowercase string", +); # end: 29f721de-9aad-435f-ba37-7662df4fb551 + +cmp-ok( # begin: 2a762efd-8695-4e04-b0d6-9736899fbc16 + rle-decode(rle-encode("zzz ZZ zZ")), + "eq", + "zzz ZZ zZ", + "encode and then decode: encode followed by decode gives original string", +); # end: 2a762efd-8695-4e04-b0d6-9736899fbc16 + +done-testing;