Skip to content

Commit

Permalink
Exercise/grains (#26)
Browse files Browse the repository at this point in the history
* new exercise - series

* new exercise - series [no important files changed]

* new exercise - grains
  • Loading branch information
GroophyLifefor committed Apr 9, 2024
1 parent e9ec01c commit 49e040f
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@
"strings"
],
"difficulty": 2
},
{
"slug": "grains",
"name": "Grains",
"uuid": "5a7b87cf-09f4-4430-b08b-af572ce00c07",
"practices": [],
"prerequisites": [],
"difficulty": 1
}
]
},
Expand Down
15 changes: 15 additions & 0 deletions exercises/practice/grains/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Description

Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.

There once was a wise servant who saved the life of a prince.
The king promised to pay whatever the servant could dream up.
Knowing that the king loved chess, the servant told the king he would like to have grains of wheat.
One grain on the first square of a chess board, with the number of grains doubling on each successive square.

There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).

Write code that shows:

- how many grains were on a given square, and
- the total number of grains on the chessboard
21 changes: 21 additions & 0 deletions exercises/practice/grains/.meta/Example.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@echo off
setlocal EnableDelayedExpansion

set "input=%~1"

if %input% LSS 1 (
echo square must be between 1 and 64
exit /b 1
)

if %input% GTR 64 (
echo square must be between 1 and 64
exit /b 1
)

set /a "grains=1"
for /l %%i in (2,1,%input%) do (
set /a "grains*=2"
)

echo %grains%
16 changes: 16 additions & 0 deletions exercises/practice/grains/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"authors": ["GroophyLifefor"],
"files": {
"solution": [
"Grains.bat"
],
"test": [
"GrainsTest.bat"
],
"example": [
".meta/Example.bat"
]
},
"blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.",
"source": "The CodeRanch Cattle Drive, Assignment 6"
}
13 changes: 13 additions & 0 deletions exercises/practice/grains/.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/grains/Grains.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@echo off
setlocal EnableDelayedExpansion

set "input=%~1"
set "grains="

REM Your code goes here


echo %grains%
130 changes: 130 additions & 0 deletions exercises/practice/grains/GrainsTest.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
@echo off
REM ---------------------------------------------------
REM Grains Unit Testing
REM ---------------------------------------------------

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

CALL :Initialize

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

set "expected=2"
set "if_success=Test passed"
set "if_failed=Test failed: grains on square 2."
CALL :Assert "2"

set "expected=4"
set "if_success=Test passed"
set "if_failed=Test failed: grains on square 3."
CALL :Assert "3"

set "expected=8"
set "if_success=Test passed"
set "if_failed=Test failed: grains on square 4."
CALL :Assert "4"

set "expected=32768"
set "if_success=Test passed"
set "if_failed=Test failed: grains on square 16."
CALL :Assert "16"

REM -!!!- Batch supports only up to 31 bits, so the maximum value is 2^31-1
set "expected=1073741824"
set "if_success=Test passed"
set "if_failed=Test failed: grains on square 31."
CALL :Assert "31"

set "expected=square must be between 1 and 64"
set "if_success=Test passed"
set "if_failed=Test failed: square 0 is invalid."
CALL :Assert "0"

set "expected=square must be between 1 and 64"
set "if_success=Test passed"
set "if_failed=Test failed: negative square is invalid."
CALL :Assert "-1"

set "expected=square must be between 1 and 64"
set "if_success=Test passed"
set "if_failed=Test failed: square greater than 64 is invalid."
CALL :Assert "65"

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 49e040f

Please sign in to comment.