How to resolve the algorithm Five weekends step by step in the Gambas programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Five weekends step by step in the Gambas 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 Gambas programming language
Source code in the gambas programming language
Public Sub Main()
Dim aMonth As Short[] = [1, 3, 5, 7, 8, 10, 12] 'All 31 day months
Dim aMMStore As New String[] 'To store results
Dim siYear, siMonth, siCount As Short 'Various variables
Dim dDay As Date 'To store the day to check
Dim sTemp As String 'Temp string
For siYear = 1900 To 2100 'Loop through each year
For siMonth = 0 To 6 'Loop through each 31 day month
dDay = Date(siYear, aMonth[siMonth], 1) 'Get the date of the 1st of the month
If WeekDay(dDay) = 5 Then aMMStore.Add(Format(dDay, "mmmm yyyy")) 'If the 1st is a Friday then store the result
Next
Next
For Each sTemp In aMMStore 'For each item in the stored array..
Inc siCount 'Increase siCount
If siCount < 6 Then Print aMMStore[siCount] 'If 1 of the 1st 5 dates then print it
If siCount = 6 Then Print String$(14, "-") 'Print a separator
If siCount > aMMStore.Max - 4 Then Print aMMStore[siCount - 1] 'If 1 of the last 5 dates then print it
Next
Print gb.NewLine & "Total months = " & Str(siCount) 'Print the number of months found
siCount = 0 'Reset siCount
sTemp = aMMStore.Join(",") 'Put all the stored dates in one string joined by commas
aMMStore.Clear 'Clear the store for reuse
For siYear = 1900 To 2100 'Loop through each year
If Not InStr(sTemp, Str(siYear)) Then 'If the year is not in the stored string then..
Inc siCount 'Increase siCount (Amount of years that don't have 5 weekend months)
aMMStore.Add(Str(siYear)) 'Add to the store
End If
Next
Print gb.NewLine & "There are " & Str(siCount) &
" years that do not have at least one five-weekend month" 'Print the amount of years with no 5 weekend months
Print aMMStore.Join(",") 'Print the years with no 5 weekend months
End
You may also check:How to resolve the algorithm Idiomatically determine all the characters that can be used for symbols step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Aliquot sequence classifications step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Anagrams step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Primality by Wilson's theorem step by step in the XPL0 programming language