Given a date
string in the form Day Month Year
, where:
Day
is in the set { "1st ", "2nd ", "3rd ", "4th ", ..., "30th ", "31st "}
.Month
is in the set { "Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec "}
.Year
is in the range [1900, 2100]
.Convert the date string to the format YYYY-MM-DD
, where:
YYYY
denotes the 4 digit year.MM
denotes the 2 digit month.DD
denotes the 2 digit day.Example 1:
Input: date = "20th Oct 2052 " Output: "2052-10-20 "
Example 2:
Input: date = "6th Jun 1933 " Output: "1933-06-06 "
Example 3:
Input: date = "26th May 1960 " Output: "1960-05-26 "
Constraints:
program main
implicit none
character(len=10) :: date
integer :: year, month, day
! Example 1
date = "20th Oct 2052"
call convert_date(date, year, month, day)
write (*, '(I4, "-", I2.2, "-", I2.2)') year, month, day
! Example 2
date = "6th Jun 1933"
call convert_date(date, year, month, day)
write (*, '(I4, "-", I2.2, "-", I2.2)') year, month, day
! Example 3
date = "26th May 1960"
call convert_date(date, year, month, day)
write (*, '(I4, "-", I2.2, "-", I2.2)') year, month, day
contains
subroutine convert_date(date, year, month, day)
implicit none
character(len=*), intent(in) :: date
integer, intent(out) :: year, month, day
integer :: i
character(len=3) :: month_name
read (date, *) day, month_name, year
do i = 1, size(month_names)
if (month_names(i) == month_name) then
month = i
exit
end if
end do
end subroutine convert_date
end program main
temp.f95:35:16: 35 | if (month_names(i) == month_name) then | 1 Error: Function ‘month_names’ at (1) has no IMPLICIT type temp.f95:34:34: 34 | do i = 1, size(month_names) | 1 Error: Symbol ‘month_names’ at (1) has no IMPLICIT type; did you mean ‘month_name’?
PROGRAM DATE_CONVERSION
IMPLICIT NONE
CHARACTER(len=10) :: date
CHARACTER(len=4) :: year
CHARACTER(len=2) :: month
CHARACTER(len=2) :: day
CHARACTER(len=10) :: output
READ(*,*) date
! Extract the year
year = date(7:10)
! Extract the month
SELECT CASE (date(5:7))
CASE ("Jan")
month = "01"
CASE ("Feb")
month = "02"
CASE ("Mar")
month = "03"
CASE ("Apr")
month = "04"
CASE ("May")
month = "05"
CASE ("Jun")
month = "06"
CASE ("Jul")
month = "07"
CASE ("Aug")
month = "08"
CASE ("Sep")
month = "09"
CASE ("Oct")
month = "10"
CASE ("Nov")
month = "11"
CASE ("Dec")
month = "12"
END SELECT
! Extract the day
day = date(1:2)
! Build the output string
output = year // "-" // month // "-" // day
! Print the output
WRITE (*,*) output
END PROGRAM DATE_CONVERSION
At line 10 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7f5e0cf69960 in ??? #1 0x7f5e0cf6a4d9 in ??? #2 0x7f5e0d1be17b in ??? #3 0x7f5e0d1b7684 in ??? #4 0x7f5e0d1b82aa in ??? #5 0x5c2928ca2229 in MAIN__ #6 0x5c2928ca244c in main
def hasValidPath(grid):
m, n = len(grid), len(grid[0])
visited = [[False for _ in range(n)] for _ in range(m)]
return dfs(grid, 0, 0, m, n, visited)
def dfs(grid, x, y, m, n, visited):
if x < 0 or y < 0 or x >= m or y >= n or visited[x][y]:
return False
if x == m - 1 and y == n - 1:
return True
visited[x][y] = True
next_x = [-1, 0, 1, 0]
next_y = [0, 1, 0, -1]
for i in range(4):
dx = x + next_x[i]
dy = y + next_y[i]
if dfs(grid, dx, dy, m, n, visited):
return True
return False
The algorithm performs a depth-first search through the grid starting from the upper-left cell (0,0), marking each visited cell in a separate grid. It iterates 4 different directions for each cell, checking boundaries, and continuing its search if a valid path is found from the next cell in the given direction.
The DFS continues until it reaches the bottom-right cell (m-1, n-1), at which point it returns true. If the algorithm cannot find a path to the bottom-right cell, it returns false.
#include <vector>
bool hasValidPath(std::vector<std::vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
std::vector<std::vector<bool>> visited(m, std::vector<bool>(n, false));
return dfs(grid, 0, 0, m, n, visited);
}
bool dfs(std::vector<std::vector<int>>& grid, int x, int y, int m, int n, std::vector<std::vector<bool>>& visited) {
if (x < 0 || y < 0 || x >= m || y >= n || visited[x][y]) return false;
if (x == m - 1 && y == n - 1) return true;
visited[x][y] = true;
int next_x[4] = {-1, 0, 1, 0};
int next_y[4] = {0, 1, 0, -1};
for (int i = 0; i < 4; i++) {
int dx = x + next_x[i];
int dy = y + next_y[i];
if (dfs(grid, dx, dy, m, n, visited)) return true;
}
return false;
}
The algorithm performs a depth-first search through the grid starting from the upper-left cell (0,0), marking each visited cell in a separate grid. It iterates 4 different directions for each cell, checking boundaries, and continuing its search if a valid path is found from the next cell in the given direction.
The DFS continues until it reaches the bottom-right cell (m-1, n-1), at which point it returns true. If the algorithm cannot find a path to the bottom-right cell, it returns false.