Skip to content

Commit 8b1a9cb

Browse files
authored
Add leap (#72)
1 parent c486eab commit 8b1a9cb

File tree

8 files changed

+257
-0
lines changed

8 files changed

+257
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838
"prerequisites": [],
3939
"difficulty": 1
4040
},
41+
{
42+
"slug": "leap",
43+
"name": "Leap",
44+
"uuid": "9d73247b-7844-4111-9703-4afd78a4218e",
45+
"practices": [],
46+
"prerequisites": [],
47+
"difficulty": 1
48+
},
4149
{
4250
"slug": "raindrops",
4351
"name": "Raindrops",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Instructions
2+
3+
Your task is to determine whether a given year is a leap year.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Introduction
2+
3+
A leap year (in the Gregorian calendar) occurs:
4+
5+
- In every year that is evenly divisible by 4.
6+
- 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.
7+
8+
Some examples:
9+
10+
- 1997 was not a leap year as it's not divisible by 4.
11+
- 1900 was not a leap year as it's not divisible by 400.
12+
- 2000 was a leap year!
13+
14+
~~~~exercism/note
15+
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).
16+
~~~~
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@echo off
2+
setlocal enabledelayedexpansion
3+
4+
set "year=%~1"
5+
6+
set /a "mod4=%year% %% 4"
7+
set /a "mod100=%year% %% 100"
8+
set /a "mod400=%year% %% 400"
9+
10+
set "result=0"
11+
12+
if %mod4%==0 (
13+
if %mod100% neq 0 (
14+
set result=1
15+
) else (
16+
if %mod400%==0 (
17+
set result=1
18+
)
19+
)
20+
)
21+
22+
echo %result%
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"Leap.bat"
8+
],
9+
"test": [
10+
"LeapTest.bat"
11+
],
12+
"example": [
13+
".meta/Example.bat"
14+
]
15+
},
16+
"blurb": "Determine whether a given year is a leap year.",
17+
"source": "CodeRanch Cattle Drive, Assignment 3",
18+
"source_url": "https://coderanch.com/t/718816/Leap"
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[6466b30d-519c-438e-935d-388224ab5223]
13+
description = "year not divisible by 4 in common year"
14+
15+
[ac227e82-ee82-4a09-9eb6-4f84331ffdb0]
16+
description = "year divisible by 2, not divisible by 4 in common year"
17+
18+
[4fe9b84c-8e65-489e-970b-856d60b8b78e]
19+
description = "year divisible by 4, not divisible by 100 in leap year"
20+
21+
[7fc6aed7-e63c-48f5-ae05-5fe182f60a5d]
22+
description = "year divisible by 4 and 5 is still a leap year"
23+
24+
[78a7848f-9667-4192-ae53-87b30c9a02dd]
25+
description = "year divisible by 100, not divisible by 400 in common year"
26+
27+
[9d70f938-537c-40a6-ba19-f50739ce8bac]
28+
description = "year divisible by 100 but not by 3 is still not a leap year"
29+
30+
[42ee56ad-d3e6-48f1-8e3f-c84078d916fc]
31+
description = "year divisible by 400 is leap year"
32+
33+
[57902c77-6fe9-40de-8302-587b5c27121e]
34+
description = "year divisible by 400 but not by 125 is still a leap year"
35+
36+
[c30331f6-f9f6-4881-ad38-8ca8c12520c1]
37+
description = "year divisible by 200, not divisible by 400 in common year"

exercises/practice/leap/Leap.bat

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@echo off
2+
setlocal enabledelayedexpansion
3+
4+
set "year=%~1"
5+
set "result="
6+
7+
REM Your code goes here
8+
9+
10+
echo %result%

exercises/practice/leap/LeapTest.bat

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
@echo off
2+
REM ---------------------------------------------------
3+
REM Leap Unit Testing
4+
REM
5+
REM sUnit Testing Framework version: 0.2
6+
REM
7+
REM ---------------------------------------------------
8+
9+
set isTestRunner=false
10+
if "%1" == "test-runner" (
11+
set isTestRunner=true
12+
)
13+
14+
:Main
15+
REM Initalize result variable
16+
set "slug=Leap"
17+
18+
CALL :Initialize
19+
20+
REM --------------------
21+
REM Test Case Start \/\/
22+
REM Resource: https://github.com/exercism/problem-specifications/blob/main/exercises/leap/canonical-data.json
23+
REM --------------------
24+
set "expected=0"
25+
set "if_success=Test passed: year not divisible by 4 is common year."
26+
set "if_failed=Test failed: year not divisible by 4 is common year."
27+
CALL :Assert "2015"
28+
29+
set "expected=0"
30+
set "if_success=Test passed: year divisible by 2, not divisible by 4 is common year."
31+
set "if_failed=Test failed: year divisible by 2, not divisible by 4 is common year."
32+
CALL :Assert "1970"
33+
34+
set "expected=1"
35+
set "if_success=Test passed: year divisible by 4, not divisible by 100 is leap year."
36+
set "if_failed=Test failed: year divisible by 4, not divisible by 100 is leap year."
37+
CALL :Assert "1996"
38+
39+
set "expected=1"
40+
set "if_success=Test passed: year divisible by 4 and 5 is still a leap year."
41+
set "if_failed=Test failed: year divisible by 4 and 5 is still a leap year."
42+
CALL :Assert "1960"
43+
44+
set "expected=0"
45+
set "if_success=Test passed: year divisible by 100, not divisible by 400 is common year."
46+
set "if_failed=Test failed: year divisible by 100, not divisible by 400 is common year."
47+
CALL :Assert "2100"
48+
49+
set "expected=0"
50+
set "if_success=Test passed: year divisible by 100 but not by 3 is still not a leap year."
51+
set "if_failed=Test failed: year divisible by 100 but not by 3 is still not a leap year."
52+
CALL :Assert "1900"
53+
54+
set "expected=1"
55+
set "if_success=Test passed: year divisible by 400 is leap year."
56+
set "if_failed=Test failed: year divisible by 400 is leap year."
57+
CALL :Assert "2000"
58+
59+
set "expected=1"
60+
set "if_success=Test passed: year divisible by 400 but not by 125 is still a leap year."
61+
set "if_failed=Test failed: year divisible by 400 but not by 125 is still a leap year."
62+
CALL :Assert "2400"
63+
64+
set "expected=0"
65+
set "if_success=Test passed: year divisible by 200, not divisible by 400 is common year."
66+
set "if_failed=Test failed: year divisible by 200, not divisible by 400 is common year."
67+
CALL :Assert "1800"
68+
69+
REM --------------------
70+
REM Test Case End /\/\/\
71+
REM --------------------
72+
73+
CALL :ResolveStatus
74+
exit /b %errorlevel%
75+
REM End of Main
76+
77+
REM ---------------------------------------------------
78+
REM Assert [..Parameters(up to 9)]
79+
REM ---------------------------------------------------
80+
GOTO :End REM Prevents the code below from being executed
81+
:Assert
82+
set "stdout="
83+
84+
REM Run the program and capture the output then delete the file
85+
set filePath=%slug%.bat
86+
if "%isTestRunner%"=="true" (
87+
set filePath=.meta\Example.bat
88+
)
89+
set batPath=%~dp0
90+
CALL %batPath%%filePath% %~1 %~2 %~3 %~4 %~5 %~6 %~7 %~8 %~9 > stdout.bin 2>&1
91+
set /p stdout=<stdout.bin
92+
del stdout.bin
93+
94+
REM Check if the result is correct
95+
if "%stdout%" == "%expected%" (
96+
if defined if_success (
97+
echo %if_success%
98+
99+
REM Reset the variable to avoid duplicating the message.
100+
set "if_success="
101+
set "if_failed="
102+
)
103+
104+
REM If the result is correct, exit with code 0
105+
set /a successCount+=1
106+
exit /b 0
107+
) else (
108+
if defined if_failed (
109+
echo %if_failed%
110+
111+
REM Reset the variable to avoid duplicating the message.
112+
set "if_success="
113+
set "if_failed="
114+
)
115+
116+
REM If the result is incorrect, exit with code 1
117+
set /a failCount+=1
118+
exit /b 1
119+
)
120+
GOTO :EOF REM Go back to the line after the call to :Assert
121+
122+
:Initialize
123+
REM It's for initialize, not about checking empty file
124+
set "successCount=0"
125+
set "failCount=0"
126+
GOTO :EOF REM Go back to the line after the call to :CheckEmptyFile
127+
128+
:ResolveStatus
129+
set "status="
130+
if %failCount% gtr 0 (
131+
REM status: Fail
132+
REM message: The test failed.
133+
exit /b 1
134+
135+
) else (
136+
REM status: Pass
137+
exit /b 0
138+
139+
)
140+
GOTO :EOF REM Go back to the line after the call to :ExportResultAsJson
141+
142+
:End

0 commit comments

Comments
 (0)