In: Computer Science
USE PYTHON to find out
What day of the week is a given date? i.e. On what day of the week was 5 August 1967? if it is given that 1 January 1900 was a tuesday
Write a function is_leap() which takes 1 input argument, an integer representing a year, and returns True if the year was a leap year, and False if it was not.
.Then, write a function days_since() which takes 3 input integers day, month, year, representing a date (after 1st January 1900), and returns the total number of days passed since the 1st January 1900. (For example, days_since(2, 1, 1900) should return 1. Our day_month = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
and day_month_leap = [everthing in day_month +1]
Finally, write a function week_day() which takes 3 input integers day, month, year, representing a date (after 1st January 1900), and prints the day of the week corresponding to the date
Dont use any inbuilt function, use looping
Python 3 code :
# check if its a leap year
# If a year is leap, it is divisible by 4 but not by 400
def is_leap(year):
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
return True
else:
return False
else:
return True
return False
# Checking how many days have passed since 1 January 1990
def days_since(date, month, year):
n1 = 726833 # days of 1 January 1990 from 00/00/0000
# calculating days of given date from 00/00/0000
n2 = year * 365 + date
for i in range(0, month - 1):
n2 += monthDays[i]
years = year
if month <= 2:
years -= 1
n2 += int(years / 4 - years / 100 + years / 400)
# Returning the difference of the 2 dates
return n2 - n1
# Printing the day of the week
def week_day(date, month, year):
days = days_since(date, month, year)
print(days_of_week[days % 7])
monthDays = [31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31]
days_of_week = ["Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday"]
months=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
"December"]
date,month,year=map(int,input("Enter the date in format:DD MM YYYY: ").split())
if is_leap(year):
print("It is a Leap year")
else:
print("It is not a Leap year")
print("Since 1 January 1990 it has been {} days".format(days_since(date,month,year)))
print("{} {} {} is a ".format(date,months[month-1],year),end="")
week_day(date,month,year)
Screenshot for reference:
Output Screenshot: