-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new exercise - armstrong-numbers
- Loading branch information
1 parent
0bf26c9
commit 8170975
Showing
6 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
exercises/practice/armstrong-numbers/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Description | ||
|
||
An [Armstrong number][armstrong-number] is a number that is the sum of its own digits each raised to the power of the number of digits. | ||
|
||
For example: | ||
|
||
- 9 is an Armstrong number, because `9 = 9^1 = 9` | ||
- 10 is _not_ an Armstrong number, because `10 != 1^2 + 0^2 = 1` | ||
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153` | ||
- 154 is _not_ an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190` | ||
|
||
Write some code to determine whether a number is an Armstrong number. | ||
|
||
[armstrong-number]: https://en.wikipedia.org/wiki/Narcissistic_number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
@echo off | ||
setlocal enabledelayedexpansion | ||
|
||
set "numbers=%1" | ||
set "sum=" | ||
call :strLen numbersLen numbers | ||
|
||
for /F "delims=" %%G in ('cmd /D /U /C echo %numbers%^| find /V ""') do ( | ||
SET result=1 | ||
FOR /L %%i IN (1,1,!numbersLen!) DO SET /A result*=%%G | ||
set /a sum+=!result! | ||
) | ||
|
||
if %numbers% equ %sum% ( | ||
echo true | ||
) else ( | ||
echo false | ||
) | ||
|
||
REM ********* function ***************************** | ||
:strlen <resultVar> <stringVar> | ||
( | ||
setlocal EnableDelayedExpansion | ||
(set^ tmp=!%~2!) | ||
if defined tmp ( | ||
set "len=1" | ||
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( | ||
if "!tmp:~%%P,1!" NEQ "" ( | ||
set /a "len+=%%P" | ||
set "tmp=!tmp:~%%P!" | ||
) | ||
) | ||
) ELSE ( | ||
set len=0 | ||
) | ||
) | ||
( | ||
endlocal | ||
set "%~1=%len%" | ||
exit /b | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"authors": ["GroophyLifefor"], | ||
"files": { | ||
"solution": [ | ||
"ArmstrongNumbers.bat" | ||
], | ||
"test": [ | ||
"ArmstrongNumbersTest.bat" | ||
], | ||
"example": [ | ||
".meta/Example.bat" | ||
] | ||
}, | ||
"blurb": "Determine if a number is an Armstrong number.", | ||
"source": "Wikipedia", | ||
"source_url": "https://en.wikipedia.org/wiki/Narcissistic_number/" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@echo off | ||
setlocal enabledelayedexpansion | ||
|
||
set "numbers=%1" | ||
set "result=" | ||
|
||
REM Your code goes here | ||
|
||
|
||
echo %result% |
142 changes: 142 additions & 0 deletions
142
exercises/practice/armstrong-numbers/ArmstrongNumbersTest.bat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
@echo off | ||
REM --------------------------------------------------- | ||
REM Armstrong Numbers Unit Testing | ||
REM | ||
REM sUnit Testing Framework version: 0.2 | ||
REM --------------------------------------------------- | ||
|
||
:Main | ||
REM Initalize result variable | ||
set "slug=ArmstrongNumbers" | ||
|
||
CALL :Initialize | ||
|
||
REM -------------------- | ||
REM Test Case Start \/\/ | ||
REM Resource: https://github.com/exercism/problem-specifications/blob/main/exercises/acronym/canonical-data.json | ||
REM -------------------- | ||
set "expected=true" | ||
set "if_success=Test passed" | ||
set "if_failed=Test failed: Zero is an Armstrong number." | ||
CALL :Assert 0 | ||
|
||
set "expected=true" | ||
set "if_success=Test passed" | ||
set "if_failed=Test failed: Single-digit numbers are Armstrong numbers." | ||
CALL :Assert 5 | ||
|
||
set "expected=false" | ||
set "if_success=Test passed" | ||
set "if_failed=Test failed: There are no two-digit Armstrong numbers." | ||
CALL :Assert 10 | ||
|
||
set "expected=true" | ||
set "if_success=Test passed" | ||
set "if_failed=Test failed: Three-digit number that is an Armstrong number." | ||
CALL :Assert 153 | ||
|
||
set "expected=false" | ||
set "if_success=Test passed" | ||
set "if_failed=Test failed: Three-digit number that is not an Armstrong number." | ||
CALL :Assert 100 | ||
|
||
set "expected=true" | ||
set "if_success=Test passed" | ||
set "if_failed=Test failed: Four-digit number that is an Armstrong number." | ||
CALL :Assert 9474 | ||
|
||
set "expected=false" | ||
set "if_success=Test passed" | ||
set "if_failed=Test failed: Four-digit number that is not an Armstrong number." | ||
CALL :Assert 9475 | ||
|
||
set "expected=true" | ||
set "if_success=Test passed" | ||
set "if_failed=Test failed: Seven-digit number that is an Armstrong number." | ||
CALL :Assert 9926315 | ||
|
||
set "expected=false" | ||
set "if_success=Test passed" | ||
set "if_failed=Test failed: Seven-digit number that is not an Armstrong number." | ||
CALL :Assert 9926314 | ||
|
||
REM -- Batch Scripting does not support 64-bit integers -- | ||
REM set "expected=true" | ||
REM set "if_success=Test passed" | ||
REM set "if_failed=Test failed: Armstrong number containing seven zeroes." | ||
REM CALL :Assert 186709961001538790100634132976990 | ||
|
||
REM set "expected=true" | ||
REM set "if_success=Test passed" | ||
REM set "if_failed=Test failed: The largest and last Armstrong number." | ||
REM CALL :Assert 115132219018763992565095597973971522401 | ||
|
||
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 |