How to resolve the algorithm Five weekends step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Five weekends step by step in the D programming language

Table of Contents

Problem Statement

The month of October in 2010 has five Fridays, five Saturdays, and five Sundays.

Algorithm suggestions

Extra credit Count and/or show all of the years which do not have at least one five-weekend month (there should be 29).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Five weekends step by step in the D programming language

Source code in the d programming language

import std.stdio, std.datetime, std.algorithm, std.range;

Date[] m5w(in Date start, in Date end) pure /*nothrow*/ {
    typeof(return) res;
    // adjust to 1st day
    for (Date when = Date(start.year, start.month, 1);
         when < end;
         when.add!"months"(1))
        // Such month must have 3+4*7 days and start at friday
        // for 5 FULL weekends.
        if (when.daysInMonth == 31 &&
            when.dayOfWeek == DayOfWeek.fri)
            res ~= when;
    return res;
}

bool noM5wByYear(in int year) pure {
    return m5w(Date(year, 1, 1), Date(year, 12, 31)).empty;
}

void main() {
    immutable m = m5w(Date(1900, 1, 1), Date(2100, 12, 31));
    writeln("There are ", m.length,
            " months of which the first and last five are:");
    foreach (d; m[0 .. 5] ~ m[$ - 5 .. $])
        writeln(d.toSimpleString()[0 .. $ - 3]);

    immutable n = iota(1900, 2101).filter!noM5wByYear().walkLength();
    writefln("\nThere are %d years in the range that do not have " ~
             "months with five weekends.", n);
}


void main() {
    import std.stdio, std.datetime, std.traits;

    enum first_year = 1900;
    enum last_year = 2100;

    uint totalNo5Weekends;
    immutable(Date)[] fiveWeekendMonths;
    foreach (immutable year; first_year .. last_year + 1) {
        bool has5Weekends = false;

        foreach (immutable month; EnumMembers!Month) {
            immutable firstDay = Date(year, month, 1);
            if (firstDay.daysInMonth == 31 &&
                firstDay.dayOfWeek == DayOfWeek.fri) {
                has5Weekends = true;
                fiveWeekendMonths ~= firstDay;
            }
        }

        if (!has5Weekends)
            totalNo5Weekends++;
    }

    writefln("Total 5-weekend months between %d and %d: %d",
             first_year, last_year, fiveWeekendMonths.length);
    foreach (immutable date; fiveWeekendMonths[0 .. 5])
        writeln(date.month, ' ', date.year);
    "...".writeln;
    foreach (immutable date; fiveWeekendMonths[$ - 5 .. $])
        writeln(date.month, ' ', date.year);

    writeln("\nTotal number of years with no 5-weekend months: ",
            totalNo5Weekends);
}


  

You may also check:How to resolve the algorithm Integer sequence step by step in the Ol programming language
You may also check:How to resolve the algorithm Jump anywhere step by step in the Clipper programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the OCaml programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the Common Lisp programming language