Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new run-length-encoding exercise #186

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,15 @@
"difficulty": 5,
"topics": null
},
{
"slug": "run-length-encoding",
"name": "Run Length Encoding",
"uuid": "e8b3987c-387d-4aad-95ac-0452c332a326",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": null
},
{
"slug": "queen-attack",
"name": "Queen Attack",
Expand Down
20 changes: 20 additions & 0 deletions exercises/practice/run-length-encoding/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Instructions

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.

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.

```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.
59 changes: 59 additions & 0 deletions exercises/practice/run-length-encoding/.meta/Example.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Here is an example solution for the Run Length Encoding exercise
*/
component {

/**
* @param input : unencoded string
*
* @returns RLE-encoded string
*/
function encode(input) {
if (isEmpty(input)) {
return '';
}

chars = input.listToArray('');
count = 1;
previousChar = '';
output = '';

for (char in chars) {
if (char == previousChar) {
count++;
} else {
num = (count > 1) ? toString(count) : '';
output = output & num & previousChar;
previousChar = char;
count = 1;
}
}
num = (count > 1) ? toString(count) : '';
output = output & num & previousChar;
return output;
}

/**
* @param input : RLE-encoded string
*
* @returns unencoded string
*/
function decode(input) {
number = '';
output = '';
chars = input.listToArray('');

for (char in chars) {
if (isNumeric(char)) {
number &= char;
} else {
repeats = (len(number) > 0) ? val(number) : 1;
output &= repeatString(char, repeats);
number = '';
}
}
return output;
}


}
7 changes: 7 additions & 0 deletions exercises/practice/run-length-encoding/.meta/ExampleTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
component extends="RunLengthEncodingTest" {

function beforeAll(){
SUT = createObject( 'Solution' );
}

}
17 changes: 17 additions & 0 deletions exercises/practice/run-length-encoding/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": ["colinleach"],
"files": {
"solution": [
"RunLengthEncoding.cfc"
],
"test": [
"RunLengthEncodingTest.cfc"
],
"example": [
".meta/Example.cfc"
]
},
"blurb": "Implement run-length encoding and decoding.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Run-length_encoding"
}
49 changes: 49 additions & 0 deletions exercises/practice/run-length-encoding/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[ad53b61b-6ffc-422f-81a6-61f7df92a231]
description = "run-length encode a string -> empty string"

[52012823-b7e6-4277-893c-5b96d42f82de]
description = "run-length encode a string -> single characters only are encoded without count"

[b7868492-7e3a-415f-8da3-d88f51f80409]
description = "run-length encode a string -> string with no single characters"

[859b822b-6e9f-44d6-9c46-6091ee6ae358]
description = "run-length encode a string -> single characters mixed with repeated characters"

[1b34de62-e152-47be-bc88-469746df63b3]
description = "run-length encode a string -> multiple whitespace mixed in string"

[abf176e2-3fbd-40ad-bb2f-2dd6d4df721a]
description = "run-length encode a string -> lowercase characters"

[7ec5c390-f03c-4acf-ac29-5f65861cdeb5]
description = "run-length decode a string -> empty string"

[ad23f455-1ac2-4b0e-87d0-b85b10696098]
description = "run-length decode a string -> single characters only"

[21e37583-5a20-4a0e-826c-3dee2c375f54]
description = "run-length decode a string -> string with no single characters"

[1389ad09-c3a8-4813-9324-99363fba429c]
description = "run-length decode a string -> single characters with repeated characters"

[3f8e3c51-6aca-4670-b86c-a213bf4706b0]
description = "run-length decode a string -> multiple whitespace mixed in string"

[29f721de-9aad-435f-ba37-7662df4fb551]
description = "run-length decode a string -> lowercase string"

[2a762efd-8695-4e04-b0d6-9736899fbc16]
description = "encode and then decode -> encode followed by decode gives original string"
20 changes: 20 additions & 0 deletions exercises/practice/run-length-encoding/RunLengthEncoding.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Your implementation of the Run Length Encoding exercise
*/
component {

/**
* @returns
*/
function encode(input) {
colinleach marked this conversation as resolved.
Show resolved Hide resolved
// Implement me here
}

/**
* @returns
*/
function decode(input) {
// Implement me here
}

}
74 changes: 74 additions & 0 deletions exercises/practice/run-length-encoding/RunLengthEncodingTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
component extends="testbox.system.BaseSpec" {

function beforeAll(){
SUT = createObject( 'runLengthEncoding' );
}

function run(){

describe( "My Run Length Encoding class", function(){

// run-length encode a string

it( 'empty string', function(){
expect( SUT.encode( input='' ) ).toBe( '' );
});

it( 'single characters only are encoded without count', function(){
expect( SUT.encode( input='XYZ' ) ).toBe( 'XYZ' );
});

it( 'string with no single characters', function(){
expect( SUT.encode( input='AABBBCCCC' ) ).toBe( '2A3B4C' );
});

it( 'single characters mixed with repeated characters', function(){
expect( SUT.encode( input='WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB' ) ).toBe( '12WB12W3B24WB' );
});

it( 'multiple whitespace mixed in string', function(){
expect( SUT.encode( input=' hsqq qww ' ) ).toBe( '2 hs2q q2w2 ' );
});

it( 'lowercase characters', function(){
expect( SUT.encode( input='aabbbcccc' ) ).toBe( '2a3b4c' );
});

// run-length decode a string

it( 'empty string', function(){
expect( SUT.decode( input='' ) ).toBe( '' );
});

it( 'single characters only', function(){
expect( SUT.decode( input='XYZ' ) ).toBe( 'XYZ' );
});

it( 'string with no single characters', function(){
expect( SUT.decode( input='2A3B4C' ) ).toBe( 'AABBBCCCC' );
});

it( 'single characters with repeated characters', function(){
expect( SUT.decode( input='12WB12W3B24WB' ) ).toBe( 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB' );
});

it( 'multiple whitespace mixed in string', function(){
expect( SUT.decode( input='2 hs2q q2w2 ' ) ).toBe( ' hsqq qww ' );
});

it( 'lowercase string', function(){
expect( SUT.decode( input='2a3b4c' ) ).toBe( 'aabbbcccc' );
});

// encode and then decode

it( 'encode followed by decode gives original string', function(){
expect( SUT.decode( SUT.encode( input='zzz ZZ zZ' ) ) ).toBe( 'zzz ZZ zZ' );
});


});

}

}
103 changes: 103 additions & 0 deletions exercises/practice/run-length-encoding/TestRunner.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* I am a CommandBox task runner which you can use to test your implementation of this exercise against the
* provided test suite. To use me, open the CommandBox CLI and run this:
*
* CommandBox> task run TestRunner
*
* To start up a test watcher that will automatically rerun the test suite every time you save a file change, run this:
*
* CommandBox> task run TestRunner --watcher
*
*/
component {

/**
* @solution Runs the tests against the solution
* @watcher Start up a file watch that re-runs the tests on file changes. Use Ctrl-C to stop
*/
function run( boolean solution=false, boolean watcher=false ) {

ensureTestBox();

if( watcher ) {

// Tabula rasa
command( 'cls' ).run();

// Start watcher
watch()
.paths( '*.cfc' )
.inDirectory( getCWD() )
.withDelay( 500 )
.onChange( function() {

// Clear the screen
command( 'cls' )
.run();

// This is neccessary so changes to tests get picked up right away.
pagePoolClear();

runTests( solution );

} )
.start();

} else {
runTests( solution );
}

}

/**
* Make sure the TestBox framework is installed
*/
private function ensureTestBox() {
var excerciseRoot = getCWD();
var testBoxRoot = excerciseRoot & '/testbox';

if( !directoryExists( testBoxRoot ) ) {

print.boldYellowLine( 'Installing some missing dependencies for you!' ).toConsole();
command( 'install' )
.inWorkingDirectory( excerciseRoot )
.run();
}

// Bootstrap TestBox framework
filesystemUtil.createMapping( '/testbox', testBoxRoot );
}

/**
* Invoke TestBox to run the test suite
*/
private function runTests( boolean solution=false ) {

// Create TestBox and run the tests
testData = new testbox.system.TestBox()
.runRaw( directory = {
// Find all CFCs...
mapping = filesystemUtil.makePathRelative( getCWD() ),
// ... in this directory ...
recurse = false,
// ... whose name ends in "test"
filter = function( path ) {
return path.reFind( ( solution ? 'Solution' : '' ) & 'Test.cfc$' );
}
} )
.getMemento();

// Print out the results with ANSI formatting for the CLI
getInstance( 'CLIRenderer@testbox-commands' )
.render( print, testData, true );

print.toConsole();

// Set proper exit code
if( testData.totalFail || testData.totalError ) {
setExitCode( 1 );
}
}

}

8 changes: 8 additions & 0 deletions exercises/practice/run-length-encoding/box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies":{
"testbox":"^2.5.0+107"
},
"installPaths":{
"testbox":"testbox/"
}
}
Loading