From 3e1039f472445f83db5ea596eeab35eb433e322b Mon Sep 17 00:00:00 2001 From: KOTP Date: Thu, 2 Nov 2023 15:42:49 -0400 Subject: [PATCH] Exercise: Run-length encoding (and decoding) This is currently draft/unfinished/work-in-progress. ref: #172 --- config.json | 8 +++++ .../run-length-encoding/.docs/instructions.md | 21 ++++++++++++ .../run-length-encoding/.meta/config.json | 19 +++++++++++ .../run-length-encoding/.meta/example.vim | 18 ++++++++++ .../run_length_encoding.vader | 15 +++++++++ .../run_length_encoding.vim | 33 +++++++++++++++++++ 6 files changed, 114 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.vim create mode 100644 exercises/practice/run-length-encoding/run_length_encoding.vader create mode 100644 exercises/practice/run-length-encoding/run_length_encoding.vim diff --git a/config.json b/config.json index 028568a..21df9bd 100644 --- a/config.json +++ b/config.json @@ -193,6 +193,14 @@ "prerequisites": [], "difficulty": 3 }, + { + "slug": "run-length-encoding", + "name": "Run-Length Encoding", + "uuid": "1024a826-2f71-4833-9e96-59e0b0156f26", + "practices": [], + "prerequisites": [], + "difficulty": 3 + }, { "slug": "rna-transcription", "name": "RNA Transcription", 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..4ce96f7 --- /dev/null +++ b/exercises/practice/run-length-encoding/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "KOTP" + ], + "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..6f2acc5 --- /dev/null +++ b/exercises/practice/run-length-encoding/.meta/example.vim @@ -0,0 +1,18 @@ +function decode:(string) + return substitute(a:string, '\d\+\(\D\)', '\=submatch(1) . repeat(submatch(1), str2nr(submatch(0)))', 'g') +endfunction + +function encode:(string) + let size = 1 + let result = '' + while size <= len(a:string) + let char = a:string[size-1] + let l:count = 1 + while size+l:count <= len(a:string) && a:string[size+l:count-1] ==# char + let l:count += 1 + endwhile + let result .= l:count . char + let size += l:count + endwhile + return 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..cc5dbfa --- /dev/null +++ b/exercises/practice/run-length-encoding/run_length_encoding.vader @@ -0,0 +1,15 @@ + +Execute (no name given): + let g:name = '' + let g:expected = "One for you, one for me." + AssertEqual g:expected, TwoFer(g:name) + +Execute (a name given): + let g:name = "Alice" + let g:expected = "One for Alice, one for me." + AssertEqual g:expected, TwoFer(g:name) + +Execute (another name given): + let g:name = "Bob" + let g:expected = "One for Bob, one for me." + AssertEqual g:expected, TwoFer(g:name) 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..56e95ed --- /dev/null +++ b/exercises/practice/run-length-encoding/run_length_encoding.vim @@ -0,0 +1,33 @@ +"" +"" 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! RunLengthDecoding(name) abort + " your code goes here +endfunction + +function! RunLengthEncoding(name) abort + " your code goes here +endfunction