Skip to content

Commit 5f6aa51

Browse files
committed
Exercise: Run-length encoding (and decoding)
This is currently draft/unfinished/work-in-progress.
1 parent 4ed3c5d commit 5f6aa51

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Instructions
2+
3+
Write the function that uses the run-length encoding and decoding algorithms to encode and decode a string.
4+
5+
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.
6+
7+
Example:
8+
9+
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" -> "12WB12W3B24WB"
10+
11+
RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression.
12+
13+
Example:
14+
15+
"AABCCCDEEEE" -> "2AB3CD4E" -> "AABCCCDEEEE"
16+
17+
For simplicity, you can assume that the unencoded string will only contain the letters A through Z. (either upper or lower case) and whitespace.
18+
19+
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.
20+
21+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"KOTP"
4+
],
5+
"files": {
6+
"solution": [
7+
"run_length_encoding.vim"
8+
],
9+
"test": [
10+
"run_length_encoding.vader"
11+
],
12+
"example": [
13+
".meta/example.vim"
14+
]
15+
},
16+
"blurb": "Implement run-length encoding and decoding.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Run-length_encoding"
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function decode:(string)
2+
return substitute(a:string, '\d\+\(\D\)', '\=submatch(1) . repeat(submatch(1), str2nr(submatch(0)))', 'g')
3+
endfunction
4+
5+
function encode:(string)
6+
let size = 1
7+
let result = ''
8+
while size <= len(a:string)
9+
let char = a:string[size-1]
10+
let l:count = 1
11+
while size+l:count <= len(a:string) && a:string[size+l:count-1] ==# char
12+
let l:count += 1
13+
endwhile
14+
let result .= l:count . char
15+
let size += l:count
16+
endwhile
17+
return result
18+
endfunction
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
""
2+
"" Write a function that will perform the algorithm that is known as "run-length
3+
"" encoding".
4+
""
5+
"" Run-length encoding (RLE) is a simple form of data compression where runs of
6+
"" consecutive identical data elements are replaced with one data value and
7+
"" count.
8+
""
9+
"" Example:
10+
""
11+
"" "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" -> "12WB12W3B24WB"
12+
""
13+
"" RLE allows the original data to be perfectly reconstructed from the
14+
"" compressed data, which makes it a lossless data compression.
15+
""
16+
"" Example:
17+
""
18+
"" "AABCCCDEEEE" -> "2AB3CD4E" -> "AABCCCDEEEE"
19+
""
20+
"" For simplicity, you can assume that the unencoded string will only contain
21+
"" the letters A through Z. (either upper or lower case) and whitespace.
22+
23+
"" This means that the encoded data will not contain any numbers, so that any
24+
"" numbers contained within data to be decoded will always represent the count
25+
"" for the character that follows it.
26+
""
27+
function! RunLengthDecoding(name) abort
28+
" your code goes here
29+
endfunction
30+
31+
function! RunLengthEncoding(name) abort
32+
" your code goes here
33+
endfunction
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
Execute (no name given):
3+
let g:name = ''
4+
let g:expected = "One for you, one for me."
5+
AssertEqual g:expected, TwoFer(g:name)
6+
7+
Execute (a name given):
8+
let g:name = "Alice"
9+
let g:expected = "One for Alice, one for me."
10+
AssertEqual g:expected, TwoFer(g:name)
11+
12+
Execute (another name given):
13+
let g:name = "Bob"
14+
let g:expected = "One for Bob, one for me."
15+
AssertEqual g:expected, TwoFer(g:name)

0 commit comments

Comments
 (0)