Skip to content

Commit e79a909

Browse files
committed
feat(pandas): add problem 1148
1 parent b6e4e1b commit e79a909

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Fiddling around with DataFusion, pandas, and PyArrow.
99
| 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 ||||
12-
| 1148 | [Article Views I](https://leetcode.com/problems/article-views-i) | Easy ||| |
12+
| 1148 | [Article Views I](https://leetcode.com/problems/article-views-i) | Easy ||| |
1313
| 1683 | [Invalid Tweets](https://leetcode.com/problems/invalid-tweets) | Easy ||||
1414

1515
## Basic Joins

problems/pandas.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,29 @@ def problem_595(world: pd.DataFrame) -> pd.DataFrame:
7575
return world[columns]
7676

7777

78+
def problem_1148(views: pd.DataFrame) -> pd.DataFrame:
79+
"""Find all the authors that viewed at least one of their own articles.
80+
81+
Return the result table sorted by id in ascending order.
82+
83+
Parameters
84+
----------
85+
views : pd.DataFrame
86+
Table logs viewers viewing articles by authors on specific dates.
87+
88+
Returns
89+
-------
90+
pd.DataFrame
91+
92+
"""
93+
return (
94+
views[views["author_id"] == views["viewer_id"]][["author_id"]]
95+
.drop_duplicates()
96+
.sort_values("author_id")
97+
.rename(columns={"author_id": "id"})
98+
)
99+
100+
78101
def problem_1321(customer: pd.DataFrame) -> pd.DataFrame:
79102
"""Compute the moving average of how much the customer paid in a seven days window.
80103

tests/test_pandas.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
problem_176,
88
problem_584,
99
problem_595,
10+
problem_1148,
1011
problem_1321,
1112
problem_1757,
1213
)
@@ -142,6 +143,46 @@ def test_problem_595(input_data, expected_data):
142143
), f"Expected table {expected_table}, but got {result}"
143144

144145

146+
@pytest.mark.parametrize(
147+
"input_data, expected_data",
148+
[
149+
pytest.param(
150+
{
151+
"article_id": [1, 2, 3],
152+
"author_id": [3, 7, 4],
153+
"viewer_id": [3, 7, 4],
154+
"view_date": [
155+
datetime(2019, 8, 1),
156+
datetime(2019, 8, 1),
157+
datetime(2019, 7, 21),
158+
],
159+
},
160+
{
161+
"id": [3, 4, 7],
162+
},
163+
id="happy_path",
164+
),
165+
pytest.param(
166+
{
167+
"article_id": [1, 2],
168+
"author_id": [3, 7],
169+
"viewer_id": [3, 7],
170+
"view_date": [datetime(2019, 8, 1), datetime(2019, 8, 1)],
171+
},
172+
{
173+
"id": [3, 7],
174+
},
175+
id="all_match",
176+
),
177+
],
178+
)
179+
def test_problem_1148(input_data, expected_data):
180+
table = pd.DataFrame(input_data)
181+
expected_table = pd.DataFrame(expected_data)
182+
result = problem_1148(table).reset_index(drop=True)
183+
assert result.equals(expected_table)
184+
185+
145186
@pytest.mark.parametrize(
146187
"input_data, expected_data",
147188
[

0 commit comments

Comments
 (0)