diff --git a/config.json b/config.json index e0c25e2..cf9b673 100644 --- a/config.json +++ b/config.json @@ -338,6 +338,14 @@ "prerequisites": [], "difficulty": 1 }, + { + "slug": "run-length-encoding", + "name": "Run-Length Encoding", + "uuid": "1024a826-2f71-4833-9e96-59e0b0156f26", + "practices": [], + "prerequisites": [], + "difficulty": 3 + }, { "slug": "robot-simulator", "name": "Robot Simulator", 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..4757b87 --- /dev/null +++ b/exercises/practice/run-length-encoding/.docs/instructions.md @@ -0,0 +1,21 @@ +# Instructions + +Write the function that uses the run-length encoding and decoding algorithms to encode and decode a string. + +Run-length encoding (RLE) is a simple form of data compression where runs of consecutive identical data elements are replaced with one data value and count. + +Example: + +"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" -> "12WB12W3B24WB" + +RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression. + +Example: + +"AABCCCDEEEE" -> "2AB3CD4E" -> "AABCCCDEEEE" + +For simplicity, you can assume that the unencoded string will only contain the letters A through Z. (either upper or lower case) and whitespace. + +This means that the encoded data will not contain any numbers, so that any numbers contained within data to be decoded will always represent the count for the character that follows it. + + 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..7f06b7f --- /dev/null +++ b/exercises/practice/run-length-encoding/.meta/config.json @@ -0,0 +1,22 @@ +{ + "authors": [ + "KOTP" + ], + "contributors": [ + "BNAndras" + ], + "files": { + "solution": [ + "run_length_encoding.vim" + ], + "test": [ + "run_length_encoding.vader" + ], + "example": [ + ".meta/example.vim" + ] + }, + "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.vim b/exercises/practice/run-length-encoding/.meta/example.vim new file mode 100644 index 0000000..5bfcb1f --- /dev/null +++ b/exercises/practice/run-length-encoding/.meta/example.vim @@ -0,0 +1,43 @@ +function! Decode(string) abort + let l:result = '' + let l:charCount = '' + for l:current in split(a:string, '\zs') + let l:codepoint = char2nr(l:current) + if (l:codepoint < 48 || l:codepoint > 57) + let l:number = max([l:charCount, 1]) + let l:result .= repeat(l:current, l:number) + let l:charCount = '' + else + let l:charCount .= l:current + endif + endfor + + return l:result +endfunction + +function! Encode(string) abort + if len(a:string) == 0 + return '' + endif + + let l:result = '' + let l:charCount = 1 + let l:previous = '' + for l:current in split(a:string, '\zs') + if (l:current != l:previous) + let l:number = l:charCount > 1 ? l:charCount : '' + let l:result .= l:number . l:previous + + let l:previous = l:current + let l:charCount = 1 + else + let l:charCount += 1 + endif + endfor + + let l:number = l:charCount > 1 ? l:charCount : '' + let l:result .= l:number . l:previous + + return l:result +endfunction + diff --git a/exercises/practice/run-length-encoding/run_length_encoding.vader b/exercises/practice/run-length-encoding/run_length_encoding.vader new file mode 100644 index 0000000..faa336e --- /dev/null +++ b/exercises/practice/run-length-encoding/run_length_encoding.vader @@ -0,0 +1,64 @@ +Execute (encode - empty string): + let g:string = "" + let g:expected = "" + AssertEqual g:expected, Encode(g:string) + +Execute (encode - single characters only are encoded without count): + let g:string = "XYZ" + let g:expected = "XYZ" + AssertEqual g:expected, Encode(g:string) + +Execute (encode - string with no single characters): + let g:string = "AABBBCCCC" + let g:expected = "2A3B4C" + AssertEqual g:expected, Encode(g:string) + +Execute (encode - single characters mixed with repeated characters): + let g:string = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" + let g:expected = "12WB12W3B24WB" + AssertEqual g:expected, Encode(g:string) + +Execute (encode - multiple whitespace mixed in string): + let g:string = " hsqq qww " + let g:expected = "2 hs2q q2w2 " + AssertEqual g:expected, Encode(g:string) + +Execute (encode - lowercase characters): + let g:string = "aabbbcccc" + let g:expected = "2a3b4c" + AssertEqual g:expected, Encode(g:string) + +Execute (decode - empty string): + let g:string = "" + let g:expected = "" + AssertEqual g:expected, Decode(g:string) + +Execute (decode - single characters only): + let g:string = "XYZ" + let g:expected = "XYZ" + AssertEqual g:expected, Decode(g:string) + +Execute (decode - string with no single characters): + let g:string = "2A3B4C" + let g:expected = "AABBBCCCC" + AssertEqual g:expected, Decode(g:string) + +Execute (decode - single characters with repeated characters): + let g:string = "12WB12W3B24WB" + let g:expected = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" + AssertEqual g:expected, Decode(g:string) + +Execute (decode - multiple whitespace mixed in string): + let g:string = "2 hs2q q2w2 " + let g:expected = " hsqq qww " + AssertEqual g:expected, Decode(g:string) + +Execute (decode - lowercase string): + let g:string = "2a3b4c" + let g:expected = "aabbbcccc" + AssertEqual g:expected, Decode(g:string) + +Execute (decode - encode followed by decode gives original string): + let g:string = "zzz ZZ zZ" + let g:expected = "zzz ZZ zZ" + AssertEqual g:expected, Decode(Encode(g:string)) diff --git a/exercises/practice/run-length-encoding/run_length_encoding.vim b/exercises/practice/run-length-encoding/run_length_encoding.vim new file mode 100644 index 0000000..d265172 --- /dev/null +++ b/exercises/practice/run-length-encoding/run_length_encoding.vim @@ -0,0 +1,34 @@ +"" +"" Write a function that will perform the algorithm that is known as "run-length +"" encoding". +"" +"" Run-length encoding (RLE) is a simple form of data compression where runs of +"" consecutive identical data elements are replaced with one data value and +"" count. +"" +"" Example: +"" +"" "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" -> "12WB12W3B24WB" +"" +"" RLE allows the original data to be perfectly reconstructed from the +"" compressed data, which makes it a lossless data compression. +"" +"" Example: +"" +"" "AABCCCDEEEE" -> "2AB3CD4E" -> "AABCCCDEEEE" +"" +"" For simplicity, you can assume that the unencoded string will only contain +"" the letters A through Z. (either upper or lower case) and whitespace. + +"" This means that the encoded data will not contain any numbers, so that any +"" numbers contained within data to be decoded will always represent the count +"" for the character that follows it. +"" +function! Decode(string) abort + " your code goes here +endfunction + +function! Encode(string) abort + " your code goes here +endfunction +