How to resolve the algorithm The Twelve Days of Christmas step by step in the D 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 D 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 D programming language
Source code in the d programming language
immutable gifts =
"A partridge in a pear tree.
Two turtle doves
Three french hens
Four calling birds
Five golden 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";
immutable days = "first second third fourth fifth
sixth seventh eighth ninth tenth
eleventh twelfth";
void main() @safe {
import std.stdio, std.string, std.range;
foreach (immutable n, immutable day; days.split) {
auto g = gifts.splitLines.take(n + 1).retro;
writeln("On the ", day,
" day of Christmas\nMy true love gave to me:\n",
g[0 .. $ - 1].join('\n'),
(n > 0 ? " and\n" ~ g.back : g.back.capitalize), '\n');
}
}
You may also check:How to resolve the algorithm Pythagorean triples step by step in the Groovy programming language
You may also check:How to resolve the algorithm Binary digits step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Stata programming language
You may also check:How to resolve the algorithm Brazilian numbers step by step in the Delphi programming language
You may also check:How to resolve the algorithm Pick random element step by step in the Zig programming language