Skip to content

Commit

Permalink
Add reverse-string (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored Jan 3, 2024
1 parent 0d763f0 commit d31b95b
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "reverse-string",
"name": "Reverse String",
"uuid": "9f53f896-1390-4cc9-89fc-02212a205c40",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "rna-transcription",
"name": "RNA Transcription",
Expand Down
7 changes: 7 additions & 0 deletions exercises/practice/reverse-string/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Instructions

Reverse a string

For example:
input: "cool"
output: "looc"
19 changes: 19 additions & 0 deletions exercises/practice/reverse-string/.meta/Example.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Here is an example solution for the ReverseString exercise
*/
component {

/**
* @returns
*/
function reverse( value ) {
var reversed = '';

for (i = Len(value); i >= 1; i--) {
reversed &= value[i];
}

return reversed;
}

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

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

}
19 changes: 19 additions & 0 deletions exercises/practice/reverse-string/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"ReverseString.cfc"
],
"test": [
"ReverseStringTest.cfc"
],
"example": [
".meta/Example.cfc"
]
},
"blurb": "Reverse a string.",
"source": "Introductory challenge to reverse an input string",
"source_url": "https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb"
}
28 changes: 28 additions & 0 deletions exercises/practice/reverse-string/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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.

[c3b7d806-dced-49ee-8543-933fd1719b1c]
description = "an empty string"

[01ebf55b-bebb-414e-9dec-06f7bb0bee3c]
description = "a word"

[0f7c07e4-efd1-4aaa-a07a-90b49ce0b746]
description = "a capitalized word"

[71854b9c-f200-4469-9f5c-1e8e5eff5614]
description = "a sentence with punctuation"

[1f8ed2f3-56f3-459b-8f3e-6d8d654a1f6c]
description = "a palindrome"

[b9e7dec1-c6df-40bd-9fa3-cd7ded010c4c]
description = "an even-sized word"
13 changes: 13 additions & 0 deletions exercises/practice/reverse-string/ReverseString.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Your implementation of the ReverseString exercise
*/
component {

/**
* @returns
*/
function reverse( value ) {
// Implement me here
}

}
39 changes: 39 additions & 0 deletions exercises/practice/reverse-string/ReverseStringTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
component extends="testbox.system.BaseSpec" {

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

function run(){

describe( "My ReverseString class", function(){

it( 'an empty string', function(){
expect( SUT.reverse( value='' ) ).toBe( '' );
});

it( 'a word', function(){
expect( SUT.reverse( value='robot' ) ).toBe( 'tobor' );
});

it( 'a capitalized word', function(){
expect( SUT.reverse( value='Ramen' ) ).toBe( 'nemaR' );
});

it( 'a sentence with punctuation', function(){
expect( SUT.reverse( value='I''m hungry!' ) ).toBe( '!yrgnuh m''I' );
});

it( 'a palindrome', function(){
expect( SUT.reverse( value='racecar' ) ).toBe( 'racecar' );
});

it( 'an even-sized word', function(){
expect( SUT.reverse( value='drawer' ) ).toBe( 'reward' );
});

});

}

}
103 changes: 103 additions & 0 deletions exercises/practice/reverse-string/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/reverse-string/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/"
}
}
37 changes: 37 additions & 0 deletions exercises/practice/reverse-string/index.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!---
This file will only be used if you want to start up a web server in this directory. You can do so by running:
$> box
CommandBox> install
CommandBox> server start
However, this is not necessary unless you really just want to use the HTML reporters on TestBox.
Ideally, you'll skip the need for this file entirely and just run the tests directly frm the CLI like this:
CommandBox> task run TestRunner
--->
<cfsetting showDebugOutput="false">
<cfparam name="url.reporter" default="simple">
<cfscript>
// get a list of all CFCs in this folder whose name looks like "XXXTest.cfc"
// And turn it into compnent path relative to the web root
url.bundles = directoryList(
path=expandPath( '/' ),
filter='*Test.cfc' )
.map( function( path ) {
return path
.replaceNoCase( expandPath( '/' ), '' )
.left( -4 )
} )
.toList();
</cfscript>
<!--- Ensure TestBox --->
<cfif fileExists( "/testbox/system/runners/HTMLRunner.cfm" )>
<!--- Include the TestBox HTML Runner --->
<cfinclude template="/testbox/system/runners/HTMLRunner.cfm">
<cfelse>
Oops, you don't have TestBox installed yet! Please run <b>box install</b> from the root of your exercise folder and refresh this page.
</cfif>

0 comments on commit d31b95b

Please sign in to comment.