Skip to content

Commit 61dace9

Browse files
authored
Add run-length-encoding (#329)
1 parent cb584dd commit 61dace9

File tree

7 files changed

+184
-0
lines changed

7 files changed

+184
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,14 @@
589589
"prerequisites": [],
590590
"difficulty": 5
591591
},
592+
{
593+
"slug": "run-length-encoding",
594+
"name": "Run-Length Encoding",
595+
"uuid": "b496f1b1-c04e-4d13-95bf-507bf3e616a7",
596+
"practices": [],
597+
"prerequisites": [],
598+
"difficulty": 5
599+
},
592600
{
593601
"slug": "spiral-matrix",
594602
"name": "Spiral Matrix",
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Instructions
2+
3+
Implement run-length encoding and decoding.
4+
5+
Run-length encoding (RLE) is a simple form of data compression, where runs (consecutive data elements) are replaced by just one data value and count.
6+
7+
For example we can represent the original 53 characters with only 13.
8+
9+
```text
10+
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" -> "12WB12W3B24WB"
11+
```
12+
13+
RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression.
14+
15+
```text
16+
"AABCCCDEEEE" -> "2AB3CD4E" -> "AABCCCDEEEE"
17+
```
18+
19+
For simplicity, you can assume that the unencoded string will only contain the letters A through Z (either lower or upper case) and whitespace.
20+
This way data to be encoded will never contain any numbers and numbers inside data to be decoded always represent the count for the following character.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"run-length-encoding.coffee"
8+
],
9+
"test": [
10+
"run-length-encoding.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class RunLengthEncoding
2+
@encode: (string) ->
3+
consecutiveChars = /([\w\s])\1*/g
4+
string.replace consecutiveChars, (match) ->
5+
if match.length > 1 then match.length + match[0] else match[0]
6+
7+
@decode: (string) ->
8+
countAndChar = /(\d+)(\w|\s)/g
9+
string.replace countAndChar, (_, count, char) ->
10+
char.repeat count
11+
12+
module.exports = RunLengthEncoding
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[ad53b61b-6ffc-422f-81a6-61f7df92a231]
13+
description = "run-length encode a string -> empty string"
14+
15+
[52012823-b7e6-4277-893c-5b96d42f82de]
16+
description = "run-length encode a string -> single characters only are encoded without count"
17+
18+
[b7868492-7e3a-415f-8da3-d88f51f80409]
19+
description = "run-length encode a string -> string with no single characters"
20+
21+
[859b822b-6e9f-44d6-9c46-6091ee6ae358]
22+
description = "run-length encode a string -> single characters mixed with repeated characters"
23+
24+
[1b34de62-e152-47be-bc88-469746df63b3]
25+
description = "run-length encode a string -> multiple whitespace mixed in string"
26+
27+
[abf176e2-3fbd-40ad-bb2f-2dd6d4df721a]
28+
description = "run-length encode a string -> lowercase characters"
29+
30+
[7ec5c390-f03c-4acf-ac29-5f65861cdeb5]
31+
description = "run-length decode a string -> empty string"
32+
33+
[ad23f455-1ac2-4b0e-87d0-b85b10696098]
34+
description = "run-length decode a string -> single characters only"
35+
36+
[21e37583-5a20-4a0e-826c-3dee2c375f54]
37+
description = "run-length decode a string -> string with no single characters"
38+
39+
[1389ad09-c3a8-4813-9324-99363fba429c]
40+
description = "run-length decode a string -> single characters with repeated characters"
41+
42+
[3f8e3c51-6aca-4670-b86c-a213bf4706b0]
43+
description = "run-length decode a string -> multiple whitespace mixed in string"
44+
45+
[29f721de-9aad-435f-ba37-7662df4fb551]
46+
description = "run-length decode a string -> lowercase string"
47+
48+
[2a762efd-8695-4e04-b0d6-9736899fbc16]
49+
description = "encode and then decode -> encode followed by decode gives original string"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class RunLengthEncoding
2+
@encode: (string) ->
3+
4+
@decode: (string) ->
5+
6+
module.exports = RunLengthEncoding
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
RLE = require './run-length-encoding'
2+
3+
describe 'Run Length Encoding', ->
4+
describe 'encode', ->
5+
it 'empty string', ->
6+
input = ''
7+
expected = ''
8+
expect(RLE.encode input).toEqual expected
9+
10+
xit 'single characters only are encoded without count', ->
11+
input = 'XYZ'
12+
expected = 'XYZ'
13+
expect(RLE.encode input).toEqual expected
14+
15+
xit 'string with no single characters', ->
16+
input = 'AABBBCCCC'
17+
expected = '2A3B4C'
18+
expect(RLE.encode input).toEqual expected
19+
20+
xit 'string with single characters mixed with repeated characters', ->
21+
input = 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB'
22+
expected = '12WB12W3B24WB'
23+
expect(RLE.encode input).toEqual expected
24+
25+
xit 'string with multiple whitespace mixed', ->
26+
input = ' hsqq qww '
27+
expected = '2 hs2q q2w2 '
28+
expect(RLE.encode input).toEqual expected
29+
30+
xit 'string with lowercase characters', ->
31+
input = 'aabbbcccc'
32+
expected = '2a3b4c'
33+
expect(RLE.encode input).toEqual expected
34+
35+
describe 'decode', ->
36+
xit 'empty string', ->
37+
input = ''
38+
expected = ''
39+
expect(RLE.decode input).toEqual expected
40+
41+
xit 'string with single characters only', ->
42+
input = 'XYZ'
43+
expected = 'XYZ'
44+
expect(RLE.decode input).toEqual expected
45+
46+
xit 'string with no single characters', ->
47+
input = '2A3B4C'
48+
expected = 'AABBBCCCC'
49+
expect(RLE.decode input).toEqual expected
50+
51+
xit 'string with single characters mixed with repeated characters', ->
52+
input = '12WB12W3B24WB'
53+
expected = 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB'
54+
expect(RLE.decode input).toEqual expected
55+
56+
xit 'string with multiple whitespaces', ->
57+
input = '2 hs2q q2w2 '
58+
expected = ' hsqq qww '
59+
expect(RLE.decode input).toEqual expected
60+
61+
xit 'string with lowercase characters', ->
62+
input = '2a3b4c'
63+
expected = 'aabbbcccc'
64+
expect(RLE.decode input).toEqual expected
65+
66+
xit 'encode followed by decode gives original string', ->
67+
plainText = 'zzz ZZ zZ'
68+
input = RLE.encode plainText
69+
expected = 'zzz ZZ zZ'
70+
expect(RLE.decode input).toEqual expected

0 commit comments

Comments
 (0)