Skip to content

Commit 0477146

Browse files
committed
feat(pandas): add problem 1280
1 parent 3b24dde commit 0477146

File tree

3 files changed

+173
-1
lines changed

3 files changed

+173
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Fiddling around with DataFusion, pandas, and PyArrow.
2222
| 197 | [Rising Temperature](https://leetcode.com/problems/rising-temperature) | Easy ||||
2323
| 1661 | [Average Time of Process per Machine](https://leetcode.com/problems/average-time-of-process-per-machine) | Easy ||||
2424
| 577 | [Employee Bonus](https://leetcode.com/problems/employee-bonus) | Easy ||||
25-
| 1280 | [Students and Examinations](https://leetcode.com/problems/students-and-examinations) | Easy ||| |
25+
| 1280 | [Students and Examinations](https://leetcode.com/problems/students-and-examinations) | Easy ||| |
2626
| 570 | [Managers with at Least 5 Direct Reports](https://leetcode.com/problems/managers-with-at-least-5-direct-reports) | Medium ||||
2727
| 1934 | [Confirmation Rate](https://leetcode.com/problems/confirmation-rate) | Medium ||||
2828

problems/pandas.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,41 @@ def problem_1148(views: pd.DataFrame) -> pd.DataFrame:
205205
)
206206

207207

208+
def problem_1280(
209+
students: pd.DataFrame, subjects: pd.DataFrame, examinations: pd.DataFrame
210+
) -> pd.DataFrame:
211+
"""Find the number of times each student attended each exam.
212+
213+
Return the result table ordered by student_id and subject_name.
214+
215+
Parameters
216+
----------
217+
students : pd.DataFrame
218+
This table contains the ID and the name of one student in the school.
219+
subjects : pd.DataFrame
220+
This table contains the name of one subject in the school.
221+
examinations : pd.DataFrame
222+
This table indicates that a student attended the exam of subject_name.
223+
224+
Returns
225+
-------
226+
pd.DataFrame
227+
228+
"""
229+
examinations_agg = (
230+
examinations.groupby(["student_id", "subject_name"])
231+
.size()
232+
.reset_index(name="attended_exams")
233+
)
234+
joined = joined = (
235+
students.merge(subjects, how="cross")
236+
.merge(examinations_agg, how="left")
237+
.sort_values(["student_id", "subject_name"])
238+
)
239+
joined["attended_exams"] = joined["attended_exams"].fillna(0).astype(int)
240+
return joined
241+
242+
208243
def problem_1321(customer: pd.DataFrame) -> pd.DataFrame:
209244
"""Compute the moving average of how much the customer paid in a seven days window.
210245

tests/test_pandas.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
problem_595,
1414
problem_1068,
1515
problem_1148,
16+
problem_1280,
1617
problem_1321,
1718
problem_1378,
1819
problem_1581,
@@ -377,6 +378,142 @@ def test_problem_1148(input_data, expected_data):
377378
assert result.equals(expected_table)
378379

379380

381+
@pytest.mark.parametrize(
382+
"input_data_1, input_data_2, input_data_3, expected_data",
383+
[
384+
pytest.param(
385+
{
386+
"student_id": [1, 2, 13, 6],
387+
"student_name": ["Alice", "Bob", "John", "Alex"],
388+
},
389+
{"subject_name": ["Math", "Physics", "Programming"]},
390+
{
391+
"student_id": [1, 1, 1, 2, 1, 1, 13, 13, 13, 2, 1],
392+
"subject_name": [
393+
"Math",
394+
"Physics",
395+
"Programming",
396+
"Programming",
397+
"Physics",
398+
"Math",
399+
"Math",
400+
"Programming",
401+
"Physics",
402+
"Math",
403+
"Math",
404+
],
405+
},
406+
{
407+
"student_id": [1, 1, 1, 2, 2, 2, 6, 6, 6, 13, 13, 13],
408+
"student_name": [
409+
"Alice",
410+
"Alice",
411+
"Alice",
412+
"Bob",
413+
"Bob",
414+
"Bob",
415+
"Alex",
416+
"Alex",
417+
"Alex",
418+
"John",
419+
"John",
420+
"John",
421+
],
422+
"subject_name": [
423+
"Programming",
424+
"Physics",
425+
"Math",
426+
"Programming",
427+
"Math",
428+
"Physics",
429+
"Programming",
430+
"Physics",
431+
"Math",
432+
"Programming",
433+
"Physics",
434+
"Math",
435+
],
436+
"attended_exams": [1, 2, 3, 1, 1, 0, 0, 0, 0, 1, 1, 1],
437+
},
438+
id="happy_path",
439+
),
440+
pytest.param(
441+
{
442+
"student_id": [1, 2, 13, 6],
443+
"student_name": ["Alice", "Bob", "John", None],
444+
},
445+
{"subject_name": ["Math", "Physics", "Programming"]},
446+
{
447+
"student_id": [1, 1, 1, 2, 1, 1, 13, 13, 13, 2, 1],
448+
"subject_name": [
449+
"Math",
450+
"Physics",
451+
"Programming",
452+
"Programming",
453+
"Physics",
454+
"Math",
455+
"Math",
456+
"Programming",
457+
"Physics",
458+
"Math",
459+
"Math",
460+
],
461+
},
462+
{
463+
"student_id": [1, 1, 1, 2, 2, 2, 6, 6, 6, 13, 13, 13],
464+
"student_name": [
465+
"Alice",
466+
"Alice",
467+
"Alice",
468+
"Bob",
469+
"Bob",
470+
"Bob",
471+
None,
472+
None,
473+
None,
474+
"John",
475+
"John",
476+
"John",
477+
],
478+
"subject_name": [
479+
"Programming",
480+
"Physics",
481+
"Math",
482+
"Programming",
483+
"Math",
484+
"Physics",
485+
"Programming",
486+
"Physics",
487+
"Math",
488+
"Programming",
489+
"Physics",
490+
"Math",
491+
],
492+
"attended_exams": [1, 2, 3, 1, 1, 0, 0, 0, 0, 1, 1, 1],
493+
},
494+
id="happy_path_null_name",
495+
),
496+
],
497+
)
498+
def test_problem_1280(input_data_1, input_data_2, input_data_3, expected_data):
499+
table_1 = pd.DataFrame(input_data_1)
500+
table_2 = pd.DataFrame(input_data_2)
501+
table_3 = pd.DataFrame(input_data_3)
502+
expected_table = (
503+
pd.DataFrame(expected_data)
504+
.sort_values(["student_id", "subject_name"])
505+
.reset_index(drop=True)
506+
)
507+
result = (
508+
problem_1280(table_1, table_2, table_3)
509+
.sort_values(["student_id", "subject_name"])
510+
.reset_index(drop=True)
511+
)
512+
assert_frame_equal(
513+
result, expected_table, check_dtype=False, check_index_type=False
514+
)
515+
516+
380517
@pytest.mark.parametrize(
381518
"input_data, expected_data",
382519
[

0 commit comments

Comments
 (0)