How to resolve the algorithm Long year step by step in the jq programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Long year step by step in the jq programming language
Table of Contents
Problem Statement
Most years have 52 weeks, some have 53, according to ISO8601.
Write a function which determines if a given year is long (53 weeks) or not, and demonstrate it.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Long year 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 has53weeks:
day_of_week(.; 1; 1) == 5 or day_of_week(.; 12; 31) == 5;
# To display results neatly:
def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
"Long years from 1900 to 2100 inclusive:",
([range(1900;2101) | select(has53weeks)] | nwise(10) | join(", "))
# Use jq's mktime and gmtime to produce the day of week,
# with 0 for Sunday, 1 for Monday, etc
# $year $month $day are conventional
def day_of_week_per_gmtime($year; $month; $day):
[$year, $month - 1, $day, 0, 0, 1, 0, 0] | mktime | gmtime | .[-2];
# 4 corresponds to Thursday
def has53weeks:
day_of_week_per_gmtime(.; 1; 1) == 4 or day_of_week(.; 12; 31) == 4;
def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
"Long years from 1900 to 2100 inclusive:",
([range(1900;2101) | select(has53weeks)] | nwise(10) | join(", "))
You may also check:How to resolve the algorithm SHA-256 step by step in the Frink programming language
You may also check:How to resolve the algorithm Copy a string step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the Oz programming language
You may also check:How to resolve the algorithm Circles of given radius through two points step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Count the coins step by step in the Ruby programming language