Skip to content

Commit 4a84316

Browse files
committed
feat(pandas): add problem 176
1 parent 7ffd01c commit 4a84316

File tree

3 files changed

+116
-50
lines changed

3 files changed

+116
-50
lines changed

README.md

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,71 @@ Fiddling around with DataFusion and PyArrow.
44

55
### Select
66

7-
- [X] [] 1757. [Recyclable and Low Fat Products](https://leetcode.com/problems/recyclable-and-low-fat-products) - Easy
8-
- [X] [] 584. [Find Customer Referee](https://leetcode.com/problems/find-customer-referee) - Easy
9-
- [X] [] 595. [Big Countries](https://leetcode.com/problems/big-countries) - Easy
10-
- [X] [] 1148. [Article Views I](https://leetcode.com/problems/article-views-i) - Easy
11-
- [X] [] 1683. [Invalid Tweets](https://leetcode.com/problems/invalid-tweets) - Easy
7+
- [X] [] [] 1757. [Recyclable and Low Fat Products](https://leetcode.com/problems/recyclable-and-low-fat-products) - Easy
8+
- [X] [] [] 584. [Find Customer Referee](https://leetcode.com/problems/find-customer-referee) - Easy
9+
- [X] [] [] 595. [Big Countries](https://leetcode.com/problems/big-countries) - Easy
10+
- [X] [] [] 1148. [Article Views I](https://leetcode.com/problems/article-views-i) - Easy
11+
- [X] [] [] 1683. [Invalid Tweets](https://leetcode.com/problems/invalid-tweets) - Easy
1212

1313
### Basic Joins
1414

15-
- [X] [] 1378. [Replace Employee ID With The Unique Identifier](https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier) - Easy
16-
- [X] [] 1068. [Product Sales Analysis I](https://leetcode.com/problems/product-sales-analysis-i) - Easy
17-
- [X] [] 1581. [Customer Who Visited but Did Not Make Any Transactions](https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions) - Easy
18-
- [X] [] 197. [Rising Temperature](https://leetcode.com/problems/rising-temperature) - Easy
19-
- [X] [] 1661. [Average Time of Process per Machine](https://leetcode.com/problems/average-time-of-process-per-machine) - Easy
20-
- [X] [] 577. [Employee Bonus](https://leetcode.com/problems/employee-bonus) - Easy
21-
- [X] [] 1280. [Students and Examinations](https://leetcode.com/problems/students-and-examinations) - Easy
22-
- [X] [] 570. [Managers with at Least 5 Direct Reports](https://leetcode.com/problems/managers-with-at-least-5-direct-reports) - Medium
23-
- [X] [] 1934. [Confirmation Rate](https://leetcode.com/problems/confirmation-rate) - Medium
15+
- [X] [] [] 1378. [Replace Employee ID With The Unique Identifier](https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier) - Easy
16+
- [X] [] [] 1068. [Product Sales Analysis I](https://leetcode.com/problems/product-sales-analysis-i) - Easy
17+
- [X] [] [] 1581. [Customer Who Visited but Did Not Make Any Transactions](https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions) - Easy
18+
- [X] [] [] 197. [Rising Temperature](https://leetcode.com/problems/rising-temperature) - Easy
19+
- [X] [] [] 1661. [Average Time of Process per Machine](https://leetcode.com/problems/average-time-of-process-per-machine) - Easy
20+
- [X] [] [] 577. [Employee Bonus](https://leetcode.com/problems/employee-bonus) - Easy
21+
- [X] [] [] 1280. [Students and Examinations](https://leetcode.com/problems/students-and-examinations) - Easy
22+
- [X] [] [] 570. [Managers with at Least 5 Direct Reports](https://leetcode.com/problems/managers-with-at-least-5-direct-reports) - Medium
23+
- [X] [] [] 1934. [Confirmation Rate](https://leetcode.com/problems/confirmation-rate) - Medium
2424

2525
### Basic Aggregate Functions
2626

27-
- [X] [X] 620. [Not Boring Movies](https://leetcode.com/problems/not-boring-movies) - Easy
28-
- [X] [] 1251. [Average Selling Price](https://leetcode.com/problems/average-selling-price) - Easy
29-
- [X] [] 1075. [Project Employees I](https://leetcode.com/problems/project-employees-i) - Easy
30-
- [X] [] 1633. [Percentage of Users Attended a Contest](https://leetcode.com/problems/percentage-of-users-attended-a-contest) - Easy
31-
- [X] [] 1211. [Queries Quality and Percentage](https://leetcode.com/problems/queries-quality-and-percentage) - Easy
32-
- [X] [] 1193. [Monthly Transactions I](https://leetcode.com/problems/monthly-transactions-i) - Medium
33-
- [X] [] 1174. [Immediate Food Delivery II](https://leetcode.com/problems/immediate-food-delivery-ii) - Medium
34-
- [X] [] 550. [Game Play Analysis IV](https://leetcode.com/problems/game-play-analysis-iv) - Medium
27+
- [X] [X] [] 620. [Not Boring Movies](https://leetcode.com/problems/not-boring-movies) - Easy
28+
- [X] [] [] 1251. [Average Selling Price](https://leetcode.com/problems/average-selling-price) - Easy
29+
- [X] [] [] 1075. [Project Employees I](https://leetcode.com/problems/project-employees-i) - Easy
30+
- [X] [] [] 1633. [Percentage of Users Attended a Contest](https://leetcode.com/problems/percentage-of-users-attended-a-contest) - Easy
31+
- [X] [] [] 1211. [Queries Quality and Percentage](https://leetcode.com/problems/queries-quality-and-percentage) - Easy
32+
- [X] [] [] 1193. [Monthly Transactions I](https://leetcode.com/problems/monthly-transactions-i) - Medium
33+
- [X] [] [] 1174. [Immediate Food Delivery II](https://leetcode.com/problems/immediate-food-delivery-ii) - Medium
34+
- [X] [] [] 550. [Game Play Analysis IV](https://leetcode.com/problems/game-play-analysis-iv) - Medium
3535

3636
### Sorting and Grouping
3737

38-
- [X] [] 2356. [Number of Unique Subjects Taught by Each Teacher](https://leetcode.com/problems/number-of-unique-subjects-taught-by-each-teacher) - Easy
39-
- [X] [] 1141. [User Activity for the Past 30 Days I](https://leetcode.com/problems/user-activity-for-the-past-30-days-i) - Easy
40-
- [X] [] 1070. [Product Sales Analysis III](https://leetcode.com/problems/product-sales-analysis-iii) - Medium
41-
- [X] [] 596. [Classes More Than 5 Students](https://leetcode.com/problems/classes-more-than-5-students) - Easy
42-
- [X] [] 1729. [Find Followers Count](https://leetcode.com/problems/find-followers-count) - Easy
43-
- [X] [] 619. [Biggest Single Number](https://leetcode.com/problems/biggest-single-number) - Easy
44-
- [X] [] 1045. [Customers Who Bought All Products](https://leetcode.com/problems/customers-who-bought-all-products) - Medium
38+
- [X] [] [] 2356. [Number of Unique Subjects Taught by Each Teacher](https://leetcode.com/problems/number-of-unique-subjects-taught-by-each-teacher) - Easy
39+
- [X] [] [] 1141. [User Activity for the Past 30 Days I](https://leetcode.com/problems/user-activity-for-the-past-30-days-i) - Easy
40+
- [X] [] [] 1070. [Product Sales Analysis III](https://leetcode.com/problems/product-sales-analysis-iii) - Medium
41+
- [X] [] [] 596. [Classes More Than 5 Students](https://leetcode.com/problems/classes-more-than-5-students) - Easy
42+
- [X] [] [] 1729. [Find Followers Count](https://leetcode.com/problems/find-followers-count) - Easy
43+
- [X] [] [] 619. [Biggest Single Number](https://leetcode.com/problems/biggest-single-number) - Easy
44+
- [X] [] [] 1045. [Customers Who Bought All Products](https://leetcode.com/problems/customers-who-bought-all-products) - Medium
4545

4646
### Advanced Select and Joins
4747

48-
- [X] [] 1731. [The Number of Employees Which Report to Each Employee](https://leetcode.com/problems/the-number-of-employees-which-report-to-each-employee) - Easy
49-
- [X] [] 1789. [Primary Department for Each Employee](https://leetcode.com/problems/primary-department-for-each-employee) - Easy
50-
- [X] [] 610. [Triangle Judgement](https://leetcode.com/problems/triangle-judgement) - Easy
51-
- [X] [] 180. [Consecutive Numbers](https://leetcode.com/problems/consecutive-numbers) - Medium
52-
- [X] [] 1164. [Product Price at a Given Date](https://leetcode.com/problems/product-price-at-a-given-date) - Medium
53-
- [X] [] 1204. [Last Person to Fit in the Bus](https://leetcode.com/problems/last-person-to-fit-in-the-bus) - Medium
54-
- [X] [] 1907. [Count Salary Categories](https://leetcode.com/problems/count-salary-categories) - Medium
48+
- [X] [] [] 1731. [The Number of Employees Which Report to Each Employee](https://leetcode.com/problems/the-number-of-employees-which-report-to-each-employee) - Easy
49+
- [X] [] [] 1789. [Primary Department for Each Employee](https://leetcode.com/problems/primary-department-for-each-employee) - Easy
50+
- [X] [] [] 610. [Triangle Judgement](https://leetcode.com/problems/triangle-judgement) - Easy
51+
- [X] [] [] 180. [Consecutive Numbers](https://leetcode.com/problems/consecutive-numbers) - Medium
52+
- [X] [] [] 1164. [Product Price at a Given Date](https://leetcode.com/problems/product-price-at-a-given-date) - Medium
53+
- [X] [] [] 1204. [Last Person to Fit in the Bus](https://leetcode.com/problems/last-person-to-fit-in-the-bus) - Medium
54+
- [X] [] [] 1907. [Count Salary Categories](https://leetcode.com/problems/count-salary-categories) - Medium
5555

5656
### Subqueries
5757

58-
- [X] [] 1978. [Employees Whose Manager Left the Company](https://leetcode.com/problems/employees-whose-manager-left-the-company) - Easy
59-
- [X] [] 626. [Exchange Seats](https://leetcode.com/problems/exchange-seats) - Medium
60-
- [X] [] 1341. [Movie Rating](https://leetcode.com/problems/movie-rating) - Medium
61-
- [] [X] 1321. [Restaurant Growth](https://leetcode.com/problems/restaurant-growth) - Medium
62-
- [X] [] 602. [Friend Requests II: Who Has the Most Friends](https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends) - Medium
63-
- [X] [] 585. [Investments in 2016](https://leetcode.com/problems/investments-in-2016) - Medium
64-
- [X] [] 185. [Department Top Three Salaries](https://leetcode.com/problems/department-top-three-salaries) - Hard
58+
- [X] [] [] 1978. [Employees Whose Manager Left the Company](https://leetcode.com/problems/employees-whose-manager-left-the-company) - Easy
59+
- [X] [] [] 626. [Exchange Seats](https://leetcode.com/problems/exchange-seats) - Medium
60+
- [X] [] [] 1341. [Movie Rating](https://leetcode.com/problems/movie-rating) - Medium
61+
- [] [X] [] 1321. [Restaurant Growth](https://leetcode.com/problems/restaurant-growth) - Medium
62+
- [X] [] [] 602. [Friend Requests II: Who Has the Most Friends](https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends) - Medium
63+
- [X] [] [] 585. [Investments in 2016](https://leetcode.com/problems/investments-in-2016) - Medium
64+
- [X] [] [] 185. [Department Top Three Salaries](https://leetcode.com/problems/department-top-three-salaries) - Hard
6565

6666
### Advanced String Functions / Regex / Clause
6767

68-
- [X] [] 1667. [Fix Names in a Table](https://leetcode.com/problems/fix-names-in-a-table) - Easy
69-
- [X] [] 1527. [Patients With a Condition](https://leetcode.com/problems/patients-with-a-condition) - Easy
70-
- [X] [] 196. [Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails) - Easy
71-
- [X] [X] 176. [Second Highest Salary](https://leetcode.com/problems/second-highest-salary) - Medium
72-
- [] [X] 1484. [Group Sold Products By The Date](https://leetcode.com/problems/group-sold-products-by-the-date) - Easy
73-
- [X] [] 1327. [List the Products Ordered in a Period](https://leetcode.com/problems/list-the-products-ordered-in-a-period) - Easy
74-
- [X] [] 1517. [Find Users With Valid E-Mails](https://leetcode.com/problems/find-users-with-valid-e-mails) - Easy
68+
- [X] [] [] 1667. [Fix Names in a Table](https://leetcode.com/problems/fix-names-in-a-table) - Easy
69+
- [X] [] [] 1527. [Patients With a Condition](https://leetcode.com/problems/patients-with-a-condition) - Easy
70+
- [X] [] [] 196. [Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails) - Easy
71+
- [X] [X] [X] 176. [Second Highest Salary](https://leetcode.com/problems/second-highest-salary) - Medium
72+
- [] [X] [] 1484. [Group Sold Products By The Date](https://leetcode.com/problems/group-sold-products-by-the-date) - Easy
73+
- [X] [] [] 1327. [List the Products Ordered in a Period](https://leetcode.com/problems/list-the-products-ordered-in-a-period) - Easy
74+
- [X] [] [] 1517. [Find Users With Valid E-Mails](https://leetcode.com/problems/find-users-with-valid-e-mails) - Easy

problems/pandas.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Solutions to LeetCode problems in pandas."""
2+
3+
import pandas as pd
4+
5+
6+
def problem_176(employee: pd.DataFrame) -> pd.DataFrame:
7+
"""Find the second highest distinct salary from the Employee table.
8+
9+
If there is no second highest salary, return null.
10+
11+
Parameters
12+
----------
13+
employee : pa.Table
14+
The table containing employee salary data.
15+
16+
Returns
17+
-------
18+
pd.DataFrame
19+
20+
"""
21+
filtered = (
22+
employee[["salary"]]
23+
.drop_duplicates()
24+
.sort_values("salary", ascending=False)
25+
.reset_index(drop=True)
26+
)
27+
second_highest_salary = filtered.iloc[1]["salary"] if len(filtered) > 1 else None
28+
result = pd.DataFrame([second_highest_salary], columns=["SecondHighestSalary"])
29+
if result.empty:
30+
return pd.DataFrame([None], columns=["SecondHighestSalary"])
31+
return result

tests/test_pandas.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pandas as pd
2+
import pytest
3+
4+
from problems.pandas import problem_176
5+
6+
7+
@pytest.mark.parametrize(
8+
"input_data, expected_data",
9+
[
10+
pytest.param(
11+
{"salary": [100, 200, 300]},
12+
{"SecondHighestSalary": [200]},
13+
id="distinct_salaries",
14+
),
15+
pytest.param(
16+
{"salary": [100, 200, 200, 300]},
17+
{"SecondHighestSalary": [200]},
18+
id="multiple_second_highest",
19+
),
20+
pytest.param(
21+
{"salary": [100]}, {"SecondHighestSalary": [None]}, id="single_salary"
22+
),
23+
pytest.param(
24+
{"salary": [100, 100, 100]},
25+
{"SecondHighestSalary": [None]},
26+
id="all_salaries_same",
27+
),
28+
pytest.param({"salary": []}, {"SecondHighestSalary": [None]}, id="empty_table"),
29+
],
30+
)
31+
def test_problem_176(input_data, expected_data):
32+
table = pd.DataFrame(input_data)
33+
expected_table = pd.DataFrame(expected_data)
34+
result = problem_176(table)
35+
assert result.equals(expected_table)

0 commit comments

Comments
 (0)