From 9e2c8dbba2a42616c751966368375aba9d724035 Mon Sep 17 00:00:00 2001 From: Murat Kirazkaya Date: Sat, 4 May 2024 20:50:54 +0300 Subject: [PATCH 1/3] feat: new exercise difference-of-squares --- config.json | 8 ++ .../.docs/instructions.md | 14 +++ .../difference-of-squares/.meta/Example.bat | 19 ++++ .../difference-of-squares/.meta/config.json | 16 +++ .../difference-of-squares/.meta/tests.toml | 13 +++ .../DifferenceOfSquares.bat | 10 ++ .../DifferenceOfSquaresTest.bat | 104 ++++++++++++++++++ 7 files changed, 184 insertions(+) create mode 100644 exercises/practice/difference-of-squares/.docs/instructions.md create mode 100644 exercises/practice/difference-of-squares/.meta/Example.bat create mode 100644 exercises/practice/difference-of-squares/.meta/config.json create mode 100644 exercises/practice/difference-of-squares/.meta/tests.toml create mode 100644 exercises/practice/difference-of-squares/DifferenceOfSquares.bat create mode 100644 exercises/practice/difference-of-squares/DifferenceOfSquaresTest.bat diff --git a/config.json b/config.json index a44a6bb..e1faa74 100644 --- a/config.json +++ b/config.json @@ -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 } ] }, diff --git a/exercises/practice/difference-of-squares/.docs/instructions.md b/exercises/practice/difference-of-squares/.docs/instructions.md new file mode 100644 index 0000000..ada61f1 --- /dev/null +++ b/exercises/practice/difference-of-squares/.docs/instructions.md @@ -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. diff --git a/exercises/practice/difference-of-squares/.meta/Example.bat b/exercises/practice/difference-of-squares/.meta/Example.bat new file mode 100644 index 0000000..9d75dec --- /dev/null +++ b/exercises/practice/difference-of-squares/.meta/Example.bat @@ -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% diff --git a/exercises/practice/difference-of-squares/.meta/config.json b/exercises/practice/difference-of-squares/.meta/config.json new file mode 100644 index 0000000..c3afcdb --- /dev/null +++ b/exercises/practice/difference-of-squares/.meta/config.json @@ -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" +} diff --git a/exercises/practice/difference-of-squares/.meta/tests.toml b/exercises/practice/difference-of-squares/.meta/tests.toml new file mode 100644 index 0000000..73466d6 --- /dev/null +++ b/exercises/practice/difference-of-squares/.meta/tests.toml @@ -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!" diff --git a/exercises/practice/difference-of-squares/DifferenceOfSquares.bat b/exercises/practice/difference-of-squares/DifferenceOfSquares.bat new file mode 100644 index 0000000..e8a1795 --- /dev/null +++ b/exercises/practice/difference-of-squares/DifferenceOfSquares.bat @@ -0,0 +1,10 @@ +@echo off +setlocal enabledelayedexpansion + +set "N=%~1" +set "difference=" + +REM Your code goes here + + +echo %difference% diff --git a/exercises/practice/difference-of-squares/DifferenceOfSquaresTest.bat b/exercises/practice/difference-of-squares/DifferenceOfSquaresTest.bat new file mode 100644 index 0000000..f238f38 --- /dev/null +++ b/exercises/practice/difference-of-squares/DifferenceOfSquaresTest.bat @@ -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=