Skip to content

Commit de16884

Browse files
authored
Add square-root (#220)
1 parent 299bd67 commit de16884

File tree

11 files changed

+291
-1
lines changed

11 files changed

+291
-1
lines changed

config.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,14 @@
347347
"prerequisites": [],
348348
"difficulty": 1
349349
},
350+
{
351+
"slug": "square-root",
352+
"name": "Square Root",
353+
"uuid": "a224cd5d-4d70-43d5-9204-7ba59bc4fe3d",
354+
"practices": [],
355+
"prerequisites": [],
356+
"difficulty": 2
357+
},
350358
{
351359
"slug": "strain",
352360
"name": "Strain",
@@ -442,4 +450,4 @@
442450
"used_for/frontends",
443451
"used_for/web_development"
444452
]
445-
}
453+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Instructions
2+
3+
Given a natural radicand, return its square root.
4+
5+
Note that the term "radicand" refers to the number for which the root is to be determined.
6+
That is, it is the number under the root symbol.
7+
8+
Check out the Wikipedia pages on [square root][square-root] and [methods of computing square roots][computing-square-roots].
9+
10+
Recall also that natural numbers are positive real whole numbers (i.e. 1, 2, 3 and up).
11+
12+
[square-root]: https://en.wikipedia.org/wiki/Square_root
13+
[computing-square-roots]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Here is an example solution for the SquareRoot exercise
3+
*/
4+
component {
5+
6+
function squareRoot( radicand ) {
7+
var result = 0;
8+
while( result ^ 2 != radicand ) {
9+
result += 1;
10+
}
11+
12+
return result;
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
component extends="SquareRootTest" {
2+
3+
function beforeAll(){
4+
SUT = createObject( 'Solution' );
5+
}
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"SquareRoot.cfc"
8+
],
9+
"test": [
10+
"SquareRootTest.cfc"
11+
],
12+
"example": [
13+
".meta/Example.cfc"
14+
]
15+
},
16+
"blurb": "Given a natural radicand, return its square root.",
17+
"source": "wolf99",
18+
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[9b748478-7b0a-490c-b87a-609dacf631fd]
13+
description = "root of 1"
14+
15+
[7d3aa9ba-9ac6-4e93-a18b-2e8b477139bb]
16+
description = "root of 4"
17+
18+
[6624aabf-3659-4ae0-a1c8-25ae7f33c6ef]
19+
description = "root of 25"
20+
21+
[93beac69-265e-4429-abb1-94506b431f81]
22+
description = "root of 81"
23+
24+
[fbddfeda-8c4f-4bc4-87ca-6991af35360e]
25+
description = "root of 196"
26+
27+
[c03d0532-8368-4734-a8e0-f96a9eb7fc1d]
28+
description = "root of 65025"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Your implementation of the SquareRoot exercise
3+
*/
4+
component {
5+
6+
/**
7+
* @returns
8+
*/
9+
function squareRoot( radicand ) {
10+
// Implement me here
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
component extends="testbox.system.BaseSpec" {
2+
3+
function beforeAll(){
4+
SUT = createObject( 'SquareRoot' );
5+
}
6+
7+
function run(){
8+
9+
describe( "My SquareRoot class", function(){
10+
11+
it( 'root of 1', function(){
12+
expect( SUT.squareRoot( radicand='1' ) ).toBe( '1' );
13+
});
14+
15+
it( 'root of 4', function(){
16+
expect( SUT.squareRoot( radicand='4' ) ).toBe( '2' );
17+
});
18+
19+
it( 'root of 25', function(){
20+
expect( SUT.squareRoot( radicand='25' ) ).toBe( '5' );
21+
});
22+
23+
it( 'root of 81', function(){
24+
expect( SUT.squareRoot( radicand='81' ) ).toBe( '9' );
25+
});
26+
27+
it( 'root of 196', function(){
28+
expect( SUT.squareRoot( radicand='196' ) ).toBe( '14' );
29+
});
30+
31+
it( 'root of 65025', function(){
32+
expect( SUT.squareRoot( radicand='65025' ) ).toBe( '255' );
33+
});
34+
35+
});
36+
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* I am a CommandBox task runner which you can use to test your implementation of this exercise against the
3+
* provided test suite. To use me, open the CommandBox CLI and run this:
4+
*
5+
* CommandBox> task run TestRunner
6+
*
7+
* To start up a test watcher that will automatically rerun the test suite every time you save a file change, run this:
8+
*
9+
* CommandBox> task run TestRunner --watcher
10+
*
11+
*/
12+
component {
13+
14+
/**
15+
* @solution Runs the tests against the solution
16+
* @watcher Start up a file watch that re-runs the tests on file changes. Use Ctrl-C to stop
17+
*/
18+
function run( boolean solution=false, boolean watcher=false ) {
19+
20+
ensureTestBox();
21+
22+
if( watcher ) {
23+
24+
// Tabula rasa
25+
command( 'cls' ).run();
26+
27+
// Start watcher
28+
watch()
29+
.paths( '*.cfc' )
30+
.inDirectory( getCWD() )
31+
.withDelay( 500 )
32+
.onChange( function() {
33+
34+
// Clear the screen
35+
command( 'cls' )
36+
.run();
37+
38+
// This is neccessary so changes to tests get picked up right away.
39+
pagePoolClear();
40+
41+
runTests( solution );
42+
43+
} )
44+
.start();
45+
46+
} else {
47+
runTests( solution );
48+
}
49+
50+
}
51+
52+
/**
53+
* Make sure the TestBox framework is installed
54+
*/
55+
private function ensureTestBox() {
56+
var excerciseRoot = getCWD();
57+
var testBoxRoot = excerciseRoot & '/testbox';
58+
59+
if( !directoryExists( testBoxRoot ) ) {
60+
61+
print.boldYellowLine( 'Installing some missing dependencies for you!' ).toConsole();
62+
command( 'install' )
63+
.inWorkingDirectory( excerciseRoot )
64+
.run();
65+
}
66+
67+
// Bootstrap TestBox framework
68+
filesystemUtil.createMapping( '/testbox', testBoxRoot );
69+
}
70+
71+
/**
72+
* Invoke TestBox to run the test suite
73+
*/
74+
private function runTests( boolean solution=false ) {
75+
76+
// Create TestBox and run the tests
77+
testData = new testbox.system.TestBox()
78+
.runRaw( directory = {
79+
// Find all CFCs...
80+
mapping = filesystemUtil.makePathRelative( getCWD() ),
81+
// ... in this directory ...
82+
recurse = false,
83+
// ... whose name ends in "test"
84+
filter = function( path ) {
85+
return path.reFind( ( solution ? 'Solution' : '' ) & 'Test.cfc$' );
86+
}
87+
} )
88+
.getMemento();
89+
90+
// Print out the results with ANSI formatting for the CLI
91+
getInstance( 'CLIRenderer@testbox-commands' )
92+
.render( print, testData, true );
93+
94+
print.toConsole();
95+
96+
// Set proper exit code
97+
if( testData.totalFail || testData.totalError ) {
98+
setExitCode( 1 );
99+
}
100+
}
101+
102+
}
103+
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"dependencies":{
3+
"testbox":"^2.5.0+107"
4+
},
5+
"installPaths":{
6+
"testbox":"testbox/"
7+
}
8+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!---
2+
3+
This file will only be used if you want to start up a web server in this directory. You can do so by running:
4+
5+
$> box
6+
CommandBox> install
7+
CommandBox> server start
8+
9+
However, this is not necessary unless you really just want to use the HTML reporters on TestBox.
10+
Ideally, you'll skip the need for this file entirely and just run the tests directly frm the CLI like this:
11+
12+
CommandBox> task run TestRunner
13+
14+
--->
15+
<cfsetting showDebugOutput="false">
16+
<cfparam name="url.reporter" default="simple">
17+
<cfscript>
18+
// get a list of all CFCs in this folder whose name looks like "XXXTest.cfc"
19+
// And turn it into compnent path relative to the web root
20+
url.bundles = directoryList(
21+
path=expandPath( '/' ),
22+
filter='*Test.cfc' )
23+
.map( function( path ) {
24+
return path
25+
.replaceNoCase( expandPath( '/' ), '' )
26+
.left( -4 )
27+
} )
28+
.toList();
29+
</cfscript>
30+
31+
<!--- Ensure TestBox --->
32+
<cfif fileExists( "/testbox/system/runners/HTMLRunner.cfm" )>
33+
<!--- Include the TestBox HTML Runner --->
34+
<cfinclude template="/testbox/system/runners/HTMLRunner.cfm">
35+
<cfelse>
36+
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.
37+
</cfif>

0 commit comments

Comments
 (0)