Day of the Year
Real-world version: Day Number Within the YearThe statement here is our own: the same inputs, outputs and technique.
A log stamp is given as date, a string in the form "YYYY-MM-DD".
Return which day of its own year that date is, counting the first of January as day 1.
A year has 366 days when it is a leap year and 365 otherwise. A year is a leap year when it divides by 4, except that a year dividing by 100 is not a leap year unless it also divides by 400.
Examples
Example 1
- Input
- date = "2020-12-31"
- Output
- 366
The year 2020 was a leap year, so it held 366 days and its final day is day 366.
Example 2
- Input
- date = "2020-03-01"
- Output
- 61
The year 2020 divides by 4 and not by 100, so February held 29 days, putting the first of March on day 61.
Example 3
- Input
- date = "1900-03-01"
- Output
- 60
The year 1900 divides by 100 but not by 400, so it was not a leap year and February held 28 days.
Constraints
- date.length == 10
- date[4] and date[7] are both '-', and every other character is a digit
- date names a real calendar date between the first of January 1900 and the last of December 2100
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def day_of_the_year(date: str) -> int:public int dayOfTheYear(String date)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's