Find the Day of the Week Project

Find the Day of the Week Challenge

Python Coding Problem

 

The goal of this Python coding problem is to write a Python program that prompts the user to enter a date in the format of “YYYY-MM-DD” and returns the corresponding day of the week. To accurately determine the day of the week for any given calendar date, implement the Christian Zeller algorithm.

Your program should:

  • Ask the user to enter a date in the specified format.
  • Validate the input to ensure it follows the correct format.
  • Implement the Christian Zeller algorithm to calculate the day of the week.
  • Display the calculated day of the week to the user.

Make sure to adhere to the following guidelines:
– Use Python 3 as the programming language.
– Preserve the original language and format of the prompt.
– Maintain the same level of technicality and formality as the original prompt.

Expected Output:

Enter a date in the format YYYY-MM-DD: 2019-03-01
The day of the week for 2019-03-01 is Friday

The Christian Zeller algorithm is a formula to calculate the day of the week for a given date. It takes into account the year, month, and day of the month to determine the day of the week. The algorithm is based on the concept of modular arithmetic and some simple calculations involving the year, month, and day.

Steps

Here are the steps and the source code to complete this Python coding problem:

1. Understand the Zeller’s formula to calculate the day of the week:

h = (q + [13(m+1)/5] + K + (K/4) + (J/4) – 2J) mod 7

Where:

  • “h” is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, …, 6 = Friday)
  • “q” is the day of the month
  • “m” is the month (3 = March, 4 = April, …, 12 = December, 1 = January, 2 = February)
  • “J” is the zero-based century (year / 100)
  • “K” is the year within the century (year \% 100)
  • mod7: operation modulo 7

2. Prompt the user to enter a date in the format YYYY-MM-DD.

3. Implement the Algorithm: Use the Christian Zeller formula to calculate the day of the week.

4. Display the Result: Map the calculated day of the week to the corresponding weekday name (e.g., 0 = Saturday, 1 = Sunday, etc.). Show the determined day of the week to the user.

Source Code

Here’s the complete source code for the Python program:

## We define the function
def find_day_of_week(year, month, day):
    if month < 3:
        month += 12
        year -= 1
    J = year // 100
    K = year % 100
    h = (day + (13*(month+1))//5 + K + K//4 + J//4 - 2*J) % 7
    return h

date_input = input("Enter a date in the format YYYY-MM-DD: ")
year, month, day = map(int, date_input.split('-'))

day_of_week = find_day_of_week(year, month, day)

weekdays = ["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
print(f"The day of the week for {date_input} is {weekdays[day_of_week]}.")

For more Python coding problems, visit Practity’s Python projects.

We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for Practice
Register New Account
Shopping cart