How to resolve the algorithm Calendar step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Calendar step by step in the Raku programming language

Table of Contents

Problem Statement

Create a routine that will generate a text calendar for any year.
Test the calendar by generating a calendar for the year 1969, on a device of the time. Choose one of the following devices:

(Ideally, the program will generate well-formatted calendars for any page width from 20 characters up.) Kudos (κῦδος) for routines that also transition from Julian to Gregorian calendar. This task is inspired by Real Programmers Don't Use PASCAL by Ed Post, Datamation, volume 29 number 7, July 1983. For further Kudos see task CALENDAR, where all code is to be in UPPERCASE. For economy of size, do not actually include Snoopy generation in either the code or the output, instead just output a place-holder.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Calendar step by step in the Raku programming language

Source code in the raku programming language

my $months-per-row = 3;
my @weekday-names  = ;
my @month-names    = ;

my Int() $year = @*ARGS.shift || 1969;
say fmt-year($year);

sub fmt-year ($year) {
    my @month-strs;
    @month-strs[$_] = [fmt-month($year, $_).lines] for 1 .. 12;
    my @C = ' ' x 30 ~ $year, '';
    for 1, 1+$months-per-row ... 12 -> $month {
        while @month-strs[$month] {
            for ^$months-per-row -> $column {
                @C[*-1] ~= @month-strs[$month+$column].shift ~ ' ' x 3 if @month-strs[$month+$column];
            }
            @C.push: '';
        }
        @C.push: '';
    }
    @C.join: "\n";
}

sub fmt-month ($year, $month) {
    my $date = Date.new($year,$month,1);
    @month-names[$month-1].fmt("%-20s\n") ~ @weekday-names ~ "\n" ~
    (('  ' xx $date.day-of-week - 1), (1..$date.days-in-month)».fmt('%2d')).flat.rotor(7, :partial).join("\n") ~
    (' ' if $_ < 7) ~ ('  ' xx 7-$_).join(' ') given Date.new($year, $month, $date.days-in-month).day-of-week;
}


  

You may also check:How to resolve the algorithm Taxicab numbers step by step in the C programming language
You may also check:How to resolve the algorithm Guess the number step by step in the J programming language
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the D programming language
You may also check:How to resolve the algorithm Averages/Mean time of day step by step in the Raku programming language
You may also check:How to resolve the algorithm Spiral matrix step by step in the Ursala programming language