Skip to content

Commit 69d68fa

Browse files
authored
Sync run-length-encoding (#830)
[no important files changed]
1 parent b6ed9e5 commit 69d68fa

File tree

3 files changed

+119
-72
lines changed

3 files changed

+119
-72
lines changed

exercises/practice/run-length-encoding/.docs/instructions.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22

33
Implement run-length encoding and decoding.
44

5-
Run-length encoding (RLE) is a simple form of data compression, where runs
6-
(consecutive data elements) are replaced by just one data value and count.
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.
76

87
For example we can represent the original 53 characters with only 13.
98

109
```text
1110
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" -> "12WB12W3B24WB"
1211
```
1312

14-
RLE allows the original data to be perfectly reconstructed from
15-
the compressed data, which makes it a lossless data compression.
13+
RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression.
1614

1715
```text
1816
"AABCCCDEEEE" -> "2AB3CD4E" -> "AABCCCDEEEE"
1917
```
2018

21-
For simplicity, you can assume that the unencoded string will only contain
22-
the letters A through Z (either lower or upper case) and whitespace. This way
23-
data to be encoded will never contain any numbers and numbers inside data to
24-
be decoded always represent the count for the following character.
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.

exercises/practice/run-length-encoding/.meta/example.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
/**
Lines changed: 115 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
class RunLengthEncodingTest extends PHPUnit\Framework\TestCase
@@ -31,59 +9,154 @@ public static function setUpBeforeClass(): void
319
require_once 'RunLengthEncoding.php';
3210
}
3311

12+
/**
13+
* uuid: ad53b61b-6ffc-422f-81a6-61f7df92a231
14+
* @testdox Run-length encode a string - empty string
15+
*/
3416
public function testEncodeEmptyString(): void
3517
{
36-
$this->assertEquals('', encode(''));
18+
$this->assertEquals(
19+
'',
20+
encode('')
21+
);
3722
}
3823

39-
public function testEncodeSingleCharactersOnly(): void
24+
/**
25+
* uuid: 52012823-b7e6-4277-893c-5b96d42f82de
26+
* @testdox Run-length encode a string - single characters only are encoded without count
27+
*/
28+
public function testEncodeSingleCharactersOnlyAreEncodedWithoutCount(): void
4029
{
41-
$this->assertEquals('XYZ', encode('XYZ'));
30+
$this->assertEquals(
31+
'XYZ',
32+
encode('XYZ')
33+
);
4234
}
4335

44-
public function testDecodeEmptyString(): void
36+
/**
37+
* uuid: b7868492-7e3a-415f-8da3-d88f51f80409
38+
* @testdox Run-length encode a string - string with no single characters
39+
*/
40+
public function testEncodeStringWithNoSingleCharacters(): void
4541
{
46-
$this->assertEquals('', decode(''));
42+
$this->assertEquals(
43+
'2A3B4C',
44+
encode('AABBBCCCC')
45+
);
4746
}
4847

49-
public function testDecodeSingleCharactersOnly(): void
48+
/**
49+
* uuid: 859b822b-6e9f-44d6-9c46-6091ee6ae358
50+
* @testdox Run-length encode a string - single characters mixed with repeated characters
51+
*/
52+
public function testEncodeSingleCharactersMixedWithRepeatedCharacters(): void
5053
{
51-
$this->assertEquals('XYZ', decode('XYZ'));
54+
$this->assertEquals(
55+
'12WB12W3B24WB',
56+
encode('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB')
57+
);
5258
}
5359

54-
public function testEncodeSimple(): void
60+
/**
61+
* uuid: 1b34de62-e152-47be-bc88-469746df63b3
62+
* @testdox Run-length encode a string - multiple whitespace mixed in string
63+
*/
64+
public function testEncodeMultipleWhitespaceMixedInString(): void
5565
{
56-
$this->assertEquals('2A3B4C', encode('AABBBCCCC'));
66+
$this->assertEquals(
67+
'2 hs2q q2w2 ',
68+
encode(' hsqq qww ')
69+
);
5770
}
5871

59-
public function testDecodeSimple(): void
72+
/**
73+
* uuid: abf176e2-3fbd-40ad-bb2f-2dd6d4df721a
74+
* @testdox Run-length encode a string - lowercase characters
75+
*/
76+
public function testEncodeLowercaseCharacters(): void
6077
{
61-
$this->assertEquals('AABBBCCCC', decode('2A3B4C'));
78+
$this->assertEquals(
79+
'2a3b4c',
80+
encode('aabbbcccc')
81+
);
6282
}
6383

64-
public function testEncodeWithSingleValues(): void
84+
/**
85+
* uuid: 7ec5c390-f03c-4acf-ac29-5f65861cdeb5
86+
* @testdox Run-length decode a string - empty string
87+
*/
88+
public function testDecodeEmptyString(): void
6589
{
6690
$this->assertEquals(
67-
'12WB12W3B24WB',
68-
encode('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB')
91+
'',
92+
decode('')
6993
);
7094
}
7195

72-
public function testDecodeWithSingleValues(): void
96+
/**
97+
* uuid: ad23f455-1ac2-4b0e-87d0-b85b10696098
98+
* @testdox Run-length decode a string - single characters only
99+
*/
100+
public function testDecodeSingleCharactersOnly(): void
101+
{
102+
$this->assertEquals(
103+
'XYZ',
104+
decode('XYZ')
105+
);
106+
}
107+
/**
108+
* uuid: 21e37583-5a20-4a0e-826c-3dee2c375f54
109+
* @testdox Run-length decode a string - string with no single characters
110+
*/
111+
public function testDecodeStringWithNoSingleCharacters(): void
112+
{
113+
$this->assertEquals(
114+
'AABBBCCCC',
115+
decode('2A3B4C')
116+
);
117+
}
118+
/**
119+
* uuid: 1389ad09-c3a8-4813-9324-99363fba429c
120+
* @testdox Run-length decode a string - single characters with repeated characters
121+
*/
122+
public function testDecodeSingleCharactersWithRepeatedCharacters(): void
73123
{
74124
$this->assertEquals(
75125
'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB',
76126
decode('12WB12W3B24WB')
77127
);
78128
}
79-
129+
/**
130+
* uuid: 3f8e3c51-6aca-4670-b86c-a213bf4706b0
131+
* @testdox Run-length decode a string - multiple whitespace mixed in string
132+
*/
80133
public function testDecodeMultipleWhitespaceMixedInString(): void
81134
{
82-
$this->assertEquals(' hsqq qww ', decode('2 hs2q q2w2 '));
135+
$this->assertEquals(
136+
' hsqq qww ',
137+
decode('2 hs2q q2w2 ')
138+
);
83139
}
84-
85-
public function testEncodeDecodeCombination(): void
140+
/**
141+
* uuid: 29f721de-9aad-435f-ba37-7662df4fb551
142+
* @testdox Run-length decode a string - lowercase string
143+
*/
144+
public function testDecodeLowercaseString(): void
86145
{
87-
$this->assertEquals('zzz ZZ zZ', decode(encode('zzz ZZ zZ')));
146+
$this->assertEquals(
147+
'aabbbcccc',
148+
decode('2a3b4c')
149+
);
150+
}
151+
/**
152+
* uuid: 2a762efd-8695-4e04-b0d6-9736899fbc16
153+
* @testdox encode followed by decode gives original string
154+
*/
155+
public function testEncodeFollowedByDecodeGivesOriginalString(): void
156+
{
157+
$this->assertEquals(
158+
'zzz ZZ zZ',
159+
decode(encode('zzz ZZ zZ'))
160+
);
88161
}
89162
}

0 commit comments

Comments
 (0)