Skip to content

Commit 043b3ab

Browse files
committed
feat(pandas): add problem 1075
1 parent 3c940ae commit 043b3ab

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Fiddling around with DataFusion, pandas, and PyArrow.
3232
|----------:|:------|:-----------|:----------:|:------:|:-------:|
3333
| 620 | [Not Boring Movies](https://leetcode.com/problems/not-boring-movies) | Easy ||||
3434
| 1251 | [Average Selling Price](https://leetcode.com/problems/average-selling-price) | Easy ||||
35-
| 1075 | [Project Employees I](https://leetcode.com/problems/project-employees-i) | Easy || ||
35+
| 1075 | [Project Employees I](https://leetcode.com/problems/project-employees-i) | Easy || ||
3636
| 1633 | [Percentage of Users Attended a Contest](https://leetcode.com/problems/percentage-of-users-attended-a-contest) | Easy ||||
3737
| 1211 | [Queries Quality and Percentage](https://leetcode.com/problems/queries-quality-and-percentage) | Easy ||||
3838
| 1193 | [Monthly Transactions I](https://leetcode.com/problems/monthly-transactions-i) | Medium ||||

problems/pandas.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,33 @@ def problem_1068(sales: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame:
201201
return sales.merge(product, on="product_id")[["product_name", "year", "price"]]
202202

203203

204+
def problem_1075(project: pd.DataFrame, employee: pd.DataFrame) -> pd.DataFrame:
205+
"""Report each project's average employee experience, rounded to 2 digits.
206+
207+
Return the result table in any order.
208+
209+
Parameters
210+
----------
211+
project : pd.DataFrame
212+
Table shows employees (employee_id) working on projects (project_id).
213+
employee : pd.DataFrame
214+
This table contains information about one employee.
215+
216+
Returns
217+
-------
218+
pd.DataFrame
219+
220+
"""
221+
joined = (
222+
project.merge(employee)
223+
.groupby("project_id")
224+
.aggregate(average_years=pd.NamedAgg(column="experience_years", aggfunc="mean"))
225+
.reset_index()
226+
)
227+
joined["average_years"] = joined["average_years"].round(2)
228+
return joined
229+
230+
204231
def problem_1148(views: pd.DataFrame) -> pd.DataFrame:
205232
"""Find all the authors that viewed at least one of their own articles.
206233

problems/pyarrow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ def problem_1075(project: pa.Table, employee: pa.Table) -> pa.Table:
673673
.aggregate([("experience_years", "mean")])
674674
)
675675
return joined.set_column(
676-
1, "experience_years", pc.round(joined["experience_years_mean"], 2)
676+
1, "average_years", pc.round(joined["experience_years_mean"], 2)
677677
)
678678

679679

tests/test_pandas.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
problem_595,
1515
problem_620,
1616
problem_1068,
17+
problem_1075,
1718
problem_1148,
1819
problem_1251,
1920
problem_1280,
@@ -437,6 +438,41 @@ def test_problem_1068(input_data_1, input_data_2, expected_data):
437438
)
438439

439440

441+
@pytest.mark.parametrize(
442+
"input_data_1, input_data_2, expected_data",
443+
[
444+
pytest.param(
445+
{
446+
"employee_id": [1, 2, 3],
447+
"project_id": [101, 102, 103],
448+
"experience_years": [5, 10, 15],
449+
},
450+
{"employee_id": [1, 2, 3], "department": ["HR", "IT", "Finance"]},
451+
{"project_id": [101, 102, 103], "average_years": [5.0, 10.0, 15.0]},
452+
id="happy_path",
453+
),
454+
pytest.param(
455+
{
456+
"employee_id": [1, 2, 3],
457+
"project_id": [101, 101, 101],
458+
"experience_years": [33, 34, 34],
459+
},
460+
{"employee_id": [1, 2, 3], "department": ["HR", "IT", "IT"]},
461+
{"project_id": [101], "average_years": [33.67]},
462+
id="happy_path_rounding_2",
463+
),
464+
],
465+
)
466+
def test_problem_1075(input_data_1, input_data_2, expected_data):
467+
table_1 = pd.DataFrame(input_data_1)
468+
table_2 = pd.DataFrame(input_data_2)
469+
expected_table = pd.DataFrame(expected_data)
470+
result = problem_1075(table_1, table_2).reset_index(drop=True)
471+
assert_frame_equal(
472+
result, expected_table, check_dtype=False, check_index_type=False
473+
)
474+
475+
440476
@pytest.mark.parametrize(
441477
"input_data, expected_data",
442478
[

tests/test_pyarrow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ def test_problem_1070(input_data, expected_data):
11011101
"experience_years": [5, 10, 15],
11021102
},
11031103
{"employee_id": [1, 2, 3], "department": ["HR", "IT", "Finance"]},
1104-
{"project_id": [101, 102, 103], "experience_years": [5.0, 10.0, 15.0]},
1104+
{"project_id": [101, 102, 103], "average_years": [5.0, 10.0, 15.0]},
11051105
id="happy_path",
11061106
),
11071107
pytest.param(
@@ -1111,7 +1111,7 @@ def test_problem_1070(input_data, expected_data):
11111111
"experience_years": [33, 34, 34],
11121112
},
11131113
{"employee_id": [1, 2, 3], "department": ["HR", "IT", "IT"]},
1114-
{"project_id": [101], "experience_years": [33.67]},
1114+
{"project_id": [101], "average_years": [33.67]},
11151115
id="happy_path_rounding_2",
11161116
),
11171117
],

0 commit comments

Comments
 (0)