From 9f0a14b9482c39077bdb078008462f4cf95c36e2 Mon Sep 17 00:00:00 2001 From: ankitabmungalpara Date: Tue, 4 Mar 2025 14:19:10 -0500 Subject: [PATCH 1/2] Create nth-highest-salary.py --- nth-highest-salary.py | 73 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 nth-highest-salary.py diff --git a/nth-highest-salary.py b/nth-highest-salary.py new file mode 100644 index 0000000..5afe1de --- /dev/null +++ b/nth-highest-salary.py @@ -0,0 +1,73 @@ +""" + +Table: Employee + ++-------------+------+ +| Column Name | Type | ++-------------+------+ +| id | int | +| salary | int | ++-------------+------+ +id is the primary key (column with unique values) for this table. +Each row of this table contains information about the salary of an employee. + + +Write a solution to find the nth highest salary from the Employee table. If there is no nth highest salary, return null. + +The result format is in the following example. + + +Example 1: + +Input: +Employee table: ++----+--------+ +| id | salary | ++----+--------+ +| 1 | 100 | +| 2 | 200 | +| 3 | 300 | ++----+--------+ +n = 2 +Output: ++------------------------+ +| getNthHighestSalary(2) | ++------------------------+ +| 200 | ++------------------------+ +Example 2: + +Input: +Employee table: ++----+--------+ +| id | salary | ++----+--------+ +| 1 | 100 | ++----+--------+ +n = 2 +Output: ++------------------------+ +| getNthHighestSalary(2) | ++------------------------+ +| null | ++------------------------+ + +""" + +# The function first removes duplicate salaries to ensure unique salary ranks. +# Then, it assigns a dense rank to each unique salary in descending order. +# Finally, it retrieves the salary corresponding to the Nth rank, returning None if N exceeds the number of unique salaries. + + +import pandas as pd + +def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame: + + dist = employee.drop_duplicates(subset='salary') + dist['rnk'] = dist['salary'].rank(method='dense', ascending=False) + ans = dist[dist.rnk == N][['salary']] + if not len(ans): + return pd.DataFrame({f'getNthHighestSalary({N})': [None]}) + ans = ans.rename(columns={'salary': f'getNthHighestSalary({N})'}) + + return ans From 33f28837eca83b4ed4fc7ec7c61165b8cdd0b498 Mon Sep 17 00:00:00 2001 From: ankitabmungalpara Date: Tue, 4 Mar 2025 14:20:35 -0500 Subject: [PATCH 2/2] Create second-highest-salary.py --- second-highest-salary.py | 82 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 second-highest-salary.py diff --git a/second-highest-salary.py b/second-highest-salary.py new file mode 100644 index 0000000..f5d482c --- /dev/null +++ b/second-highest-salary.py @@ -0,0 +1,82 @@ +""" + +Table: Employee + ++-------------+------+ +| Column Name | Type | ++-------------+------+ +| id | int | +| salary | int | ++-------------+------+ +id is the primary key (column with unique values) for this table. +Each row of this table contains information about the salary of an employee. + + +Write a solution to find the second highest distinct salary from the Employee table. If there is no second highest salary, return null (return None in Pandas). + +The result format is in the following example. + + +Example 1: + +Input: +Employee table: ++----+--------+ +| id | salary | ++----+--------+ +| 1 | 100 | +| 2 | 200 | +| 3 | 300 | ++----+--------+ +Output: ++---------------------+ +| SecondHighestSalary | ++---------------------+ +| 200 | ++---------------------+ + +Example 2: + +Input: +Employee table: ++----+--------+ +| id | salary | ++----+--------+ +| 1 | 100 | ++----+--------+ +Output: ++---------------------+ +| SecondHighestSalary | ++---------------------+ +| null | ++---------------------+ + +""" + +# This function finds the second highest salary from the given employee DataFrame. +# It removes duplicate salaries, checks if there are at least two unique values, and sorts them in descending order. +# Finally, it extracts and returns the second highest salary while renaming the column appropriately. + +import pandas as pd +import numpy as np + +def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame: + + # 1. Drop any duplicate salaries. + employee = employee.drop_duplicates(["salary"]) + + # 2. If there are less than two unique salaries, return `np.NaN`. + if len(employee["salary"].unique()) < 2: + return pd.DataFrame({"SecondHighestSalary": [np.NaN]}) + + # 3. Sort the table by `salary` in descending order. + employee = employee.sort_values("salary", ascending=False) + + # 4. Drop the `id` column. + employee.drop("id", axis=1, inplace=True) + + # 5. Rename the `salary` column. + employee.rename({"salary": "SecondHighestSalary"}, axis=1, inplace=True) + + # 6, 7. Return the second highest salary. + return employee.head(2).tail(1)