Implement a send_schedule function that manages email sending schedules based on subscription status.
You are given a list of subscribers, where each subscriber is represented as [id, subscribe_date, expire_date]. You are also given a list of rules (strings) and a query range [start_day, end_day].
Each rule determines when an email should be sent to a subscriber:
-
"welcome"
: send on the subscriber's
subscribe_date
.
-
"expired"
: send on the subscriber's
expire_date
.
-
A numeric string (for example
"-7"
,
"3"
or
"0"
): send on
expire_date + offset
, where offset is the integer value of the rule string. Negative means before expiration, positive means after.
Every rule applies to every subscriber. Return a sorted list of [day, subscriber_id, rule_key] triples for all emails that fall within [start_day, end_day] inclusive. Sort by day first, then by subscriber_id, then by rule_key.
Function Signature
def send_schedule(subscribers: list[list], rules: list[str], start_day: int, end_day: int) -> list[list]:
...
Rules
-
Each subscriber is a list
[id, subscribe_date, expire_date]
. The
id
is a string and both dates are integers.
-
Each output triple is
[day, subscriber_id, rule_key]
.
day
is an integer and
rule_key
is the rule string exactly as it appears in
rules
, so the offset rule
"5"
stays the string
"5"
.
-
Sort by
day
ascending. Break ties by
subscriber_id
, then by
rule_key
, comparing strings by character code the way Python compares
str
values. Under that order
"0"
comes before
"expired"
and
"-10"
comes before
"-7"
.
-
If two rules send the same subscriber an email on the same day, both triples are returned.
-
A day outside
[start_day, end_day]
, including a negative day produced by an offset, is left out.
Constraints
-
1 <= len(subscribers) <= 1000
-
1 <= len(rules) <= 20
-
Each subscriber has a unique string
id
, an integer
subscribe_date
, and an integer
expire_date
where
subscribe_date <= expire_date
.
-
Rules are strings:
"welcome"
,
"expired"
, or an integer string like
"-7"
,
"3"
,
"0"
.
-
0 <= start_day <= end_day <= 10000
Examples
Input: subscribers = [["alice", 1, 30], ["bob", 5, 20]], rules = ["welcome", "expired", "-7"], start_day = 0, end_day = 35
Output: [[1, "alice", "welcome"], [5, "bob", "welcome"], [13, "bob", "-7"], [20, "bob", "expired"], [23, "alice", "-7"], [30, "alice", "expired"]]
Alice's emails fall on days 1, 30 and 23 (30 - 7). Bob's fall on days 5, 20 and 13 (20 - 7). All six days are inside the range, so all six triples are returned in day order.
Input: subscribers = [["user1", 10, 50]], rules = ["welcome", "expired", "-10", "5"], start_day = 40, end_day = 60
Output: [[40, "user1", "-10"], [50, "user1", "expired"], [55, "user1", "5"]]
The welcome email on day 10 is outside the range and is dropped. The other three rules give days 50, 40 and 55, and sorting by day gives the output.
Input: subscribers = [["carol", 3, 10], ["amy", 3, 8]], rules = ["welcome", "2", "0", "expired"], start_day = 5, end_day = 10
Output: [[8, "amy", "0"], [8, "amy", "expired"], [10, "amy", "2"], [10, "carol", "0"], [10, "carol", "expired"]]
Both welcome emails (day 3) and carol's "2" email (day 12) fall outside the range. On day 8 amy has two emails and "0" sorts before "expired". On day 10 "amy" sorts before "carol".