Skip to content

Commit

Permalink
Add square-root
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Dec 24, 2023
1 parent 0d763f0 commit b7aad69
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 1 deletion.
10 changes: 9 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@
},
{
"slug": "secret-handshake",
"practices": [],
"name": "Secret Handshake",
"uuid": "04769d33-feb0-40c0-9b0f-dcbd48303524",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
Expand All @@ -323,6 +323,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "a224cd5d-4d70-43d5-9204-7ba59bc4fe3d",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "sum-of-multiples",
"name": "Sum of Multiples",
Expand Down
13 changes: 13 additions & 0 deletions exercises/practice/square-root/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Instructions

Given a natural radicand, return its square root.

Note that the term "radicand" refers to the number for which the root is to be determined.
That is, it is the number under the root symbol.

Check out the Wikipedia pages on [square root][square-root] and [methods of computing square roots][computing-square-roots].

Recall also that natural numbers are positive real whole numbers (i.e. 1, 2, 3 and up).

[square-root]: https://en.wikipedia.org/wiki/Square_root
[computing-square-roots]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
15 changes: 15 additions & 0 deletions exercises/practice/square-root/.meta/Example.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Here is an example solution for the SquareRoot exercise
*/
component {

function squareRoot( radicand ) {
var result = 0;
while( result ^ 2 != radicand ) {
result += 1;
}

return result;
}

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

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

}
19 changes: 19 additions & 0 deletions exercises/practice/square-root/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"SquareRoot.cfc"
],
"test": [
"SquareRootTest.cfc"
],
"example": [
".meta/Example.cfc"
]
},
"blurb": "Given a natural radicand, return its square root.",
"source": "wolf99",
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
}
28 changes: 28 additions & 0 deletions exercises/practice/square-root/.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.

[9b748478-7b0a-490c-b87a-609dacf631fd]
description = "root of 1"

[7d3aa9ba-9ac6-4e93-a18b-2e8b477139bb]
description = "root of 4"

[6624aabf-3659-4ae0-a1c8-25ae7f33c6ef]
description = "root of 25"

[93beac69-265e-4429-abb1-94506b431f81]
description = "root of 81"

[fbddfeda-8c4f-4bc4-87ca-6991af35360e]
description = "root of 196"

[c03d0532-8368-4734-a8e0-f96a9eb7fc1d]
description = "root of 65025"
13 changes: 13 additions & 0 deletions exercises/practice/square-root/SquareRoot.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Your implementation of the SquareRoot exercise
*/
component {

/**
* @returns
*/
function squareRoot( radicand ) {
// Implement me here
}

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

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

function run(){

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

it( 'root of 1', function(){
expect( SUT.squareRoot( radicand='1' ) ).toBe( '1' );
});

it( 'root of 4', function(){
expect( SUT.squareRoot( radicand='4' ) ).toBe( '2' );
});

it( 'root of 25', function(){
expect( SUT.squareRoot( radicand='25' ) ).toBe( '5' );
});

it( 'root of 81', function(){
expect( SUT.squareRoot( radicand='81' ) ).toBe( '9' );
});

it( 'root of 196', function(){
expect( SUT.squareRoot( radicand='196' ) ).toBe( '14' );
});

it( 'root of 65025', function(){
expect( SUT.squareRoot( radicand='65025' ) ).toBe( '255' );
});

});

}

}
103 changes: 103 additions & 0 deletions exercises/practice/square-root/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/square-root/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/square-root/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 b7aad69

Please sign in to comment.