Skip to content

Commit 5199647

Browse files
committed
feat(pandas): add problem 596
1 parent aa14482 commit 5199647

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Fiddling around with DataFusion, pandas, and PyArrow.
3939
| 2356 | [Number of Unique Subjects Taught by Each Teacher](https://leetcode.com/problems/number-of-unique-subjects-taught-by-each-teacher) | Easy ||||
4040
| 1141 | [User Activity for the Past 30 Days I](https://leetcode.com/problems/user-activity-for-the-past-30-days-i) | Easy ||||
4141
| 1070 | [Product Sales Analysis III](https://leetcode.com/problems/product-sales-analysis-iii) | Medium ||||
42-
| 596 | [Classes More Than 5 Students](https://leetcode.com/problems/classes-more-than-5-students) | Easy || ||
42+
| 596 | [Classes More Than 5 Students](https://leetcode.com/problems/classes-more-than-5-students) | Easy || ||
4343
| 1729 | [Find Followers Count](https://leetcode.com/problems/find-followers-count) | Easy ||||
4444
| 619 | [Biggest Single Number](https://leetcode.com/problems/biggest-single-number) | Easy ||||
4545
| 1045 | [Customers Who Bought All Products](https://leetcode.com/problems/customers-who-bought-all-products) | Medium ||||

problems/pandas.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,28 @@ def problem_595(world: pd.DataFrame) -> pd.DataFrame:
236236
return world[columns]
237237

238238

239+
def problem_596(courses: pd.DataFrame) -> pd.DataFrame:
240+
"""Find all the classes that have at least five students.
241+
242+
Return the result table in any order.
243+
244+
Parameters
245+
----------
246+
courses : pd.DataFrame
247+
Table indicates the name of a student and the class in which they are enrolled.
248+
249+
Returns
250+
-------
251+
pd.DataFrame
252+
253+
"""
254+
return (
255+
courses.groupby("class", as_index=False)
256+
.aggregate(count=pd.NamedAgg("student", "count"))
257+
.query("count >= 5")[["class"]]
258+
)
259+
260+
239261
def problem_1068(sales: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame:
240262
"""Report the product_name, year, and price for each sale_id in the Sales table.
241263

tests/test_pandas.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,72 @@ def test_problem_595(input_data, expected_data):
433433
)
434434

435435

436+
@pytest.mark.parametrize(
437+
"input_data, expected_data",
438+
[
439+
pytest.param(
440+
{
441+
"student": [
442+
"A",
443+
"B",
444+
"C",
445+
"D",
446+
"E",
447+
"F",
448+
"G",
449+
"H",
450+
"I",
451+
"J",
452+
"K",
453+
"L",
454+
],
455+
"class": [
456+
"Math",
457+
"Math",
458+
"Biology",
459+
"Math",
460+
"Computer",
461+
"Math",
462+
"Math",
463+
"Math",
464+
"Computer",
465+
"Computer",
466+
"Computer",
467+
"Computer",
468+
],
469+
},
470+
{"class": ["Math", "Computer"]},
471+
id="happy_path_multiple_classes",
472+
),
473+
pytest.param(
474+
{
475+
"class": ["History", "History", "History", "History", "History"],
476+
"student": ["Alice", "Bob", "Charlie", "David", "Eve"],
477+
},
478+
{"class": ["History"]},
479+
id="edge_case_exactly_5_students",
480+
),
481+
pytest.param(
482+
{
483+
"class": ["Art", "Art", "Art", "Art"],
484+
"student": ["Alice", "Bob", "Charlie", "David"],
485+
},
486+
{"class": []},
487+
id="edge_case_less_than_5_students",
488+
),
489+
],
490+
)
491+
def test_problem_596(input_data, expected_data):
492+
table = pd.DataFrame(input_data)
493+
expected_table = (
494+
pd.DataFrame(expected_data).sort_values("class").reset_index(drop=True)
495+
)
496+
result = problem_596(table).sort_values("class").reset_index(drop=True)
497+
assert_frame_equal(
498+
result, expected_table, check_dtype=False, check_index_type=False
499+
)
500+
501+
436502
@pytest.mark.parametrize(
437503
"input_data, expected_data",
438504
[

0 commit comments

Comments
 (0)