Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add leap #72

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "leap",
"name": "Leap",
"uuid": "9d73247b-7844-4111-9703-4afd78a4218e",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "raindrops",
"name": "Raindrops",
Expand Down
3 changes: 3 additions & 0 deletions exercises/practice/leap/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Instructions

Your task is to determine whether a given year is a leap year.
16 changes: 16 additions & 0 deletions exercises/practice/leap/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Introduction

A leap year (in the Gregorian calendar) occurs:

- In every year that is evenly divisible by 4.
- Unless the year is evenly divisible by 100, in which case it's only a leap year if the year is also evenly divisible by 400.

Some examples:

- 1997 was not a leap year as it's not divisible by 4.
- 1900 was not a leap year as it's not divisible by 400.
- 2000 was a leap year!

~~~~exercism/note
For a delightful, four-minute explanation of the whole phenomenon of leap years, check out [this YouTube video](https://www.youtube.com/watch?v=xX96xng7sAE).
~~~~
22 changes: 22 additions & 0 deletions exercises/practice/leap/.meta/Example.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@echo off
setlocal enabledelayedexpansion

set "year=%~1"

set /a "mod4=%year% %% 4"
set /a "mod100=%year% %% 100"
set /a "mod400=%year% %% 400"

set "result=0"

if %mod4%==0 (
if %mod100% neq 0 (
set result=1
) else (
if %mod400%==0 (
set result=1
)
)
)

echo %result%
19 changes: 19 additions & 0 deletions exercises/practice/leap/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"Leap.bat"
],
"test": [
"LeapTest.bat"
],
"example": [
".meta/Example.bat"
]
},
"blurb": "Determine whether a given year is a leap year.",
"source": "CodeRanch Cattle Drive, Assignment 3",
"source_url": "https://coderanch.com/t/718816/Leap"
}
37 changes: 37 additions & 0 deletions exercises/practice/leap/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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.

[6466b30d-519c-438e-935d-388224ab5223]
description = "year not divisible by 4 in common year"

[ac227e82-ee82-4a09-9eb6-4f84331ffdb0]
description = "year divisible by 2, not divisible by 4 in common year"

[4fe9b84c-8e65-489e-970b-856d60b8b78e]
description = "year divisible by 4, not divisible by 100 in leap year"

[7fc6aed7-e63c-48f5-ae05-5fe182f60a5d]
description = "year divisible by 4 and 5 is still a leap year"

[78a7848f-9667-4192-ae53-87b30c9a02dd]
description = "year divisible by 100, not divisible by 400 in common year"

[9d70f938-537c-40a6-ba19-f50739ce8bac]
description = "year divisible by 100 but not by 3 is still not a leap year"

[42ee56ad-d3e6-48f1-8e3f-c84078d916fc]
description = "year divisible by 400 is leap year"

[57902c77-6fe9-40de-8302-587b5c27121e]
description = "year divisible by 400 but not by 125 is still a leap year"

[c30331f6-f9f6-4881-ad38-8ca8c12520c1]
description = "year divisible by 200, not divisible by 400 in common year"
10 changes: 10 additions & 0 deletions exercises/practice/leap/Leap.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@echo off
setlocal enabledelayedexpansion

set "year=%~1"
set "result="

Comment on lines +4 to +6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a practice exercise, the tests are meant to be read, can these three lines be gone?

If not, is line 5 valid? If not, we should note deliver code that is inherently broken on its own to students. We might assign it to a number, or we might assign it to a place holder string such as something that says "replace this with a good default value", but it should not be broken on its own, delivered.

Copy link
Member Author

@BNAndras BNAndras Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree about line 5, but I used the darts implementation here as a guide. That’s how the maintainer did it there. The only change I did was to show the test name for passing tests since the test name is shown only on failure for the other exercises.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not really feel strongly about it either way, but if these are practice I have been noticing some students taking some of the "scaffolding" that is sometimes delivered as "given" and "not to be touched". And unless told explicitly otherwise, I would want the practice exercises to provide all the areas to practice, where it is not something that "Must be done and this way and even by name, generally."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To the extent even that the "default year" is set? What is a default year, and how did we choose that?"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not following about the default. year is being set to the input for each test being checked. lt’s above the line telling the student to add their code so students just need to know that variable contains the test input. Would it address some of the potential confusion for me to rename that variable as input?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @kotp, may I don't get what is wrong, 'year' is a parameter and %~1 means it's the first parameter, this exercise takes one parameter so we don't need to add the second parameter, and the result added to that can be updated so at the end of the program that will be printed and unit tests able to see the output. In the code part set "result=" is not affecting program, that means, at the start we don't have a variable that results, and set "result=" removes the variable from memory, so can't remove a variable that doesn't initialize. In the batch file, we no need to initialize variables, when you try to assign, if not has that variable, creates one of them, so open to manipulation at the center of code.

REM Your code goes here


echo %result%
142 changes: 142 additions & 0 deletions exercises/practice/leap/LeapTest.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
@echo off
REM ---------------------------------------------------
REM Leap Unit Testing
REM
REM sUnit Testing Framework version: 0.2
REM
REM ---------------------------------------------------

set isTestRunner=false
if "%1" == "test-runner" (
set isTestRunner=true
)

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

CALL :Initialize

REM --------------------
REM Test Case Start \/\/
REM Resource: https://github.com/exercism/problem-specifications/blob/main/exercises/leap/canonical-data.json
REM --------------------
set "expected=0"
set "if_success=Test passed: year not divisible by 4 is common year."
set "if_failed=Test failed: year not divisible by 4 is common year."
CALL :Assert "2015"

set "expected=0"
set "if_success=Test passed: year divisible by 2, not divisible by 4 is common year."
set "if_failed=Test failed: year divisible by 2, not divisible by 4 is common year."
CALL :Assert "1970"

set "expected=1"
set "if_success=Test passed: year divisible by 4, not divisible by 100 is leap year."
set "if_failed=Test failed: year divisible by 4, not divisible by 100 is leap year."
CALL :Assert "1996"

set "expected=1"
set "if_success=Test passed: year divisible by 4 and 5 is still a leap year."
set "if_failed=Test failed: year divisible by 4 and 5 is still a leap year."
CALL :Assert "1960"

set "expected=0"
set "if_success=Test passed: year divisible by 100, not divisible by 400 is common year."
set "if_failed=Test failed: year divisible by 100, not divisible by 400 is common year."
CALL :Assert "2100"

set "expected=0"
set "if_success=Test passed: year divisible by 100 but not by 3 is still not a leap year."
set "if_failed=Test failed: year divisible by 100 but not by 3 is still not a leap year."
CALL :Assert "1900"

set "expected=1"
set "if_success=Test passed: year divisible by 400 is leap year."
set "if_failed=Test failed: year divisible by 400 is leap year."
CALL :Assert "2000"

set "expected=1"
set "if_success=Test passed: year divisible by 400 but not by 125 is still a leap year."
set "if_failed=Test failed: year divisible by 400 but not by 125 is still a leap year."
CALL :Assert "2400"

set "expected=0"
set "if_success=Test passed: year divisible by 200, not divisible by 400 is common year."
set "if_failed=Test failed: year divisible by 200, not divisible by 400 is common year."
CALL :Assert "1800"

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
set filePath=%slug%.bat
if "%isTestRunner%"=="true" (
set filePath=.meta\Example.bat
)
set batPath=%~dp0
CALL %batPath%%filePath% %~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