Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise: Run-length encoding (and decoding) #188

Merged
merged 6 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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.


22 changes: 22 additions & 0 deletions exercises/practice/run-length-encoding/.meta/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
43 changes: 43 additions & 0 deletions exercises/practice/run-length-encoding/.meta/example.vim
Original file line number Diff line number Diff line change
@@ -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
kotp marked this conversation as resolved.
Show resolved Hide resolved

64 changes: 64 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,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))
34 changes: 34 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,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
kotp marked this conversation as resolved.
Show resolved Hide resolved

Loading