easy +10 pts

Quarter from date

Return the calendar quarter (1–4) for a given date string in YYYY-MM-DD format.

Write a function `quarter_from_date(date_str: str) -> int` that takes a date string in the format 'YYYY-MM-DD' and returns the calendar quarter as an integer: 1 for Jan–Mar, 2 for Apr–Jun, 3 for Jul–Sep, 4 for Oct–Dec. Input is guaranteed to be a valid date in that format. Do not use any external libraries; the standard library `datetime` is allowed.

Constraints

- `date_str` is a string of the form 'YYYY-MM-DD' with a valid calendar date. - Year can be any positive integer up to 9999. - Solution should be O(1) time and space.

Example

>>> quarter_from_date('2023-01-15')
1
>>> quarter_from_date('2024-06-30')
2
>>> quarter_from_date('2022-09-01')
3
>>> quarter_from_date('2025-12-31')
4
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Extract the month from the date string. It is always at positions 5-6 (0-indexed).
Use integer arithmetic: if month is 1-3 return 1, 4-6 return 2, etc.
Consider applying the formula (month - 1) // 3 + 1.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.