Return a boolean true or false based on whether the given year is a leap year.
The given integer may be positive or negative.
Keep In Mind
it is a leap year on every year that is evenly divisible by 4
except every year that is evenly divisible by 100
unless the year is also evenly divisible by 400
Examples:
(2017) => false
(2016) => true
(1992) => true
(1066) => false
(-7) => false
*^You can assume that you will always recieved valid input
I like BrianDGLS's 29 character solution very much for it's mathematical cleverness, although I have to get used to implicit type conversion. :-) It wouldn't be that short in Haskell with its strong typing (no implicit type conversions) and long keywords. The standard solution perhaps would read in Haskell:
Language: Haskell
Length: 42
Solution:
Anyway, I had the most fun solving it with a little 3 step implication checker:
The idea is the following chain of implications which is true for year x if and only if x is the number of a leap year:
Because Bool implements an ordering of
False < True
implications can be checked by comparisons:Leap years can therefore be characterised with the following algorithm:
mod<$>[2017]<*>[100,400,1,4] -- -> [17,17,0,1]
(using lists as applicative functor, expressing 4 computations "in one", gives this solution it's beauty, doesn't it)map(==0)[17,17,0,1] -- -> [False,False,True,False]
(\l -> sort l == l) [False,False,True,False] -- -> False
or nicer and shorter with functions as applicative functor:((==)<*>sort) [False,False,True,False] -- -> False
That's it, a an implication checker for uniform predicate functions. Using lists as applicative functors like that is helpful everytime a problem has a number of uniform mathematical constraints. The sort function needs to be imported from Data.List, so it's a solution for the lambdabot.
Language: Haskell (Lambdabot)
Length: 50
Solution: (my favourite of those in Haskell)
Mapping is enough here, saving 3 chars: Length 47
Without the sort function, step 3 is more tricky. A little too tricky to be really nice, I think, but hey, uncurrying the comparison function in order to apply it to zipped tuples of consecutive list elements!
Language: Haskell
Length: 66
Solution: