How to resolve the algorithm Last Friday of each month step by step in the Nanoquery programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Last Friday of each month step by step in the Nanoquery 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 Nanoquery programming language

Source code in the nanoquery programming language

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

  

You may also check:How to resolve the algorithm Multifactorial step by step in the zkl programming language
You may also check:How to resolve the algorithm Kaprekar numbers step by step in the CLU programming language
You may also check:How to resolve the algorithm Comments step by step in the Batch File programming language
You may also check:How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the Clojure programming language
You may also check:How to resolve the algorithm Calendar - for REAL programmers step by step in the BASIC programming language