Five weekends: Difference between revisions
→{{header|Python}}: update to Python > 3.9; PEP 8 and other minor tweaks
(→{{header|Python}}: update to Python > 3.9; PEP 8 and other minor tweaks) |
|||
Line 5,539:
=={{header|Python}}==
<syntaxhighlight lang="python">from datetime import
timedelta)
DAY
START, STOP = date(1900, 1, 1), date(2101, 1, 1)
WEEKEND = {6, 5, 4}
FMT
'Compute months with five weekends between dates'▼
when = start▼
fiveweekends = []▼
while when < stop:▼
if weekenddays >= 15:▼
fiveweekends.append(when - DAY)▼
weekenddays = 0▼
lastmonth = mon▼
if wday in WEEKEND:▼
when += DAY▼
return fiveweekends▼
def five_weekends_per_month(start: date = START,
stop: date = STOP) -> list[date]:
last_month = weekend_days = 0
if current_date.month != last_month:
last_month = current_date.month
return five_weekends
dates = five_weekends_per_month()
indent = ' '
print(
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:]))
years_without_five_weekends_months = (STOP.year - START.year
print('\nThere are %i years in the range that do not have months with five weekends'▼
print(f"\nThere are {years_without_five_weekends_months} years in the "
▲
'''Alternate Algorithm'''
The condition is equivalent to having a thirty-one day month in which the last day of the month is a Sunday.
<syntaxhighlight lang="python">LONGMONTHS = (1, 3, 5, 7, 8, 10, 12) # Jan Mar May Jul Aug Oct Dec
def five_weekends_per_month2(start: date = START,
if date(yr, month, 31).timetuple()[6] == 6 # Sunday▼
for month in LONG_MONTHS
dates2 =
assert dates2 == dates</syntaxhighlight>
|