How to print every month in a specified year that contains five full weekends (Fri, Sat, Sun) in Ruby

1 Answer

0 votes
# A month has five full weekends when it has 31 days,
# and the 1st day of the month is a Friday.

require "date"

# ------------------------------------------------------------
# Month names as a single reusable constant
# ------------------------------------------------------------
MONTH_NAMES = [
  "January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
]

# ------------------------------------------------------------
# Function: returns true if a month has 5 full weekends
# ------------------------------------------------------------
def has_five_full_weekends(year, month)
  first_day = Date.new(year, month, 1)

  days_in_month = (first_day.next_month - 1).day
  weekday = first_day.wday  # Sunday=0 ... Friday=5 ... Saturday=6

  days_in_month == 31 && weekday == 5  # Friday
end

# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
def main
  y_value = 2026

  (1..12).each do |m|
    if has_five_full_weekends(y_value, m)
      puts "#{MONTH_NAMES[m - 1]} #{y_value} has five full weekends."
    end
  end
end

main



=begin
run:

May 2026 has five full weekends.
=end

 



answered May 27 by avibootz

Related questions

...