Skip to content

Commit

Permalink
Exercise: Run-length encoding (and decoding)
Browse files Browse the repository at this point in the history
This is currently draft/unfinished/work-in-progress.

ref: #172
  • Loading branch information
kotp committed Nov 2, 2023
1 parent 4ed3c5d commit 3e1039f
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 21 additions & 0 deletions exercises/practice/run-length-encoding/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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.


19 changes: 19 additions & 0 deletions exercises/practice/run-length-encoding/.meta/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
18 changes: 18 additions & 0 deletions exercises/practice/run-length-encoding/.meta/example.vim
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions exercises/practice/run-length-encoding/run_length_encoding.vader
Original file line number Diff line number Diff line change
@@ -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)
33 changes: 33 additions & 0 deletions exercises/practice/run-length-encoding/run_length_encoding.vim
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3e1039f

Please sign in to comment.