Given a string date
representing a Gregorian calendar date formatted as YYYY-MM-DD
, return the day number of the year.
Example 1:
Input: date = "2019-01-09 " Output: 9 Explanation: Given date is the 9th day of the year in 2019.
Example 2:
Input: date = "2019-02-10 " Output: 41
Constraints:
date.length == 10
date[4] == date[7] == '-'
, and all other date[i]
's are digitsdate
represents a calendar date between Jan 1st, 1900 and Dec 31th, 2019.program main
implicit none
character(len=10) :: date
integer :: day_of_year
! Example 1
date = "2019-01-09"
write(*,*) day_of_year(date), "Expected: 9"
! Example 2
date = "2019-02-10"
write(*,*) day_of_year(date), "Expected: 41"
contains
function day_of_year(date) result(day)
implicit none
character(len=*), intent(in) :: date
integer :: day
! Convert YYYY-MM-DD to YYYYMMDD
date = adjustl(date)
date(5:6) = date(4:5)
date(4:4) = "-"
! Calculate day of year
day = 0
do while (date /= "000000")
select case (date(7:8))
case ("01","03","05","07","08","10","12")
day = day + 31
case ("04","06","09","11")
day = day + 30
case ("02")
if (mod(date(1:2),4)==0) then
day = day + 29
else
day = day + 28
end if
end select
date = date(9:)
end do
end function day_of_year
end program main
temp.f95:17:25: 17 | function day_of_year(date) result(day) | 1 Error: Procedure βday_of_yearβ at (1) has an explicit interface from a previous declaration temp.f95:18:21: 18 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:19:44: 19 | character(len=*), intent(in) :: date | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:20:22: 20 | integer :: day | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:23:28: 23 | date = adjustl(date) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:28:15: 28 | day = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:29:35: 29 | do while (date /= "000000") | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:30:35: 30 | select case (date(7:8)) | 1 Error: Unexpected SELECT CASE statement in CONTAINS section at (1) temp.f95:31:21: 31 | case ("01","03","05","07","08","10","12") | 1 Error: Unexpected CASE statement at (1) temp.f95:32:34: 32 | day = day + 31 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:33:21: 33 | case ("04","06","09","11") | 1 Error: Unexpected CASE statement at (1) temp.f95:34:34: 34 | day = day + 30 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:35:21: 35 | case ("02") | 1 Error: Unexpected CASE statement at (1) temp.f95:36:49: 36 | if (mod(date(1:2),4)==0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:37:38: 37 | day = day + 29 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:38:24: 38 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:39:38: 39 | day = day + 28 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:40:23: 40 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:41:15: 41 | end select | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:43:11: 43 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:44:7: 44 | end function day_of_year | 1 Error: Expecting END PROGRAM statement at (1)
! This program solves the problem of finding the day number of the year given a date in the Gregorian calendar.
! The date is represented as a string in the format "YYYY-MM-DD".
! The program runs with all provided examples and outputs the day number of the year to stdout.
! Declare variables
integer :: year, month, day, day_number
character(len=10) :: date
! Read input date from stdin
read(*,*) date
! Extract year, month, and day from input date
read(date(1:4),*) year
read(date(6:7),*) month
read(date(9:10),*) day
! Calculate day number of the year
day_number = day + (month-1)*30 + (year-1900)*365
! Print output
write(*,*) day_number
end program
At line 10 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7b65a32f0960 in ??? #1 0x7b65a32f14d9 in ??? #2 0x7b65a354517b in ??? #3 0x7b65a353e684 in ??? #4 0x7b65a353f2aa in ??? #5 0x559176b3f219 in MAIN__ #6 0x559176b3f484 in main
def is_leap_year(year: int) -> bool:
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
def day_of_year(date: str) -> int:
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
year, month, day = int(date[:4]), int(date[5:7]), int(date[8:])
if is_leap_year(year):
days_in_month[1] = 29
return sum(days_in_month[:month-1]) + day
The algorithm is straightforward. First, we extract the `year`, `month`, and `day` from the input date string. Then, we determine if the year is a leap year by checking if it's evenly divisible by 4 and not evenly divisible by 100, or evenly divisible by 400.
If the year is a leap year, we update the number of days in February (the second month) to be 29 days. Finally, we iterate over the months that have already passed, summing the number of days in each and add the day
of the input month. The result is the day number of the year.
#include <vector>
bool is_leap_year(int year) {
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
int day_of_year(string date) {
vector<int> days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int year = stoi(date.substr(0, 4));
int month = stoi(date.substr(5, 2));
int day = stoi(date.substr(8, 2));
if (is_leap_year(year)) {
days_in_month[1] = 29;
}
int result = 0;
for (int i = 0; i < month - 1; ++i) {
result += days_in_month[i];
}
return result + day;
}
The algorithm is straightforward. First, we extract the `year`, `month`, and `day` from the input date string. Then, we determine if the year is a leap year by checking if it's evenly divisible by 4 and not evenly divisible by 100, or evenly divisible by 400.
If the year is a leap year, we update the number of days in February (the second month) to be 29 days. Finally, we iterate over the months that have already passed, summing the number of days in each and add the day
of the input month. The result is the day number of the year.