Determine the day of the week of the given date in that month. Given the weekday of the first day of the month

3.8k views Asked by At

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

2

There are 2 answers

1
shravan999 On
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 + 1
    return shift if not start_day_number + shift > 7 else shift - 7


def main():
    start_day = input()
    month_day_number = int(input())

    day = determine_day_by_number(start_day, month_day_number)
    print(day)


if __name__ == '__main__':
    main()
    
1
Fanisus On
dayseq = ["Sunday", "Monday", "Tuesday",
          "Wednesday", "Thursday", "Friday", "Saturday"]
inputday = input("Enter a day: ")  # monday
inputdate = int(input("Enter a date for the above day: "))  # 4
inputdayofdate = int(input("Enter a date to find day: "))  # 7      Thursday
step = 1


def loopers(currentday, step):
    if step == -1:
        if currentday == "Sunday":
            return "Saturday"
        return dayseq[dayseq.index(currentday)-1]
    elif step == 1:
        if currentday == "Saturday":
            return "Sunday"
        return dayseq[dayseq.index(currentday)+1]


if inputdayofdate < inputdate:
    step = -1

for i in range(inputdate, inputdayofdate, step):
    inputday = loopers(inputday, step)

print(inputday)

The above code will take 3 inputs (day date anddatetofind) and it will calculate day from the set day and date
OR

dayseq = ["Sunday", "Monday", "Tuesday",
          "Wednesday", "Thursday", "Friday", "Saturday"]
inputday = input("Enter a day: ")  # monday
inputdayofdate = int(input("Enter a date to find day: "))  # 7      Thursday

def loopers(currentday):
    if currentday == "Saturday":
        return "Sunday"
    return dayseq[dayseq.index(currentday)+1]

for i in range(inputdayofdate -1):
    inputday = loopers(inputday)

print(inputday)

The above code with consider given day as 1st of a month and continue