How to resolve the algorithm Five weekends step by step in the Racket programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Five weekends step by step in the Racket programming language

Table of Contents

Problem Statement

The month of October in 2010 has five Fridays, five Saturdays, and five Sundays.

Algorithm suggestions

Extra credit Count and/or show all of the years which do not have at least one five-weekend month (there should be 29).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Five weekends step by step in the Racket programming language

Source code in the racket programming language

#lang racket
(require srfi/19)

(define long-months '(1 3 5 7 8 10 12))
(define days #(sun mon tue wed thu fri sat))

(define (week-day date)
  (vector-ref days (date-week-day date)))

(define (five-weekends-a-month start end)
  (for*/list ([year (in-range start (+ end 1))]
              [month long-months]
              [date (in-value (make-date 0 0 0 0 31 month year 0))]
              #:when (eq? (week-day date) 'sun))
    date))

(define weekends (five-weekends-a-month 1900 2100))
(define count (length weekends))

(displayln (~a "There are " count " months with five weekends."))
(displayln "The first five are: ")
(for ([w (take weekends 5)]) 
  (displayln (date->string w "~b ~Y")))
(displayln "The last five are: ")
(for ([w (drop weekends (- count 5))]) 
  (displayln (date->string w "~b ~Y")))


  

You may also check:How to resolve the algorithm Search a list step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm SOAP step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Calendar - for REAL programmers step by step in the Tcl programming language
You may also check:How to resolve the algorithm Yin and yang step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Floyd-Warshall algorithm step by step in the jq programming language