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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm The Twelve Days of Christmas step by step in the UNIX Shell 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 UNIX Shell programming language

Source code in the unix programming language

#!/usr/bin/env bash
ordinals=(first   second third fourth fifth    sixth
          seventh eighth ninth tenth  eleventh twelfth)

gifts=( "A partridge in a pear tree." "Two turtle doves and"
        "Three French hens,"          "Four calling birds,"
        "Five gold rings,"            "Six geese a-laying,"
        "Seven swans a-swimming,"     "Eight maids a-milking,"
        "Nine ladies dancing,"        "Ten lords a-leaping,"
        "Eleven pipers piping,"       "Twelve drummers drumming," )

echo_gifts() {
  local i day=$1
  echo "On the ${ordinals[day]} day of Christmas, my true love sent to me:"
  for (( i=day; i >=0; --i )); do
    echo "${gifts[i]}"
  done
  echo
}

for (( day=0; day < 12; ++day )); do
  echo_gifts $day
done


#!/bin/sh
ordinal() {
  n=$1
  set first   second third fourth fifth    sixth \
      seventh eighth ninth tenth  eleventh twelfth
  shift $n
  echo $1
}

gift() {
  n=$1
  set "A partridge in a pear tree." "Two turtle doves and"      \
      "Three French hens,"          "Four calling birds,"       \
      "Five gold rings,"            "Six geese a-laying,"       \
      "Seven swans a-swimming,"     "Eight maids a-milking,"    \
      "Nine ladies dancing,"        "Ten lords a-leaping,"      \
      "Eleven pipers piping,"       "Twelve drummers drumming," 
  shift $n
  echo "$1"
}

echo_gifts() {
  day=$1
  echo "On the `ordinal $day` day of Christmas, my true love sent to me:"
  for i in `seq $day 0`; do
    gift $i
  done
  echo
}

for day in `seq 0 11`; do
  echo_gifts $day
done


  

You may also check:How to resolve the algorithm FizzBuzz step by step in the PostScript programming language
You may also check:How to resolve the algorithm Temperature conversion step by step in the Visual FoxPro programming language
You may also check:How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the Lua programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the Aikido programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the GML programming language