-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exercise: Run-length encoding (and decoding)
This is currently draft/unfinished/work-in-progress. ref: #172
- Loading branch information
Showing
6 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
exercises/practice/run-length-encoding/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
exercises/practice/run-length-encoding/run_length_encoding.vader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
exercises/practice/run-length-encoding/run_length_encoding.vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |