TopCoder problem "SpecialDay" used in TCHS SRM 32 (Division I Level Two)



Problem Statement

    In the United States, some holidays fall on the n-th weekday of a month rather than on the same date each year. For example, Thanksgiving is the fourth Thursday in November, Labor Day is the first Monday in September, Mother's Day is the second Sunday in May, etc. You are given a String month, an int which, and a String weekday that specify one such holiday. The holiday lands on the which-th weekday of the month (which is a 1-based index).



You will also be given an int day. Your birthday happens to be on the day-th day of the same month (day is a 1-based index). Return the number of times your birthday coincides with the given holiday between the year 2000 and the year 2100, inclusive.



The weekdays, in order, are: "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY".



The months of the year, in order are: "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER".
 

Definition

    
Class:SpecialDay
Method:howMany
Parameters:String, int, String, int
Returns:int
Method signature:int howMany(String weekday, int which, String month, int day)
(be sure your method is public)
    
 

Notes

-January 1st, 2000 is on a Saturday.
-January, March, May, July, August, October and December have 31 days.
-April, June, September and November have 30 days.
-February has 28 days unless it is a leap year wherein it has 29 days.
-A leap year is a year that is divisible by 4, unless it is also divisible by 100 and not 400.
 

Constraints

-day will be between 1 and the number of days in month, inclusive.
-which will be between 1 and 5, inclusive.
-weekday will be one of the following: {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"}
-month will be one of the following: {"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"}
 

Examples

0)
    
"THURSDAY"
4
"NOVEMBER"
23
Returns: 14
Here we are asked to calculate the number of times a person born on November 23rd has a birthday on Thanksgiving. When the 4th THURSDAY of NOVEMBER is also the 23rd of NOVEMBER it will be one of the following years:



2000, 2006, 2017, 2023, 2028, 2034, 2045, 2051, 2056, 2062, 2073, 2079, 2084 or 2090.
1)
    
"SUNDAY"
2
"MAY"
13
Returns: 15
Here we are asked to calculate the number of times a person born on May 13th has a birthday on Mother's Day. When the 2nd SUNDAY of MAY is also the 13th of MAY it will be one of the following years:



2001, 2007, 2012, 2018, 2029, 2035, 2040, 2046, 2057, 2063, 2068, 2074, 2085, 2091 or 2096.
2)
    
"TUESDAY"
5
"FEBRUARY"
29
Returns: 4
Watch out for leap years!
3)
    
"MONDAY"
2
"OCTOBER"
6
Returns: 0
This scenario is impossible as the 2nd MONDAY cannot be on the 6th day of the month. It can only be on the 8th through the 14th, inclusive.
4)
    
"MONDAY"
5
"FEBRUARY"
29
Returns: 3

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=7428

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10656&pm=7428

Writer:

Uranium-235

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Simulation