Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 94174b4

Browse files
feat: add solution to lc problem: No.3657 (#4665)
No.3657.Find Loyal Customers
1 parent cf894ad commit 94174b4

File tree

4 files changed

+115
-2
lines changed

4 files changed

+115
-2
lines changed

‎solution/3600-3699/3657.Find Loyal Customers/README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tags:
2020

2121
<pre>
2222
+------------------+---------+
23-
| Column Name | Type |
23+
| Column Name | Type |
2424
+------------------+---------+
2525
| transaction_id | int |
2626
| customer_id | int |
@@ -146,7 +146,46 @@ transaction_type can be either &#39;purchase&#39; or &#39;refund&#39;.
146146
#### MySQL
147147

148148
```sql
149+
# Write your MySQL query statement below
150+
SELECT customer_id
151+
FROM customer_transactions
152+
GROUP BY 1
153+
HAVING
154+
COUNT(1) >= 3
155+
AND SUM(transaction_type = 'refund') / COUNT(1) < 0.2
156+
AND DATEDIFF(MAX(transaction_date), MIN(transaction_date)) >= 30
157+
ORDER BY 1;
158+
```
149159

160+
#### Pandas
161+
162+
```python
163+
import pandas as pd
164+
165+
166+
def find_loyal_customers(customer_transactions: pd.DataFrame) -> pd.DataFrame:
167+
customer_transactions["transaction_date"] = pd.to_datetime(
168+
customer_transactions["transaction_date"]
169+
)
170+
grouped = customer_transactions.groupby("customer_id")
171+
agg_df = grouped.agg(
172+
total_transactions=("transaction_type", "size"),
173+
refund_count=("transaction_type", lambda x: (x == "refund").sum()),
174+
min_date=("transaction_date", "min"),
175+
max_date=("transaction_date", "max"),
176+
).reset_index()
177+
agg_df["date_diff"] = (agg_df["max_date"] - agg_df["min_date"]).dt.days
178+
agg_df["refund_ratio"] = agg_df["refund_count"] / agg_df["total_transactions"]
179+
result = (
180+
agg_df[
181+
(agg_df["total_transactions"] >= 3)
182+
& (agg_df["refund_ratio"] < 0.2)
183+
& (agg_df["date_diff"] >= 30)
184+
][["customer_id"]]
185+
.sort_values("customer_id")
186+
.reset_index(drop=True)
187+
)
188+
return result
150189
```
151190

152191
<!-- tabs:end -->

‎solution/3600-3699/3657.Find Loyal Customers/README_EN.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tags:
2020

2121
<pre>
2222
+------------------+---------+
23-
| Column Name | Type |
23+
| Column Name | Type |
2424
+------------------+---------+
2525
| transaction_id | int |
2626
| customer_id | int |
@@ -146,7 +146,46 @@ transaction_type can be either &#39;purchase&#39; or &#39;refund&#39;.
146146
#### MySQL
147147

148148
```sql
149+
# Write your MySQL query statement below
150+
SELECT customer_id
151+
FROM customer_transactions
152+
GROUP BY 1
153+
HAVING
154+
COUNT(1) >= 3
155+
AND SUM(transaction_type = 'refund') / COUNT(1) < 0.2
156+
AND DATEDIFF(MAX(transaction_date), MIN(transaction_date)) >= 30
157+
ORDER BY 1;
158+
```
149159

160+
#### Pandas
161+
162+
```python
163+
import pandas as pd
164+
165+
166+
def find_loyal_customers(customer_transactions: pd.DataFrame) -> pd.DataFrame:
167+
customer_transactions["transaction_date"] = pd.to_datetime(
168+
customer_transactions["transaction_date"]
169+
)
170+
grouped = customer_transactions.groupby("customer_id")
171+
agg_df = grouped.agg(
172+
total_transactions=("transaction_type", "size"),
173+
refund_count=("transaction_type", lambda x: (x == "refund").sum()),
174+
min_date=("transaction_date", "min"),
175+
max_date=("transaction_date", "max"),
176+
).reset_index()
177+
agg_df["date_diff"] = (agg_df["max_date"] - agg_df["min_date"]).dt.days
178+
agg_df["refund_ratio"] = agg_df["refund_count"] / agg_df["total_transactions"]
179+
result = (
180+
agg_df[
181+
(agg_df["total_transactions"] >= 3)
182+
& (agg_df["refund_ratio"] < 0.2)
183+
& (agg_df["date_diff"] >= 30)
184+
][["customer_id"]]
185+
.sort_values("customer_id")
186+
.reset_index(drop=True)
187+
)
188+
return result
150189
```
151190

152191
<!-- tabs:end -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pandas as pd
2+
3+
4+
def find_loyal_customers(customer_transactions: pd.DataFrame) -> pd.DataFrame:
5+
customer_transactions["transaction_date"] = pd.to_datetime(
6+
customer_transactions["transaction_date"]
7+
)
8+
grouped = customer_transactions.groupby("customer_id")
9+
agg_df = grouped.agg(
10+
total_transactions=("transaction_type", "size"),
11+
refund_count=("transaction_type", lambda x: (x == "refund").sum()),
12+
min_date=("transaction_date", "min"),
13+
max_date=("transaction_date", "max"),
14+
).reset_index()
15+
agg_df["date_diff"] = (agg_df["max_date"] - agg_df["min_date"]).dt.days
16+
agg_df["refund_ratio"] = agg_df["refund_count"] / agg_df["total_transactions"]
17+
result = (
18+
agg_df[
19+
(agg_df["total_transactions"] >= 3)
20+
& (agg_df["refund_ratio"] < 0.2)
21+
& (agg_df["date_diff"] >= 30)
22+
][["customer_id"]]
23+
.sort_values("customer_id")
24+
.reset_index(drop=True)
25+
)
26+
return result
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Write your MySQL query statement below
2+
SELECT customer_id
3+
FROM customer_transactions
4+
GROUP BY 1
5+
HAVING
6+
COUNT(1) >= 3
7+
AND SUM(transaction_type = 'refund') / COUNT(1) < 0.2
8+
AND DATEDIFF(MAX(transaction_date), MIN(transaction_date)) >= 30
9+
ORDER BY 1;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /