Longest reading streak
A reading log uses R for a day with reading and - for a missed day. Read the log and print the longest consecutive run of R characters. An all-missed log has a longest run of zero.
Input
One line containing 1 to 200 characters, each either R or -.
Output
One integer: the longest consecutive reading streak.
Example 1
Input
RR-RRR--R
Output
3
The three R characters in the middle form the longest run.
Constraints
- 1 <= log length <= 200
- The log contains only R and -
Hints
Hint 1 of 3
Keep one counter for the current run and one for the best run.
Hint 2 of 3
A dash resets only the current run.
Hint 3 of 3
Update best immediately after increasing current.
Solution
Show a reference solution and explanation
log = input().strip()
best = 0
current = 0
for mark in log:
if mark == "R":
current += 1
best = max(best, current)
else:
current = 0
print(best)
Why it works
The current counter represents the run ending at the character being inspected. Each missed day breaks that run, while the best counter remembers the largest value seen anywhere in the log.
Lesson for this exercise: Loops in Python: for and while