Day 11 - Python

Problem Statement:

You are given a starting date in the format "dd-mm-yyyy" and an integer representing the number of days to add. Your task is to write a program to calculate the target date and determine the day of the week for that date.

Write a function calculate_target_date(start_date, num_days) where:

start_date is a string representing the starting date in the format "dd-mm-yyyy".

num_days is an integer representing the number of days to add to the starting date.

The function should return a string in the format "dd-mm-yyyy DayOfWeek" where "DayOfWeek" represents the day of the week for the target date.

Function Signature:

def calculate_target_date(start_date: str, num_days: int) -> str:

    pass

Input Format:

The first line contains a string start_date (1 <= len(start_date) <= 10^4), representing the starting date in the format "dd-mm-yyyy".

The second line contains an integer num_days (0 <= num_days <= 10^9), representing the number of days to add to the starting date.

Output Format:

A string representing the target date in the format "dd-mm-yyyy" and in the next line - DayOfWeek".

Constraints:

The input date will be a valid date in the format "dd-mm-yyyy".

The input num_days will be a non-negative integer.

Sample Input:

07-07-2024

100

Sample Output:

15-10-2024

Tuesday

Explanation:

The starting date is July 7th, 2024. Adding 100 days to this date results in October 15th, 2024. October 15th, 2024 falls on a Tuesday. Hence, the output is "15-10-2024\nTuesday".


SOLUTION:

from datetime import datetime, timedelta


def get_day_of_week(date):

    days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

    return days[date.weekday()]


startDate = input()

startDate = datetime.strptime(startDate, "%d-%m-%Y")

n = int(input())

targetDate = startDate + timedelta(days=n)

dayOfWeek = get_day_of_week(targetDate)

print(targetDate.strftime('%d-%m-%Y'))

print(dayOfWeek)


Insights:
  • The code starts with importing the datetime and timedelta classes from the datetime module. These classes are used for handling dates and time intervals, respectively.
  • There is a function defined named get_day_of_week(date). This function takes a date object as input and returns the corresponding day of the week (Monday, Tuesday, etc.) as a string.
  • The code prompts the user to input a start date in the format "dd-mm-yyyy". It then parses this input string into a datetime object using the strptime() method.
  • The code prompts the user to input an integer representing the number of days to add to the start date. It calculates the target date by adding the specified number of days (n) to the start date using the timedelta object.
  • It calls the get_day_of_week() function to determine the day of the week for the target date. The target date is printed in the format "dd-mm-yyyy" using the strftime() method.
Make the aptitude question more easier with Coding! Explore the Mathematics behind the same!
Happy Coding! :)

Comments