Day Name Given the weekday of the first day of the month, determine the day of the week of the given date in that month.
Input The first line is a string D. The second line is an integer N.
Output The output should be a string.
Explanation In the given example, D = Monday. As the 1st of the day of the month is a Monday, it means the 7th and 14th of the month will also be Mondays (A week has 7 days). So the 16th day (N = 16) of the month will be a Tuesday.
So, the output should be Tuesday.
Sample Input 1: Monday 16
Sample Output 1: Tuesday
Sample Input 2: Tuesday 17
Sample Output 2: Thursday
Approach:
def determine_day_by_number(start_day: str, month_day_number: int):
number_to_day = dict(
[[1, 'Monday'], [2, 'Tuesday'], [3, 'Wednesday'], [4, 'Thursday'], [5, 'Friday'], [6, 'Saturday'],
[7, 'Sunday']])
day_to_number = dict(zip(number_to_day.values(), number_to_day.keys()))
if start_day not in number_to_day.values():
print('Wrong day name')
exit()
start_day_number = day_to_number[start_day]
shift = get_day_shift(start_day_number, month_day_number)
number_of_required_day = start_day_number + shift
return number_to_day[number_of_required_day]
def get_day_shift(start_day_number: int, month_day_number: int):
shift = month_day_number
if start_day_number + shift > 7:
number_of_full_weeks = shift // 7
shift -= 7 * number_of_full_weeks
elif start_day_number == 1:
return start_day_number
return shift if not start_day_number + shift > 7 else shift - 7
start_day = input()
month_day_number = int(input())
day = determine_day_by_number(start_day, month_day_number)
print(day)
INPUT:
Tuesday
1
Output should be: Tuesday
My Output: Wednesday