Days Between a Customer's First and Last Order
Whetcode Supply is putting together a long-service discount, and support is sanity-checking the rule one account at a time before it goes live. The question they ask of each account is the same: how long has this customer's relationship with the shop actually run — the stretch of time from their very first order to their most recent one?
Two details make this less obvious than it sounds. The order count is no help at all: one of these customers placed two orders on the same day, so three orders do not mean three days of anything. And order_date is stored as plain text, so the values look like dates without behaving like them.
orders_df — one row per order placed this year. customer_id says who placed it; order_date is a text date in YYYY-MM-DD form.
| id | customer_id | product_id | quantity | order_date |
|---|---|---|---|---|
| 1 | 1 | 1 | 2 | 2023-02-01 |
| 2 | 1 | 4 | 1 | 2023-02-01 |
| 3 | 2 | 2 | 1 | 2023-02-10 |
| 4 | 3 | 3 | 3 | 2023-03-05 |
| 5 | 4 | 5 | 5 | 2023-03-11 |
| 6 | 1 | 3 | 1 | 2023-04-02 |
| 7 | 5 | 1 | 4 | 2023-05-20 |
| 8 | 2 | 5 | 2 | 2023-05-22 |
| 9 | 3 | 4 | 1 | 2023-06-01 |
| 10 | 5 | 2 | 1 | 2023-06-15 |
Input
The DataFrame above is already built for you. One line of input arrives: the customer_id of the account support is checking this time, as a whole number. Every account named there has placed at least one order.
Task: Print one whole number: how many days separate that customer's earliest order from their latest. Measure the gap between the two dates, not the number of calendar days touched — two orders on the same day are 0 days apart, an order on the 1st and one on the 2nd are 1 day apart. An account with only one order has no gap at all and prints 0.
Example: if their first and last orders were a fortnight apart, you'd print 14.
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in