-1

1 Jan 2017 was a Sunday. Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

I was given this information in prior. I am supposed to write 1 a program to return the day (e.g. Sun, Mon, Tue, etc) in 2017, given input parameters date and month.

months = ["Jan","Feb","March", "April", "May", "June", "July", "September", "October", "November", "December"]
days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]        

def whatday(date,mth):

    mth1 = []

    day1 =[]

    counter=0

    for mths in months:
        enqueue(mth1, mths)
        for day in days:
            enqueue(day1,day)
        for counter in range(1, daysofthemonth(mths)+1):
            if counter == date and mths==mth:
                curr_day = dequeue(day1)
                break
            if counter == daysofthemonth(mths):
                dequeue(day1)
                dequeue(mth1)
                counter = 0
            if len(day1)==0:
                for day in days:
                    enqueue(day1,day)
            else:
                dequeue(day1)

    return curr_day

My code works for the first week of January but returns None onwards.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • Could you share us what `dequeue` and `enqueue` are defined as? It looks like you save the return value for some days, and don't care about it on others. – GeeTransit Jun 23 '19 at 00:50
  • 2
    Possible duplicate of [How do I get the day of week given a date in Python?](https://stackoverflow.com/questions/9847213/how-do-i-get-the-day-of-week-given-a-date-in-python). Something like `["Mon", "Tue"..."Sun"][date(2017, month, day).weekday()]` should work. – ggorlen Jun 23 '19 at 00:50

1 Answers1

0

TRY:-

import time

date = "1"
month = "1"

date_month = date + "-" + month

day_obj = time.strptime(date_month + "-2017", "%d-%m-%Y")

day = time.strftime("%a", day_obj)

print(day)

OUTPUT:-

Sun

Instead of creating a day calculator, you can leave the job to python. Use either time or datetime module for this purpose.

P.S.:- Currently the program outputs the abbreviated weekday name. If you want the full weekday name, then use %A instead of %a.

Vasu Deo.S
  • 1,820
  • 1
  • 11
  • 23