Last Friday of each month: Difference between revisions

Content added Content deleted
mNo edit summary
Line 2,142: Line 2,142:
=={{header|Nanoquery}}==
=={{header|Nanoquery}}==
<lang Nanoquery>import Nanoquery.Util
<lang Nanoquery>import Nanoquery.Util

// a function to check if a year is a leap year
// a function to check if a year is a leap year
def isLeapYear($year)
def isLeapYear(year)
if ($year % 100 = 0)
if (year % 100 = 0)
return ($year % 400 = 0)
return (year % 400 = 0)
else
else
return ($year % 4 = 0)
return (year % 4 = 0)
end
end
end
end

// a function to format 1-digit numbers as "0x"
// a function to format 1-digit numbers as "0x"
def format($num)
def form(num)
if ($num > 9)
if (num > 9)
return str($num)
return str(num)
else
else
return "0" + str($num)
return "0" + str(num)
end
end
end
end

// get a year from the console
// get a year from the console
print "enter year: "
print "enter year: "
$year = int(input())
year = int(input())

// build a list of the expected amount of days for each month
// 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}
$days = list(12)
append $days 31
if isLeapYear($year)
if isLeapYear($year)
append $days 29
days[1] = 29
else
append $days 28
end
end
// march - december
append $days 31 30 31 30 31 31 30 31 30 31

// loop through each month
// loop through each month
for ($month = 1) ($month <= len($days)) ($month = $month + 1)
for month in range(1, len($days))
// loop through each day of the month
// loop through each day of the month
$friday = new(Date)
friday = null
for ($day = 1) ($day <= $days[$month - 1]) ($day = $day + 1)
for day in range(1, days[month - 1])
// create a date object for this date
// create a date object for this date
$date = new(Date)
date = new(Date)
$date.setYear($year).setMonth($month).setDay($day)
date.setYear(year).setMonth(month).setDay(day)

// check if it's a friday
// check if it's a friday
if ($date.getDayOfWeek() = "Friday")
if (date.getDayOfWeek() = "Friday")
// if it is, keep it
// if it is, keep it
$friday = new(Date, $date)
friday = new(Date, date)
end
end
end for
end for

// display the last friday found
// display the last friday found
print $friday.getYear() + "-"
print friday.getYear() + "-"
print format($friday.getMonth()) + "-"
print form(friday.getMonth()) + "-"
println format($friday.getDay())
println form(friday.getDay())
end</lang>
end</lang>