Skip to content

Commit

Permalink
sync run length encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasnorre committed Oct 28, 2024
1 parent 32b62f8 commit 2fd159d
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 72 deletions.
12 changes: 4 additions & 8 deletions exercises/practice/run-length-encoding/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@

Implement run-length encoding and decoding.

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.
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.

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

```text
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" -> "12WB12W3B24WB"
```

RLE allows the original data to be perfectly reconstructed from
the compressed data, which makes it a lossless data compression.
RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression.

```text
"AABCCCDEEEE" -> "2AB3CD4E" -> "AABCCCDEEEE"
```

For simplicity, you can assume that the unencoded string will only contain
the letters A through Z (either lower or upper case) and whitespace. 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.
For simplicity, you can assume that the unencoded string will only contain the letters A through Z (either lower or upper case) and whitespace.
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.
22 changes: 0 additions & 22 deletions exercises/practice/run-length-encoding/.meta/example.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

/**
Expand Down
157 changes: 115 additions & 42 deletions exercises/practice/run-length-encoding/RunLengthEncodingTest.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

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

/**
* uuid: ad53b61b-6ffc-422f-81a6-61f7df92a231
* @testdox empty string
*/
public function testEncodeEmptyString(): void
{
$this->assertEquals('', encode(''));
$this->assertEquals(
'',
encode('')
);
}

public function testEncodeSingleCharactersOnly(): void
/**
* uuid: 52012823-b7e6-4277-893c-5b96d42f82de
* @testdox single characters only are encoded without count
*/
public function testEncodeSingleCharactersOnlyAreEncodedWithoutCount(): void
{
$this->assertEquals('XYZ', encode('XYZ'));
$this->assertEquals(
'XYZ',
encode('XYZ')
);
}

public function testDecodeEmptyString(): void
/**
* uuid: b7868492-7e3a-415f-8da3-d88f51f80409
* @testdox string with no single characters
*/
public function testEncodeStringWithNoSingleCharacters(): void
{
$this->assertEquals('', decode(''));
$this->assertEquals(
'2A3B4C',
encode('AABBBCCCC')
);
}

public function testDecodeSingleCharactersOnly(): void
/**
* uuid: 859b822b-6e9f-44d6-9c46-6091ee6ae358
* @testdox single characters mixed with repeated characters
*/
public function testEncodeSingleCharactersMixedWithRepeatedCharacters(): void
{
$this->assertEquals('XYZ', decode('XYZ'));
$this->assertEquals(
'12WB12W3B24WB',
encode('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB')
);
}

public function testEncodeSimple(): void
/**
* uuid: 1b34de62-e152-47be-bc88-469746df63b3
* @testdox multiple whitespace mixed in string
*/
public function testEncodeMultipleWhitespaceMixedInString(): void
{
$this->assertEquals('2A3B4C', encode('AABBBCCCC'));
$this->assertEquals(
'2 hs2q q2w2 ',
encode(' hsqq qww ')
);
}

public function testDecodeSimple(): void
/**
* uuid: abf176e2-3fbd-40ad-bb2f-2dd6d4df721a
* @testdox lowercase characters
*/
public function testEncodeLowercaseCharacters(): void
{
$this->assertEquals('AABBBCCCC', decode('2A3B4C'));
$this->assertEquals(
'2a3b4c',
encode('aabbbcccc')
);
}

public function testEncodeWithSingleValues(): void
/**
* uuid: 7ec5c390-f03c-4acf-ac29-5f65861cdeb5
* @testdox empty string
*/
public function testDecodeEmptyString(): void
{
$this->assertEquals(
'12WB12W3B24WB',
encode('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB')
'',
decode('')
);
}

public function testDecodeWithSingleValues(): void
/**
* uuid: ad23f455-1ac2-4b0e-87d0-b85b10696098
* @testdox single characters only
*/
public function testDecodeSingleCharactersOnly(): void
{
$this->assertEquals(
'XYZ',
decode('XYZ')
);
}
/**
* uuid: 21e37583-5a20-4a0e-826c-3dee2c375f54
* @testdox string with no single characters
*/
public function testDecodeStringWithNoSingleCharacters(): void
{
$this->assertEquals(
'AABBBCCCC',
decode('2A3B4C')
);
}
/**
* uuid: 1389ad09-c3a8-4813-9324-99363fba429c
* @testdox single characters with repeated characters
*/
public function testDecodeSingleCharactersWithRepeatedCharacters(): void
{
$this->assertEquals(
'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB',
decode('12WB12W3B24WB')
);
}

/**
* uuid: 3f8e3c51-6aca-4670-b86c-a213bf4706b0
* @testdox multiple whitespace mixed in string
*/
public function testDecodeMultipleWhitespaceMixedInString(): void
{
$this->assertEquals(' hsqq qww ', decode('2 hs2q q2w2 '));
$this->assertEquals(
' hsqq qww ',
decode('2 hs2q q2w2 ')
);
}

public function testEncodeDecodeCombination(): void
/**
* uuid: 29f721de-9aad-435f-ba37-7662df4fb551
* @testdox lowercase string
*/
public function testDecodeLowercaseString(): void
{
$this->assertEquals('zzz ZZ zZ', decode(encode('zzz ZZ zZ')));
$this->assertEquals(
'aabbbcccc',
decode('2a3b4c')
);
}
/**
* uuid: 2a762efd-8695-4e04-b0d6-9736899fbc16
* @testdox encode followed by decode gives original string
*/
public function testEncodeFollowedByDecodeGivesOriginalString(): void
{
$this->assertEquals(
'zzz ZZ zZ',
decode('zzz ZZ zZ')
);
}
}

0 comments on commit 2fd159d

Please sign in to comment.