Five weekends: Difference between revisions
Content added Content deleted
(added Factor) |
(→{{header|Ruby}}: Remove nested loop; shortened somewhat.) |
||
Line 5,787: | Line 5,787: | ||
# of the month falls on a Sunday and the month has 31 days. |
# of the month falls on a Sunday and the month has 31 days. |
||
LONG_MONTHS = [1,3,5,7,8,10,12] |
|||
dates = [] |
|||
1900. |
YEARS = (1900..2100).to_a |
||
1.upto(12) do |month| |
|||
dates = YEARS.product(LONG_MONTHS).map{|y, m| Date.new(y,m,-1)}.select(&:sunday?) |
|||
d = Date.new(year, month, -1) # -1 is last day of month |
|||
dates << d if d.sunday? && d.day == 31 |
|||
end |
|||
end |
|||
puts "There are #{dates.size} months with 5 weekends from 1900 to 2100:" |
puts "There are #{dates.size} months with 5 weekends from 1900 to 2100:" |
||
puts dates.first(5).map { |
puts dates.first(5).map {|d| d.strftime("%b %Y") }, "..." |
||
⚫ | |||
puts "..." |
|||
⚫ | |||
⚫ | |||
⚫ | |||
years = (1900..2100).to_a - years_with_5w |
|||
puts "There are #{ |
puts "There are #{years_4w.size} years without months with 5 weekends:" |
||
puts |
puts years_4w.join(", ")</lang> |
||
'''Output''' |
'''Output''' |