From 184a23e21ce7568250eb7bdb7adcd55f4055e9ff Mon Sep 17 00:00:00 2001 From: JS <1924550@stud.hs-mannheim.de> Date: Wed, 29 Jan 2025 10:23:46 +0100 Subject: [PATCH] finishing leap exercise --- exercises/block1/1-leap/example-input | 10 ++++++++++ exercises/block1/1-leap/example-output | 10 ++++++++++ exercises/block1/1-leap/leap.hs | 18 ++++++++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/exercises/block1/1-leap/example-input b/exercises/block1/1-leap/example-input index e69de29..e53ad42 100644 --- a/exercises/block1/1-leap/example-input +++ b/exercises/block1/1-leap/example-input @@ -0,0 +1,10 @@ +1996 +2001 +2020 +1900 +2004 +2100 +1600 +2012 +1800 +2024 \ No newline at end of file diff --git a/exercises/block1/1-leap/example-output b/exercises/block1/1-leap/example-output index e69de29..5dbefa7 100644 --- a/exercises/block1/1-leap/example-output +++ b/exercises/block1/1-leap/example-output @@ -0,0 +1,10 @@ +True +False +True +False +True +False +True +True +False +True diff --git a/exercises/block1/1-leap/leap.hs b/exercises/block1/1-leap/leap.hs index 9e5bd5e..eabde88 100644 --- a/exercises/block1/1-leap/leap.hs +++ b/exercises/block1/1-leap/leap.hs @@ -2,10 +2,24 @@ import System.IO isLeapYear :: Int -> Bool isLeapYear year -- implement here + | year `mod` 400 == 0 = True + | year `mod` 100 == 0 = False + | year `mod` 4 == 0 = True + | otherwise = False main :: IO () main = do - content <- readFile "example-input" + inputContent <- readFile "example-input" + outputContent <- readFile "example-output" - print $ isLeapYear 2000 + let years = map read (lines inputContent) :: [Int] + expectedResults = map read (lines outputContent) :: [Bool] + actualResults = map isLeapYear years + incorrect = [(y, e, a) | (y, e, a) <- zip3 years expectedResults actualResults, e /= a] + + if null incorrect + then putStrLn "All leap year checks passed successfully." + else do + putStrLn "Errors found in leap year calculation:" + mapM_ (\(y, e, a) -> putStrLn $ "Year: " ++ show y ++ ", Expected: " ++ show e ++ ", Got: " ++ show a) incorrect