Task

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:

./last_fridays 2012
2012-01-27
2012-02-24
2012-03-30
2012-04-27
2012-05-25
2012-06-29
2012-07-27
2012-08-31
2012-09-28
2012-10-26
2012-11-30
2012-12-28



Related tasks
 Five weekends
 Day of the week
 Find the last Sunday of each month


import Nanoquery.Util
 
// a function to check if a year is a leap year
def isLeapYear(year)
	if (year % 100 = 0)
		return (year % 400 = 0)
	else
		return (year % 4 = 0)
	end
end
 
// a function to format 1-digit numbers as "0x"
def form(num)
	if (num > 9)
		return str(num)
	else
		return "0" + str(num)
	end
end
 
// get a year from the console
print "enter year: "
year = int(input())
 
// build a list of the expected amount of days for each month
days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
if isLeapYear($year)
	days[1] = 29
end
 
// loop through each month
for month in range(1, len($days))
	// loop through each day of the month
	friday = null
	for day in range(1, days[month - 1])
		// create a date object for this date
		date = new(Date)
		date.setYear(year).setMonth(month).setDay(day)
 
		// check if it's a friday
		if (date.getDayOfWeek() = "Friday")
			// if it is, keep it
			friday = new(Date, date)
		end
	end for
 
	// display the last friday found
	print   friday.getYear() + "-"
	print   form(friday.getMonth()) + "-"
	println form(friday.getDay())
end