Python: Find the years where 25th of December be a Sunday between 2000 and 2150
Python Itertools: Exercise-21 with Solution
Write a Python program to find the years where 25th of December be a Sunday between 2000 and 2150.
Sample Solution:
Python Code:
'''Days of the week'''
# Source:https://bit.ly/30NoXF8
from datetime import date
from itertools import islice
# xmasIsSunday :: Int -> Bool
def xmasIsSunday(y):
'''True if Dec 25 in the given year is a Sunday.'''
return 6 == date(y, 12, 25).weekday()
# main :: IO ()
def main():
'''Years between 2000 and 2150 with 25 December on a Sunday'''
xs = list(filter(
xmasIsSunday,
enumFromTo(2000)(2150)
))
total = len(xs)
print(
fTable(main.__doc__ + ':\n\n' + '(Total ' + str(total) + ')\n')(
lambda i: str(1 + i)
)(str)(index(xs))(
enumFromTo(0)(total - 1)
)
)
# GENERIC -------------------------------------------------
# enumFromTo :: (Int, Int) -> [Int]
def enumFromTo(m):
'''Integer enumeration from m to n.'''
return lambda n: list(range(m, 1 + n))
# index (!!) :: [a] -> Int -> a
def index(xs):
'''Item at given (zero-based) index.'''
return lambda n: None if 0 > n else (
xs[n] if (
hasattr(xs, "__getitem__")
) else next(islice(xs, n, None))
)
# unlines :: [String] -> String
def unlines(xs):
'''A single string formed by the intercalation
of a list of strings with the newline character.
'''
return '\n'.join(xs)
# FORMATTING ---------------------------------------------
# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
'''Heading -> x display function -> fx display function ->
f -> xs -> tabular string.
'''
def go(xShow, fxShow, f, xs):
ys = [xShow(x) for x in xs]
w = max(map(len, ys))
return s + '\n' + '\n'.join(map(
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
xs, ys
))
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
xShow, fxShow, f, xs
)
# MAIN --
if __name__ =='__main__':
main()
Sample Output:
Years between 2000 and 2150 with 25 December on a Sunday: (Total 22) 1 -> 2005 2 -> 2011 3 -> 2016 4 -> 2022 5 -> 2033 6 -> 2039 7 -> 2044 8 -> 2050 9 -> 2061 10 -> 2067 11 -> 2072 12 -> 2078 13 -> 2089 14 -> 2095 15 -> 2101 16 -> 2107 17 -> 2112 18 -> 2118 19 -> 2129 20 -> 2135 21 -> 2140 22 -> 2146
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to find the factorial of a number using itertools module.
Next: Write a Python program to create a 24-hour time format (HH:MM ) using 4 given digits. Display the latest time and do not use any digit more than once.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework