Skip to content

Commit

Permalink
feat: new exercise difference-of-squares (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
GroophyLifefor committed May 7, 2024
1 parent 705ce1d commit 6a0ba07
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 @@ -178,6 +178,14 @@
"sets"
],
"difficulty": 6
},
{
"slug": "difference-of-squares",
"name": "Difference of Squares",
"uuid": "7051e75f-78a9-48b4-ac0a-7f6eb9262560",
"practices": [],
"prerequisites": ["basics", "numbers", "loops"],
"difficulty": 1
}
]
},
Expand Down
14 changes: 14 additions & 0 deletions exercises/practice/difference-of-squares/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Description

Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.

The square of the sum of the first ten natural numbers is
(1 + 2 + ... + 10)² = 55² = 3025.

The sum of the squares of the first ten natural numbers is
1² + 2² + ... + 10² = 385.

Hence the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers is 3025 - 385 = 2640.

You are not expected to discover an efficient solution to this yourself from first principles; research is allowed, indeed, encouraged.
Finding the best algorithm for the problem is a key skill in software engineering.
19 changes: 19 additions & 0 deletions exercises/practice/difference-of-squares/.meta/Example.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@echo off
setlocal enabledelayedexpansion

set "N=%~1"

set /a sum=0
set /a sum_of_squares=0

for /l %%i in (1, 1, %N%) do (
set /a sum+=%%i

set /a square=%%i*%%i
set /a sum_of_squares+=!square!
)

set /a square_of_sum=!sum!*!sum!
set /a difference=!square_of_sum!-!sum_of_squares!

echo %difference%
16 changes: 16 additions & 0 deletions exercises/practice/difference-of-squares/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"authors": ["GroophyLifefor"],
"files": {
"solution": [
"DifferenceOfSquares.bat"
],
"test": [
"DifferenceOfSquaresTest.bat"
],
"example": [
".meta/Example.bat"
]
},
"blurb": "Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.",
"source": "Problem 6 at Project Euler"
}
13 changes: 13 additions & 0 deletions exercises/practice/difference-of-squares/.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/difference-of-squares/DifferenceOfSquares.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@echo off
setlocal enabledelayedexpansion

set "N=%~1"
set "difference="

REM Your code goes here


echo %difference%
104 changes: 104 additions & 0 deletions exercises/practice/difference-of-squares/DifferenceOfSquaresTest.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
@echo off
REM ---------------------------------------------------
REM Difference Of Squares Unit Testing
REM
REM sUnit Testing Framework version: 0.2
REM ---------------------------------------------------

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

CALL :Initialize

REM --------------------
REM Test Case Start \/\/
REM Resource: https://github.com/exercism/problem-specifications/blob/main/exercises/difference-of-squares/canonical-data.json
REM --------------------

REM Case Title: difference of squares
REM Case Description: Subtract sum of squares from square of sums
set "expected=0"
set "if_success=Test passed"
set "if_failed=Test failed: difference of squares 1"
CALL :Assert "1"

set "expected=170"
set "if_success=Test passed"
set "if_failed=Test failed: difference of squares 5"
CALL :Assert "5"

set "expected=25164150"
set "if_success=Test passed"
set "if_failed=Test failed: difference of squares 100"
CALL :Assert "100"

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 6a0ba07

Please sign in to comment.