You are given a list of date strings dates, each in the format YYYYMMDD. Convert each date to its year and quarter in the format YYYYQn and return the converted strings in the original order. For example, "20050301" becomes "2005Q1".
The quarter n is determined by the month:
-
Months 1 to 3 belong to
Q1
.
-
Months 4 to 6 belong to
Q2
.
-
Months 7 to 9 belong to
Q3
.
-
Months 10 to 12 belong to
Q4
.
Function Signature
def convert_to_quarters(dates: list[str]) -> list[str]:
Rules
-
The year in the output is the first four characters of the input string, written exactly as given.
-
The day (the last two characters) does not affect the answer.
-
The output has one string per input date, in the same order.
Constraints
-
1 <= len(dates) <= 10^4
-
Each string in
dates
has exactly 8 characters in
YYYYMMDD
format.
-
01 <= MM <= 12
(the month is valid).
-
YYYY
is a 4-digit year.
Examples
Example 1
Input: dates = ["20050301", "20050601", "20050901", "20051201"]
Output: ["2005Q1", "2005Q2", "2005Q3", "2005Q4"]
March is in Q1, June in Q2, September in Q3 and December in Q4.
Example 2
Input: dates = ["19991231", "20000101", "20200715"]
Output: ["1999Q4", "2000Q1", "2020Q3"]
December 1999 maps to 1999Q4, January 2000 to 2000Q1 and July 2020 to 2020Q3. Each date keeps its own year.
Example 3
Input: dates = ["20231001", "20230430", "19870331"]
Output: ["2023Q4", "2023Q2", "1987Q1"]
October is the first month of Q4, April the first month of Q2, and March the last month of Q1.