Problem Statement
According to the International Organization for Standardization (ISO), the first calendar week of a year is the one that includes the first Thursday of that year, and the last calendar week of a year is the week immediately preceding the first calendar week of the next year. It follows from this definition that some years have 52 calendar weeks, while others have 53. The calendar weeks are numbered in succession from 1 to 52 or 53, as the case may be. Each week begins on a Monday and ends on a Sunday. Observe that a week may spill over from one year into another. For example, if January 1 of some year is a Wednesday, then ISO week 1 of that year includes the last Monday and Tuesday of the previous year. Similarly, if December 31 of some year is a Saturday, then the last ISO week of that year includes the first Sunday of the following year.
The months of April, June, September, and November are 30 days long. The others have 31 days, with the exception of February, which has 29 days in leap years and 28 days otherwise. A leap year is one that is divisible by 4, except if it is divisible by 100 and not divisible by 400. For example, 1996 and 2000 are leap years, but 2099 and 2100 are not. These rules were introduced by Pope Gregory XIII (hence the name "Gregorian calendar") on October 15, 1582, and are part of the calendar standard promulgated today by the ISO.
Mathematicians and programmers have been familiar with this standard for some time, and have developed efficient methods to calculate ISO week numbers. It is rumored, however, that all the library routines are about to become obsolete. Your newspaper's gossip column states that the ISO council, at its next annual congress in Geneva, will announce a three-day shift in the mapping of dates to weekdays, effective retroactively and into the future. Under this ruling, September 8, 2003 ceases to be a Monday and will henceforth be a Thursday, with all other dates remapped to agree with the order of the weekdays. The ISO standard will remain unchanged in every other respect.
Given three
Definition
- Class:
- CalendarISO
- Method:
- weekNumber
- Parameters:
- int, int, int
- Returns:
- int
- Method signature:
- int weekNumber(int year, int month, int day)
- (be sure your method is public)
Notes
- Months, days, and ISO weeks are all numbered starting from 1.
Constraints
- year is between 1582 and 9999, inclusive
- month is between 1 and 12, inclusive
- day is valid for the given month and year
- the given date is between October 15, 1582 and December 31, 9999, inclusive
Examples
1642
12
25
Returns: 51
When Isaac Newton was born on December 25, 1642, people called it Thursday. With the mapping of weekdays specified above, the same date becomes a Sunday. Therefore, the last ISO week of 1642 begins on Monday, December 26, 1642, and ends on Sunday, January 1, 1643. Because 1642 doesn't have 53 weeks under this mapping, its last week has ISO number 52. In consequence, the immediately preceding Sunday must fall in ISO week 51.
1815
2
26
Returns: 9
Napoleon escaped from the isle of Elba on February 26, 1815.
2000
1
1
Returns: 1
The Y2K bug failed to cripple the world economy on January 1, 2000.
2008
8
8
Returns: 32
The 29th Olympic Games will open in Beijing on August 8, 2008.
2276
7
4
Returns: 27
July 4, 2276 will be the quincentenary of the Declaration of Independence.
2073
12
31
Returns: 1
2006
12
29
Returns: 1
9995
12
29
Returns: 1
4485
1
29
Returns: 5
9222
1
5
Returns: 1
7658
12
27
Returns: 52
6813
1
6
Returns: 1
9004
1
6
Returns: 2
7739
12
29
Returns: 52
3605
12
30
Returns: 1
4984
1
3
Returns: 1
7197
1
2
Returns: 53
1639
1
3
Returns: 1
4446
4
10
Returns: 15
3430
12
31
Returns: 1
7810
1
4
Returns: 1
5414
4
25
Returns: 17
8325
12
17
Returns: 50
4569
1
2
Returns: 1
9050
1
6
Returns: 1
9882
5
30
Returns: 22
4049
9
11
Returns: 37
8801
11
9
Returns: 46
3498
1
2
Returns: 1
3419
5
15
Returns: 20
8521
7
11
Returns: 28
4716
6
18
Returns: 25
3421
12
31
Returns: 53
5476
12
31
Returns: 1
7775
12
20
Returns: 51
5175
1
3
Returns: 1
5938
9
26
Returns: 39
3896
1
4
Returns: 1
5964
6
29
Returns: 26
5237
11
17
Returns: 46
6925
11
10
Returns: 46
9451
2
19
Returns: 7
2957
7
8
Returns: 28
5438
4
5
Returns: 14
4290
1
5
Returns: 1
2602
4
27
Returns: 17
5901
12
30
Returns: 52
9062
3
12
Returns: 10
6157
1
4
Returns: 1
5287
2
18
Returns: 7
1639
1
2
Returns: 1
4368
1
6
Returns: 2
4967
1
15
Returns: 2
8564
1
11
Returns: 2
8829
12
31
Returns: 53
7673
9
27
Returns: 39
9528
12
30
Returns: 53
5724
11
13
Returns: 46
5846
4
30
Returns: 17
9021
1
5
Returns: 2
3709
12
6
Returns: 49
1671
1
6
Returns: 1
8168
7
11
Returns: 28
4609
1
28
Returns: 5
8807
1
6
Returns: 2
5265
12
30
Returns: 52
8545
8
2
Returns: 31
7391
4
10
Returns: 15
9178
12
9
Returns: 50
2889
8
2
Returns: 31
5727
1
1
Returns: 52
3921
1
12
Returns: 2
8592
1
5
Returns: 1
4893
1
3
Returns: 1
7675
12
30
Returns: 52
2345
3
24
Returns: 13
2854
12
30
Returns: 52
2189
1
4
Returns: 1
3036
1
6
Returns: 1
5081
1
9
Returns: 2
5184
8
15
Returns: 33
5705
10
7
Returns: 40
4201
5
3
Returns: 18
2666
1
1
Returns: 1
4850
4
11
Returns: 15
8658
3
6
Returns: 10
1813
12
30
Returns: 52
8290
12
30
Returns: 52
6998
1
11
Returns: 2
2573
1
1
Returns: 1
2720
12
21
Returns: 51
7840
11
1
Returns: 44
5498
12
27
Returns: 52
1582
10
15
Returns: 42
1582
11
15
Returns: 46
1582
12
15
Returns: 50
1582
12
20
Returns: 51
1582
12
30
Returns: 52
9999
12
25
Returns: 52
9999
12
31
Returns: 1
8007
1
1
Returns: 1
1582
10
15
Returns: 42
7975
1
1
Returns: 52
1642
12
31
Returns: 52
2073
12
31
Returns: 1
7113
1
1
Returns: 53