Five weekends: Difference between revisions

(Modified task description to make it easier to compare entries for correctness.)
Line 51:
1-2100
10-2100</pre>
 
=={{header|Python}}==
<lang python>from datetime import timedelta, date
 
DAY = timedelta(days=1)
WEEKEND = {6, 5, 4} # Sunday is day 6
FMT = '%Y %m(%B)'
 
def fiveweekendspermonth(start=date(1900, 1, 1), stop=date(2101, 1, 1)):
'Compute months with five weekends between dates'
when = start
lastmonth = weekenddays = 0
fiveweekends = []
while when < stop:
year, mon, _mday, _h, _m, _s, wday, _yday, _isdst = when.timetuple()
if mon != lastmonth:
if weekenddays >= 15:
fiveweekends.append(when - DAY)
weekenddays = 0
lastmonth = mon
if wday in WEEKEND:
weekenddays += 1
when += DAY
return fiveweekends
 
dates = fiveweekendspermonth()
indent = ' '
print('There are %s months of which the first and last five are:' % len(dates))
print(indent +('\n'+indent).join(d.strftime(FMT) for d in dates[:5]))
print(indent +'...')
print(indent +('\n'+indent).join(d.strftime(FMT) for d in dates[-5:]))</lang>
 
'''Sample Output'''
<pre>There are 201 months of which the first and last five are:
1901 03(March)
1902 08(August)
1903 05(May)
1904 01(January)
1904 07(July)
...
2097 03(March)
2098 08(August)
2099 05(May)
2100 01(January)
2100 10(October)</pre>
Anonymous user