You are given a string s
representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:
'A'
: Absent.'L'
: Late.'P'
: Present.The student is eligible for an attendance award if they meet both of the following criteria:
'A'
) for strictly fewer than 2 days total.'L'
) for 3 or more consecutive days.Return true
if the student is eligible for an attendance award, or false
otherwise.
Example 1:
Input: s = "PPALLP " Output: true Explanation: The student has fewer than 2 absences and was never late 3 or more consecutive days.
Example 2:
Input: s = "PPALLL " Output: false Explanation: The student was late 3 consecutive days in the last 3 days, so is not eligible for the award.
Constraints:
1 <= s.length <= 1000
s[i]
is either 'A'
, 'L'
, or 'P'
.program main
implicit none
character(len=1000) :: s
logical :: result
! Example 1
s = "PPALLP "
write(*,*) solve(s), "Expected: True"
! Example 2
s = "PPALLL "
write(*,*) solve(s), "Expected: False"
contains
function solve(s) result(result)
implicit none
character(len=*), intent(in) :: s
integer :: i, j, k, n_absent, n_late
logical :: is_eligible
n_absent = 0
n_late = 0
is_eligible = .false.
do i = 1, len(s) - 2
if (s(i:i+2) == 'AA') then
n_absent = n_absent + 1
end if
if (s(i:i+2) == 'LLL') then
n_late = n_late + 1
end if
if (n_absent < 2 .and. n_late == 0) then
is_eligible = .true.
else if (n_absent >= 2 .or. n_late >= 3) then
exit
end if
end do
result = is_eligible
end function solve
end program main
temp.f95:17:36: 17 | function solve(s) result(result) | 1 Error: Function result βresultβ at (1) has no IMPLICIT type
! Declare variables
integer :: i, j, k, n, m, count_absences, count_consecutive_late
character(len=1000) :: s
! Read input
read(*,*) s
! Initialize variables
n = len(s)
m = 0
count_absences = 0
count_consecutive_late = 0
! Loop through the string
do i = 1, n
if (s(i:i) == 'A') then
count_absences = count_absences + 1
else if (s(i:i) == 'L') then
count_consecutive_late = count_consecutive_late + 1
else
count_consecutive_late = 0
end if
! Check if the student is eligible for the award
if (count_absences < 2 .and. count_consecutive_late == 0) then
write(*,*) "true"
return
end if
end do
! If the student is not eligible for the award, print "false"
write(*,*) "false"
end
At line 6 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7d86f5ad9960 in ??? #1 0x7d86f5ada4d9 in ??? #2 0x7d86f5d2e17b in ??? #3 0x7d86f5d27684 in ??? #4 0x7d86f5d282aa in ??? #5 0x5a4a691d120c in MAIN__ #6 0x5a4a691d13bc in main
def checkRecord(s: str) -> bool:
late, absent = 0, 0
for c in s:
if c == 'A':
absent += 1
late = 0
elif c == 'L':
late += 1
else:
late = 0
if absent >= 2 or late >= 3:
return False
return True
The algorithm initializes variables late
and absent
as counters for late and absent days, respectively.
1. Iterate through each character of the string s
.
2. If the current character is 'A', increment the absent
counter and set late
counter to zero.
3. If the current character is 'L', increment the late
counter.
4. If the current character is 'P', set the late
counter to zero.
5. After processing each character, check if absent
is greater or equal to 2, or if late
is greater or equal to 3. If either condition is met, return false (ineligible for an attendance award).
6. If the entire string has been processed without meeting the conditions, return true (eligible for an attendance award).
#include <string>
bool checkRecord(std::string s) {
int late = 0, absent = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'A') {
absent++;
late = 0;
} else if (s[i] == 'L') {
late++;
} else {
late = 0;
}
if (absent >= 2 || late >= 3) return false;
}
return true;
}
The algorithm initializes variables late
and absent
as counters for late and absent days, respectively.
1. Iterate through each character of the string s
.
2. If the current character is 'A', increment the absent
counter and set late
counter to zero.
3. If the current character is 'L', increment the late
counter.
4. If the current character is 'P', set the late
counter to zero.
5. After processing each character, check if absent
is greater or equal to 2, or if late
is greater or equal to 3. If either condition is met, return false (ineligible for an attendance award).
6. If the entire string has been processed without meeting the conditions, return true (eligible for an attendance award).