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

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Five weekends step by step in the jq 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 jq programming language

Source code in the jq programming language

 Use Zeller's Congruence to determine the day of the week, given
# year, month and day as integers in the conventional way.
# Emit 0 for Saturday, 1 for Sunday, etc.
#
def day_of_week(year; month; day):
  if month == 1 or month == 2 then
    [month + 12, year - 1]
  else
    [month, year]
  end 
  | day + (13*(.[0] + 1)/5|floor)
    +  (.[1]%100)       + ((.[1]%100)/4|floor)
    +  (.[1]/400|floor) - 2*(.[1]/100|floor) 
  | . % 7
;

def weekday_of_last_day_of_month(year; month):
  def day_before(day): (6+day) % 7;

  if month==12 then day_before( day_of_week(year+1; 1; 1) )
  else day_before( day_of_week( year; month+1; 1 ) )
  end
;
  
# The only case where the month has 5 weekends is when the last day 
# of the month falls on a Sunday and the month has 31 days.
#
def five_weekends(from; to):
  reduce range(from; to) as $year
    ([]; reduce (1,3,5,7,8,10,12) as $month  # months with 31 days
      (.;
       weekday_of_last_day_of_month($year; $month) as $day
       | if $day == 1 then . + [[ $year, $month]] else . end ))
;

# Input [year, month] as conventional integers; print e.g. "Jan 2001"
def pp:
  def month:
    ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][.-1];
   "\(.[1] | month) \(.[0])"
;

five_weekends(1900;2101) 
| "There are \(length) months with 5 weekends from 1900 to 2100 inclusive;",
  "the first and last five are as follows:",
  ( .[0: 5][] | pp),
  "...",
  ( .[length-5: ][] | pp),
  "In this period, there are \( [range(1900;2101)] - map( .[0] ) | length ) years which have no five-weekend months."

$ jq -r -n -f Five_Weekends.jq
There are 201 months with 5 weekends from 1900 to 2100 inclusive;
the first and last five are as follows:
Mar 1901
Aug 1902
May 1903
Jan 1904
Jul 1904
...
Mar 2097
Aug 2098
May 2099
Jan 2100
Oct 2100
In this period, there are 29 years which have no five-weekend months.


  

You may also check:How to resolve the algorithm Inheritance/Single step by step in the Factor programming language
You may also check:How to resolve the algorithm I before E except after C step by step in the Fortran programming language
You may also check:How to resolve the algorithm Factors of a Mersenne number step by step in the Delphi programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the Scheme programming language
You may also check:How to resolve the algorithm Count in factors step by step in the EchoLisp programming language