Skip to content

Commit a6073e1

Browse files
committed
feat(pandas): add problem 584
1 parent fa2b2af commit a6073e1

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Fiddling around with DataFusion, pandas, and PyArrow.
77
| Problem # | Title | Difficulty | PyArrow | DataFusion | pandas |
88
|----------:|:------|:-----------|:-------:|:----------:|:------:|
99
| 1757 | [Recyclable and Low Fat Products](https://leetcode.com/problems/recyclable-and-low-fat-products) | Easy ||||
10-
| 584 | [Find Customer Referee](https://leetcode.com/problems/find-customer-referee) | Easy ||| |
10+
| 584 | [Find Customer Referee](https://leetcode.com/problems/find-customer-referee) | Easy ||| |
1111
| 595 | [Big Countries](https://leetcode.com/problems/big-countries) | Easy ||||
1212
| 1148 | [Article Views I](https://leetcode.com/problems/article-views-i) | Easy ||||
1313
| 1683 | [Invalid Tweets](https://leetcode.com/problems/invalid-tweets) | Easy ||||

problems/pandas.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ def problem_176(employee: pd.DataFrame) -> pd.DataFrame:
3131
return result
3232

3333

34+
def problem_584(customer: pd.DataFrame) -> pd.DataFrame:
35+
"""Find names of customers not referred by the customer with ID = 2.
36+
37+
Return the result table in any order.
38+
39+
Parameters
40+
----------
41+
customer : pa.Table
42+
Table shows customer IDs, names, and the ID of the customer who referred them.
43+
44+
Returns
45+
-------
46+
pa.Table
47+
48+
"""
49+
mask = customer[(customer["referee_id"].isnull()) | (customer["referee_id"] != 2)]
50+
return mask[["name"]]
51+
52+
3453
def problem_1321(customer: pd.DataFrame) -> pd.DataFrame:
3554
"""Compute the moving average of how much the customer paid in a seven days window.
3655

tests/test_pandas.py

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pandas as pd
44
import pytest
55

6-
from problems.pandas import problem_176, problem_1321, problem_1757
6+
from problems.pandas import problem_176, problem_584, problem_1321, problem_1757
77

88

99
@pytest.mark.parametrize(
@@ -37,6 +37,51 @@ def test_problem_176(input_data, expected_data):
3737
assert result.equals(expected_table)
3838

3939

40+
@pytest.mark.parametrize(
41+
"input_data, expected_data",
42+
[
43+
pytest.param(
44+
{
45+
"id": [1, 2, 4, 5],
46+
"name": ["Will", "Jane", "Bill", "Zack"],
47+
"referee_id": [None, None, None, 1],
48+
},
49+
{"name": ["Will", "Jane", "Bill", "Zack"]},
50+
id="happy_path_all_valid",
51+
),
52+
pytest.param(
53+
{
54+
"id": [3, 6],
55+
"name": ["Alex", "Mark"],
56+
"referee_id": [2, 2],
57+
},
58+
{"name": []},
59+
id="edge_case_all_referee_id_2",
60+
),
61+
pytest.param(
62+
{
63+
"id": [1, 3, 5, 6],
64+
"name": ["Will", "Alex", "Zack", "Mark"],
65+
"referee_id": [None, 2, 1, 2],
66+
},
67+
{"name": ["Will", "Zack"]},
68+
id="mixed_case_some_valid",
69+
),
70+
],
71+
)
72+
def test_problem_584(input_data, expected_data):
73+
table = pd.DataFrame(input_data)
74+
expected_table = pd.DataFrame(expected_data)
75+
result = problem_584(table).reset_index(drop=True)
76+
if result.shape == (0, len(expected_table.columns)):
77+
assert result.shape == expected_table.shape
78+
assert result.columns.equals(expected_table.columns)
79+
else:
80+
assert result.equals(
81+
expected_table
82+
), f"Expected table {expected_table}, but got {result}"
83+
84+
4085
@pytest.mark.parametrize(
4186
"input_data, expected_data",
4287
[
@@ -162,11 +207,9 @@ def test_problem_1321(input_data, expected_data):
162207
"recyclable": ["Y", "Y", "Y", "Y", "Y"],
163208
},
164209
{"product_id": [0, 1, 2, 3, 4]},
165-
)
166-
],
167-
ids=[
168-
"happy_path_mixed_values", "all_ys"
210+
),
169211
],
212+
ids=["happy_path_mixed_values", "all_ys"],
170213
)
171214
def test_problem_1757(input_data, expected_data):
172215
table = pd.DataFrame(input_data)

0 commit comments

Comments
 (0)