Given a year as input, write a Python to find the starting day of the given year. We will use the Python datetime library in order to solve this problem.
Example:
Input : 2010
Output :Friday
Input :2019
Output :Tuesday
Below is the implementation:
OutputStarting day of 2010 is Friday.
Time Complexity: O(1)
Space Complexity: O(1)
Alternate Approach Using the calendar library
step by step approach
- Import the calendar library.
- Define a function Startingday(year) that takes a single argument year.
- Create a list called Days that contains the names of the days of the week in order, starting with
- Monday and ending with Sunday.
- Call the calendar.weekday(year, 1, 1) function, passing in the year argument and the values 1 and 1 for the month and day arguments. This function returns an integer representing the day of the week for the given date, where Monday is 0 and Sunday is 6.
- Use the integer returned by calendar.weekday() to index into the Days list and get the name of the starting day of the year.
- Print a message that includes the year argument and the name of the starting day of the year.
- Define a variable year with the value 2019.
- Call the Startingday(year) function, passing in the year argument.
- The program outputs a message indicating the starting day of the year 2019, which is a Tuesday.
- The author of the code is credited in a comment at the end of the program.