easy +10 pts

Validate time format

Write a function that checks if a string is a valid 24-hour time in HH:MM format.

Write a function `is_valid_time(s)` that returns `True` if the input string `s` is a valid time in 24-hour format `HH:MM`, and `False` otherwise. A valid time must: - Have exactly 5 characters: two digits, a colon, and two digits. - The hours (first two digits) must be between `00` and `23` inclusive. - The minutes (last two digits) must be between `00` and `59` inclusive. Leading zeros are required (e.g., `09:05` is valid, `9:05` is not).

Constraints

- `s` is a string of length up to 10. - Time complexity: O(1) (constant time). - Space complexity: O(1).

Example

>>> is_valid_time('12:34')
True
>>> is_valid_time('23:59')
True
>>> is_valid_time('24:00')
False
>>> is_valid_time('12:60')
False
>>> is_valid_time('9:05')
False
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Check that the string length is exactly 5 and the colon is at index 2.
Use `s[:2]` and `s[3:]` to extract hours and minutes.
After checking digits, convert to integers and compare with the allowed ranges.
Remember that `'24:00'` is invalid because hours must be < 24.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.