Skip to content

Commit

Permalink
feat: new exercise - square-root (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
GroophyLifefor committed Apr 13, 2024
1 parent 49e040f commit c409cc7
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "de9d1220-6e35-42cd-8be0-9994dbb5edab",
"practices": ["numbers"],
"prerequisites": ["basics", "numbers", "conditionals", "loops"],
"difficulty": 2
}
]
},
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 @@
# Description

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
10 changes: 10 additions & 0 deletions exercises/practice/square-root/.meta/Example.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@echo off
setlocal EnableDelayedExpansion

set "input=%~1"
set "result="

set "Sqrt(N)=( x=(N)/(11*1024)+40, x=((N)/x+x)/2, x=((N)/x+x)/2, x=((N)/x+x)/2, x=((N)/x+x)/2, x=((N)/x+x)/2 )"
set /a "result=%sqrt(n):n=!input!%"

echo %result%
16 changes: 16 additions & 0 deletions exercises/practice/square-root/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"authors": ["GroophyLifefor"],
"files": {
"solution": [
"SquareRoot.bat"
],
"test": [
"SquareRootTest.bat"
],
"example": [
".meta/Example.bat"
]
},
"blurb": "Given a natural radicand, return its square root.",
"source": "wolf99"
}
13 changes: 13 additions & 0 deletions exercises/practice/square-root/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 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.

[af9ffe10-dc13-42d8-a742-e7bdafac449d]
description = "Say Hi!"
10 changes: 10 additions & 0 deletions exercises/practice/square-root/SquareRoot.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@echo off
setlocal EnableDelayedExpansion

set "input=%~1"
set "result="

REM Your code goes here


echo %result%
114 changes: 114 additions & 0 deletions exercises/practice/square-root/SquareRootTest.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
@echo off
REM ---------------------------------------------------
REM Square Root Unit Testing
REM ---------------------------------------------------

:Main
REM Initalize result variable
set "slug=SquareRoot"

CALL :Initialize

REM --------------------
REM Test Case Start \/\/
REM Resource: https://github.com/exercism/problem-specifications/blob/main/exercises/square-root/canonical-data.json
REM --------------------
set "expected=1"
set "if_success=Test passed"
set "if_failed=Test failed: root of 1"
CALL :Assert "1"

set "expected=2"
set "if_success=Test passed"
set "if_failed=Test failed: root of 4"
CALL :Assert "4"

set "expected=5"
set "if_success=Test passed"
set "if_failed=Test failed: root of 25"
CALL :Assert "25"

set "expected=9"
set "if_success=Test passed"
set "if_failed=Test failed: root of 81"
CALL :Assert "81"

set "expected=14"
set "if_success=Test passed"
set "if_failed=Test failed: root of 196"
CALL :Assert "196"

set "expected=255"
set "if_success=Test passed"
set "if_failed=Test failed: root of 65025"
CALL :Assert "65025"

REM --------------------
REM Test Case End /\/\/\
REM --------------------

CALL :ResolveStatus
exit /b %errorlevel%
REM End of Main

REM ---------------------------------------------------
REM Assert [..Parameters(up to 9)]
REM ---------------------------------------------------
GOTO :End REM Prevents the code below from being executed
:Assert
set "stdout="

REM Run the program and capture the output then delete the file
CALL %slug%.bat %~1 %~2 %~3 %~4 %~5 %~6 %~7 %~8 %~9 > stdout.bin 2>&1
set /p stdout=<stdout.bin
del stdout.bin

REM Check if the result is correct
if "%stdout%" == "%expected%" (
if defined if_success (
echo %if_success%

REM Reset the variable to avoid duplicating the message.
set "if_success="
set "if_failed="
)

REM If the result is correct, exit with code 0
set /a successCount+=1
exit /b 0
) else (
if defined if_failed (
echo %if_failed%

REM Reset the variable to avoid duplicating the message.
set "if_success="
set "if_failed="
)

REM If the result is incorrect, exit with code 1
set /a failCount+=1
exit /b 1
)
GOTO :EOF REM Go back to the line after the call to :Assert

:Initialize
REM It's for initialize, not about checking empty file
set "successCount=0"
set "failCount=0"
GOTO :EOF REM Go back to the line after the call to :CheckEmptyFile

:ResolveStatus
set "status="
if %failCount% gtr 0 (
REM status: Fail
REM message: The test failed.
exit /b 1

) else (
REM status: Pass
exit /b 0

)
GOTO :EOF REM Go back to the line after the call to :ExportResultAsJson

:End

0 comments on commit c409cc7

Please sign in to comment.