How to resolve the algorithm The Twelve Days of Christmas step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm The Twelve Days of Christmas step by step in the jq programming language

Table of Contents

Problem Statement

Write a program that outputs the lyrics of the Christmas carol The Twelve Days of Christmas. The lyrics can be found here.
(You must reproduce the words in the correct order, but case, format, and punctuation are left to your discretion.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm The Twelve Days of Christmas step by step in the jq programming language

Source code in the jq programming language

[ "one", "two", "three", "four", "five", "six",
    "seven", "eight", "nine", "ten", "eleven", "twelve"] as $cardinals
| [ "first", "second", "third", "fourth", "fifth", "sixth",
    "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"] as $ordinals
| [ "a partridge in a pear tree.", "turtle doves", "French hens",
    "calling birds", "gold rings", "geese a-laying", "swans a-swimming",
    "maids a-milking", "ladies dancing", "lords a-leaping", "pipers piping",
    "drummers drumming" ] as $gifts
| range(12) | . as $i | $ordinals[$i] as $nth
| "On the " + $nth + " day of Christmas, my true love sent to me:\n" +
  ([[range($i+1)]|reverse[]|. as $j|$cardinals[$j] as $count|
     if $j > 0 then $count else if $i > 0 then "and" else "" end end + 
      " " + $gifts[$j] + if $j > 0 then "," else "\n" end]
  | join("\n"))

  

You may also check:How to resolve the algorithm Word wrap step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Compiler/lexical analyzer step by step in the Elixir programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Relation programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Idris programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Seed7 programming language