検証プログラム
#! /usr/bin/ruby
CALENDER_ARRAY = [
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], # common year
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # leap year
]
LEAP_STR = ["no", "yes"]
DOW_STR = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
(0..1).each{|leap| # 0: common year 1: leap year
(0..6).each{|day_offset| # day of Jan 1st 0: sun ...
# make calender
calender = Array.new
week = Array.new
(1..day_offset).each{ # dummy for beginning of the year
week << 0
}
(0..11).each{|month|
(1..CALENDER_ARRAY[leap][month]).each{|date|
week << date
if 7 == week.size
calender << week
week = Array.new
end
}
}
week << 0 until 7 == week.size if 0 < week.size # dummy for year-end
calender << week
# check
(28..31).each{|date|
(0..6).each{|dow|
count = 0
calender.each{|week|
count += 1 if date == week[dow]
}
if 0 == count
printf("Leap: %3s ", LEAP_STR[leap])
printf("Day of Jan 1: %3s ", DOW_STR[day_offset])
printf("Date: %2d ", date)
printf("DOW: %3s\n", DOW_STR[dow])
end
}
}
}
}