Skip to content

Commit f996920

Browse files
committed
feat(pandas): add problem 1757
1 parent c8adbb2 commit f996920

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Fiddling around with DataFusion, pandas, and PyArrow.
66

77
| Problem # | Title | Difficulty | PyArrow | DataFusion | pandas |
88
|----------:|:------|:-----------|:-------:|:----------:|:------:|
9-
| 1757 | [Recyclable and Low Fat Products](https://leetcode.com/problems/recyclable-and-low-fat-products) | Easy ||| |
9+
| 1757 | [Recyclable and Low Fat Products](https://leetcode.com/problems/recyclable-and-low-fat-products) | Easy ||| |
1010
| 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 ||||

problems/pandas.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,25 @@ def problem_1321(customer: pd.DataFrame) -> pd.DataFrame:
5959
.loc[6:]
6060
)
6161
return grouped.assign(average_amount=(grouped["amount"] / 7).round(2))
62+
63+
64+
def problem_1757(products: pd.DataFrame) -> pd.DataFrame:
65+
"""Find the ids of products that are both low fat and recyclable.
66+
67+
Return the result table in any order.
68+
69+
Parameters
70+
----------
71+
products : pa.Table
72+
Table stores products with low_fats and recyclable, keyed by product_id.
73+
74+
Returns
75+
-------
76+
pa.Table
77+
78+
"""
79+
return pd.DataFrame(
80+
products[(products["low_fats"] == "Y") & (products["recyclable"] == "Y")][
81+
"product_id"
82+
]
83+
)

tests/test_pandas.py

Lines changed: 32 additions & 1 deletion
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
6+
from problems.pandas import problem_176, problem_1321, problem_1757
77

88

99
@pytest.mark.parametrize(
@@ -142,3 +142,34 @@ def test_problem_1321(input_data, expected_data):
142142
assert result[col].equals(expected_table[col]), f"Mismatch in column '{col}'"
143143

144144
assert result.equals(expected_table)
145+
146+
147+
@pytest.mark.parametrize(
148+
"input_data, expected_data",
149+
[
150+
(
151+
{
152+
"product_id": [0, 1, 2, 3, 4],
153+
"low_fats": ["Y", "Y", "N", "Y", "N"],
154+
"recyclable": ["N", "Y", "Y", "Y", "N"],
155+
},
156+
{"product_id": [1, 3]},
157+
),
158+
(
159+
{
160+
"product_id": [0, 1, 2, 3, 4],
161+
"low_fats": ["Y", "Y", "Y", "Y", "Y"],
162+
"recyclable": ["Y", "Y", "Y", "Y", "Y"],
163+
},
164+
{"product_id": [0, 1, 2, 3, 4]},
165+
)
166+
],
167+
ids=[
168+
"happy_path_mixed_values", "all_ys"
169+
],
170+
)
171+
def test_problem_1757(input_data, expected_data):
172+
table = pd.DataFrame(input_data)
173+
expected_table = pd.DataFrame(expected_data)
174+
result = problem_1757(table).reset_index(drop=True)
175+
assert result.equals(expected_table)

tests/test_pyarrow.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2153,10 +2153,18 @@ def test_problem_1731(input_data, expected_data):
21532153
"recyclable": ["N", "Y", "Y", "Y", "N"],
21542154
},
21552155
{"product_id": [1, 3]},
2156+
),
2157+
(
2158+
{
2159+
"product_id": [0, 1, 2, 3, 4],
2160+
"low_fats": ["Y", "Y", "Y", "Y", "Y"],
2161+
"recyclable": ["Y", "Y", "Y", "Y", "Y"],
2162+
},
2163+
{"product_id": [0, 1, 2, 3, 4]},
21562164
)
21572165
],
21582166
ids=[
2159-
"happy_path_mixed_values",
2167+
"happy_path_mixed_values", "all_ys"
21602168
],
21612169
)
21622170
def test_problem_1757(input_data, expected_data):

0 commit comments

Comments
 (0)