Skip to content

Commit 7ffd01c

Browse files
committed
feat(datafusion): add 620 solution
1 parent 8dc45b2 commit 7ffd01c

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

README.md

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

2525
### Basic Aggregate Functions
2626

27-
- [X] [] 620. [Not Boring Movies](https://leetcode.com/problems/not-boring-movies) - Easy
27+
- [X] [X] 620. [Not Boring Movies](https://leetcode.com/problems/not-boring-movies) - Easy
2828
- [X] [] 1251. [Average Selling Price](https://leetcode.com/problems/average-selling-price) - Easy
2929
- [X] [] 1075. [Project Employees I](https://leetcode.com/problems/project-employees-i) - Easy
3030
- [X] [] 1633. [Percentage of Users Attended a Contest](https://leetcode.com/problems/percentage-of-users-attended-a-contest) - Easy

problems/datafusion.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,29 @@ def problem_176(employee: pa.Table) -> datafusion.dataframe.DataFrame:
3838
return t
3939

4040

41-
def problem_1321(customer: pa.Table) -> pa.Table:
41+
def problem_620(cinema: pa.Table) -> datafusion.dataframe.DataFrame:
42+
"""Report movies with odd IDs and descriptions not equal to "boring".
43+
44+
Return the result table ordered by rating in descending order.
45+
46+
Parameters
47+
----------
48+
cinema : pa.Table
49+
Table contains information about the name of a movie, genre, and its rating.
50+
51+
Returns
52+
-------
53+
pa.Table
54+
55+
"""
56+
ctx = datafusion.SessionContext()
57+
cinema = ctx.from_arrow(cinema)
58+
return cinema.filter(F.col("description") != "boring", F.col("id") % 2 == 1).sort(
59+
F.col("id").sort(ascending=False)
60+
)
61+
62+
63+
def problem_1321(customer: pa.Table) -> datafusion.dataframe.DataFrame:
4264
"""Compute the moving average of how much the customer paid in a seven days window.
4365
4466
You are the restaurant owner and you want to analyze a possible expansion (there

tests/test_datafusion.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from problems.datafusion import (
77
problem_176,
8+
problem_620,
89
problem_1321,
910
problem_1484,
1011
)
@@ -41,6 +42,51 @@ def test_problem_176(input_data, expected_data):
4142
assert result.to_arrow_table().equals(expected_table)
4243

4344

45+
@pytest.mark.parametrize(
46+
"input_data, expected_data",
47+
[
48+
pytest.param(
49+
{
50+
"id": [1, 2, 3, 4],
51+
"description": ["interesting", "boring", "exciting", "boring"],
52+
},
53+
{"id": [3, 1], "description": ["exciting", "interesting"]},
54+
id="happy_path_mixed_ids_and_descriptions",
55+
),
56+
pytest.param(
57+
{"id": [1, 3], "description": ["boring", "boring"]},
58+
{"id": [], "description": []},
59+
id="edge_case_all_boring",
60+
),
61+
pytest.param(
62+
{"id": [2, 4], "description": ["interesting", "exciting"]},
63+
{"id": [], "description": []},
64+
id="edge_case_no_odd_ids",
65+
),
66+
pytest.param(
67+
{"id": [1], "description": ["interesting"]},
68+
{"id": [1], "description": ["interesting"]},
69+
id="edge_case_single_row_matching",
70+
),
71+
pytest.param(
72+
{"id": [2], "description": ["boring"]},
73+
{"id": [], "description": []},
74+
id="edge_case_single_row_not_matching",
75+
),
76+
],
77+
)
78+
def test_problem_620(input_data, expected_data):
79+
table = pa.Table.from_pydict(input_data)
80+
expected_table = pa.Table.from_pydict(
81+
expected_data,
82+
schema=pa.schema(
83+
[pa.field("id", pa.int64()), pa.field("description", pa.string())]
84+
),
85+
)
86+
result = problem_620(table)
87+
assert result.to_arrow_table().equals(expected_table)
88+
89+
4490
@pytest.mark.parametrize(
4591
"input_data, expected_data",
4692
[

0 commit comments

Comments
 (0)