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

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Five weekends step by step in the Kotlin 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 Kotlin programming language

The provided Kotlin code is a program that calculates and displays information about months with 5 weekends and years with no months with 5 weekends within a specified time range.

  1. Importing Libraries: It begins by importing the necessary Java library:

    import java.util.*

    This import provides access to the GregorianCalendar class for date and time manipulation.

  2. Constants and Variables:

    • months31: An array of month numbers (1, 3, 5, 7, 8, 10, 12) that have 31 days.
    • monthsWithFive: A mutable list to store the months with 5 weekends in the desired time range.
    • yearsWithNone: A mutable list to store the years that have no months with 5 weekends in the desired time range.
  3. Main Function: The main function is the entry point of the program. It initializes a GregorianCalendar object with a start date of January 1, 1900.

  4. Loop Through Years: The code iterates through years from 1900 to 2100 using a for loop.

  5. Loop Through Months: For each year, it iterates through months from January to December using another for loop.

  6. Check for 5 Weekends: Inside the months loop, it checks if the current month has 31 days (month in months31). If it does, it proceeds to check if the first day of the month falls on a Friday (Calendar.FRIDAY == calendar[Calendar.DAY_OF_WEEK]).

    • If both conditions are met, it increments the countInYear variable, indicating that it's a month with 5 weekends.
    • It also adds the month and year in the format "MM-YYYY" to the monthsWithFive list.
  7. Update Calendar: After checking a month, it advances the calendar by one month (adding 1 to the Calendar.MONTH field).

  8. Check for Years with No Months with 5 Weekends: After completing the loop through all months in a year, it checks if the countInYear is 0. If it's 0, it means the year has no months with 5 weekends, so it adds that year to the yearsWithNone list.

  9. Printing Results: After looping through all years, it prints out the total number of months with 5 weekends, along with the first 5 and last 5 months in that list. It then prints out the total number of years with no months with 5 weekends and lists those years.

In summary, this program finds all months with 5 weekends in the specified time range and years that have no months with 5 weekends. It prints out the count and specific months/years that meet these criteria.

Source code in the kotlin programming language

// version 1.0.6

import java.util.*

fun main(args: Array<String>) {
    val calendar = GregorianCalendar(1900, 0, 1)
    val months31 = arrayOf(1, 3, 5, 7, 8, 10, 12)
    val monthsWithFive = mutableListOf<String>()
    val yearsWithNone  = mutableListOf<Int>()
    for (year in 1900..2100) {
        var countInYear = 0 //  counts months in a given year with 5 weekends
        for (month in 1..12) {
            if ((month in months31) && (Calendar.FRIDAY == calendar[Calendar.DAY_OF_WEEK])) { 
                countInYear++
                monthsWithFive.add("%02d".format(month) + "-" + year)
            }
            calendar.add(Calendar.MONTH, 1)
        }
        if (countInYear == 0) yearsWithNone.add(year)
    }
    println("There are ${monthsWithFive.size} months with 5 weekends")
    println("The first 5 are ${monthsWithFive.take(5)}")
    println("The final 5 are ${monthsWithFive.takeLast(5)}")
    println()
    println("There are ${yearsWithNone.size} years with no months which have 5 weekends, namely:")
    println(yearsWithNone)
}


  

You may also check:How to resolve the algorithm String length step by step in the C# programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm A+B step by step in the Lang programming language
You may also check:How to resolve the algorithm Unicode variable names step by step in the Swift programming language
You may also check:How to resolve the algorithm Menu step by step in the Logo programming language