From 26bae21c991afa2e0996fd7e99cdfefdd612a298 Mon Sep 17 00:00:00 2001 From: Anjum Kausar <47412591+anjumkausar@users.noreply.github.com> Date: Mon, 2 Sep 2024 12:46:01 -0700 Subject: [PATCH 1/2] Create Problem 1: Nth Highest Salary --- Problem 1: Nth Highest Salary | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Problem 1: Nth Highest Salary diff --git a/Problem 1: Nth Highest Salary b/Problem 1: Nth Highest Salary new file mode 100644 index 0000000..5fc0bfd --- /dev/null +++ b/Problem 1: Nth Highest Salary @@ -0,0 +1,33 @@ +import pandas as pd + +def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame: + #Using Pandas + df = employee[['salary']].drop_duplicates() + if N > len(df) or N<=0: + return pd.DataFrame({f'getNthHighestSalary({N})' : [None]}) + return df.sort_values('salary', ascending = False).head(N).tail(1)[['salary']].rename(columns = {'salary': f'getNthHighestSalary({N})'}) + + #Using Python + result = [] + for i in range(len(employee)): + salary = employee['salary'][i] + if salary not in result: + result.append(salary) + result.sort(reverse = True) + if N > len(result) or N <= 0: + return pd.DataFrame ({f'getNthHighestSalary({N})' : [None]}) + return pd.DataFrame({f'getNthHighestSalary({N})' : [result[N-1]]}) + + #Using Set + result_set = set() + for i in range(len(employee)): + salary = employee['salary'][i] + result_set.add(salary) + result = [] + for element in result_set: + result.append(element) + result.sort(reverse = True) + if N > len(result) or N <= 0: + return pd.DataFrame ({f'getNthHighestSalary({N})' : [None]}) + return pd.DataFrame({f'getNthHighestSalary({N})' : [result[N-1]]}) + From 95e0c47f55feca74e7c4a05eefe88e3f821a2911 Mon Sep 17 00:00:00 2001 From: Anjum Kausar <47412591+anjumkausar@users.noreply.github.com> Date: Thu, 12 Sep 2024 19:28:15 -0700 Subject: [PATCH 2/2] Create Problem 2: Second Highest Salary --- Problem 2: Second Highest Salary | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Problem 2: Second Highest Salary diff --git a/Problem 2: Second Highest Salary b/Problem 2: Second Highest Salary new file mode 100644 index 0000000..ec96f21 --- /dev/null +++ b/Problem 2: Second Highest Salary @@ -0,0 +1,25 @@ +import pandas as pd + +def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame: + all_salaries = set() + for i in range(len(employee)): + salary = employee ['salary'][i] + all_salaries.add(salary) + result = [] + for salary in all_salaries: + result.append(salary) + result.sort(reverse = True) + if len(result)<2: + return pd.DataFrame([None], columns = ['SecondHighestSalary']) + return pd.DataFrame([result[1]], columns = ['SecondHighestSalary']) + + #Using Pandas + employee = employee.drop_duplicates(['salary']) + if len(employee) < 2: + return pd.DataFrame({'SecondHighestSalary' : [np.NaN]}) + employee.sort_values(by=['salary'], ascending = False, inplace= True) + employee.drop('id', axis = 1, inplace= True) + employee.rename(columns = {'salary': 'SecondHighestSalary'}, inplace= True) + return employee.head(2).tail(1) + +