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

Source code in the common programming language

(defun friday-before (year month day)
 (let* 
  ((timestamp (encode-universal-time 0 0 12 day month year))
   (weekday (nth 6 (multiple-value-list (decode-universal-time timestamp))))
   (fri (- timestamp (* (+ (mod (+ weekday 2) 7) 1) 86400))))
    (multiple-value-bind (_ _ _ d m y) (decode-universal-time fri)
     (list y m d))))
    
(defun last-fridays (year) 
  (append (loop for month from 2 to 12 collecting (friday-before year month 1))
          (list (friday-before (1+ year) 1 1))))

(let* ((year (read-from-string (car *args*))))
  (format t "~{~{~a-~2,'0d-~2,'0d~}~%~}" (last-fridays year)))


  

You may also check:How to resolve the algorithm Execute a Markov algorithm step by step in the APL programming language
You may also check:How to resolve the algorithm Almost prime step by step in the Maple programming language
You may also check:How to resolve the algorithm Abbreviations, easy step by step in the Clojure programming language
You may also check:How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the Vala programming language