How to resolve the algorithm Last Friday of each month step by step in the Python programming language
How to resolve the algorithm Last Friday of each month step by step in the Python programming language
Table of Contents
Problem Statement
Write a program or a script that returns the date of the last Fridays of each month of a given year. The year may be given through any simple input method in your language (command line, std in, etc).
Example of an expected output:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Last Friday of each month step by step in the Python programming language
Source code in Python
The source code snippet provided contains three different implementations of a Python function to find the last Friday of each month in a given year. The function takes a year as input and prints the dates of the last Fridays in each month of that year.
First implementation:
The first implementation uses the calendar module to generate a calendar for the specified year. For each month in the year, it finds the maximum value of the days of the week that are Fridays. The maximum value represents the last Friday of the month. The code then prints the date of the last Friday for each month in the year.
Second implementation:
The second implementation uses the calendar module to generate a calendar for the specified year. It then iterates through the calendar, extracting the dates of all Fridays that fall within the specified year. The code then creates a dictionary to store the last Friday of each month. Finally, it prints the dates of the last Fridays in each month of the year.
Third implementation:
The third implementation uses the calendar module and the itertools module to generate a flat list of all the dates in the specified year. It then iterates through the list, extracting the dates of all Fridays that fall within the specified year. The code then creates a dictionary to store the last Friday of each month. Finally, it prints the dates of the last Fridays in each month of the year.
Source code in the python programming language
import calendar
def last_fridays(year):
for month in range(1, 13):
last_friday = max(week[calendar.FRIDAY]
for week in calendar.monthcalendar(year, month))
print('{:4d}-{:02d}-{:02d}'.format(year, month, last_friday))
import calendar
c=calendar.Calendar()
fridays={}
year=raw_input("year")
for item in c.yeardatescalendar(int(year)):
for i1 in item:
for i2 in i1:
for i3 in i2:
if "Fri" in i3.ctime() and year in i3.ctime():
month,day=str(i3).rsplit("-",1)
fridays[month]=day
for item in sorted((month+"-"+day for month,day in fridays.items()),
key=lambda x:int(x.split("-")[1])):
print item
import calendar
c=calendar.Calendar()
fridays={}
year=raw_input("year")
add=list.__add__
for day in reduce(add,reduce(add,reduce(add,c.yeardatescalendar(int(year))))):
if "Fri" in day.ctime() and year in day.ctime():
month,day=str(day).rsplit("-",1)
fridays[month]=day
for item in sorted((month+"-"+day for month,day in fridays.items()),
key=lambda x:int(x.split("-")[1])):
print item
import calendar
from itertools import chain
f=chain.from_iterable
c=calendar.Calendar()
fridays={}
year=raw_input("year")
add=list.__add__
for day in f(f(f(c.yeardatescalendar(int(year))))):
if "Fri" in day.ctime() and year in day.ctime():
month,day=str(day).rsplit("-",1)
fridays[month]=day
for item in sorted((month+"-"+day for month,day in fridays.items()),
key=lambda x:int(x.split("-")[1])):
print item
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Variadic function step by step in the Slate programming language
You may also check:How to resolve the algorithm Knapsack problem/Bounded step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Numerical integration step by step in the TI-89 BASIC programming language
You may also check:How to resolve the algorithm Phrase reversals step by step in the JavaScript programming language