Implement a function called weekend_dates with two parameters, a month m (1 m 12
ID: 3689123 • Letter: I
Question
Implement a function called weekend_dates with two parameters, a month m (1 m 12) and a year y. The function should print all the weekend (Saturday and Sunday) dates that occur in month m of year y. For example, weekend_dates(4, 2016) should print
April 2, 2016 (Saturday)
April 3, 2016 (Sunday)
April 9, 2016 (Saturday)
April 10, 2016 (Sunday)
April 16, 2016 (Saturday)
April 17, 2016 (Sunday)
April 23, 2016 (Saturday)
April 24, 2016 (Sunday)
April 30, 2016 (Saturday)
This is to be written in Python 3.5 and please use correct indentation!!! Can not use Python's datetime module!
Explanation / Answer
#Assuming that jan 1st 1800 is Wednesday
def leap(year):
if(year%400==0):
#print(year,'*');
return 1
elif(year%100==0):
#print(year,'**');
return 0
elif(year%4==0):
#print(year,'***');
return 1
#print(year,'**#');
return 0
def weekends(mon,year):
days=0
for i in range(1800,year):
if(leap(i)==1):
days+=366
#print(i,366)
else:
days+=365
#print(i,365)
fday = (3+days)%7
days_mon = [31,28,31,30,31,30,31,31,30,31,30,31]
if(leap(year)==1):
days_mon[1]=29
names = ['January','February','March','April','May','June','July','August','September','October','November','December']
for i in range(0,mon-1):
fday = fday+days_mon[i]
fday = fday%7
for i in range(1,days_mon[mon]):
if((fday+i-1)%7==0):
print(names[mon-1],i,year,'Sunday')
if((fday+i-1)%7==6):
print(names[mon-1],i,year,'Saturday')
weekends(4,2016)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.