From 190db4c7f0b40244f5a599a443d286874c420230 Mon Sep 17 00:00:00 2001 From: Feminto Date: Thu, 12 Jun 2025 10:26:21 -0700 Subject: [PATCH 1/3] Adding solutions for all Pandas4 problems --- NthHighestSalary.py | 11 +++++++++++ SecondHighestSalary.py | 11 +++++++++++ 2 files changed, 22 insertions(+) create mode 100644 NthHighestSalary.py create mode 100644 SecondHighestSalary.py diff --git a/NthHighestSalary.py b/NthHighestSalary.py new file mode 100644 index 0000000..a602900 --- /dev/null +++ b/NthHighestSalary.py @@ -0,0 +1,11 @@ +import pandas as pd + +def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame: + unq_sal = sorted(employee['salary'].drop_duplicates(), reverse= True) + + #invalid cases + if len(unq_sal) < N or N <= 0: + return pd.DataFrame({f'getNthHighestSalary({N})':[None]}) + + nth_sal = unq_sal[N-1] # Nth highest will be calculated as N-1 since the Indexing starts at 0. Eg. 2nd hishest will be at Index 1 + return pd.DataFrame({f'getNthHighestSalary({N})':[nth_sal]}) \ No newline at end of file diff --git a/SecondHighestSalary.py b/SecondHighestSalary.py new file mode 100644 index 0000000..3f130a9 --- /dev/null +++ b/SecondHighestSalary.py @@ -0,0 +1,11 @@ +import pandas as pd + +def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame: + unq_sal = sorted(employee['salary'].drop_duplicates(), reverse = True) + + #invalid cases + if len(unq_sal) < 2: + return pd.DataFrame({'SecondHighestSalary':[None]}) + + second_sal = unq_sal[1] # 2nd highest is at Index 1 + return pd.DataFrame({'SecondHighestSalary':[second_sal]}) \ No newline at end of file From 479dd670132860ae9a8da1108caca2642d10f902 Mon Sep 17 00:00:00 2001 From: Feminto Date: Thu, 12 Jun 2025 10:45:26 -0700 Subject: [PATCH 2/3] Adding method 2 for solving Nth Highest Salary problem --- NthHighestSalary.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/NthHighestSalary.py b/NthHighestSalary.py index a602900..405d1ab 100644 --- a/NthHighestSalary.py +++ b/NthHighestSalary.py @@ -3,6 +3,30 @@ def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame: unq_sal = sorted(employee['salary'].drop_duplicates(), reverse= True) + #invalid cases + if len(unq_sal) < N or N <= 0: + return pd.DataFrame({f'getNthHighestSalary({N})':[None]}) + + nth_sal = unq_sal[N-1] # Nth highest will be calculated as N-1 since the Indexing starts at 0. Eg. 2nd hishest will be at Index 1 + return pd.DataFrame({f'getNthHighestSalary({N})':[nth_sal]}) + + +# Method 2 +import pandas as pd + +def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame: + r_set = set() + for i in range(len(employee)): + sal = employee['salary'][i] + r_set.add(sal) + + # print(r_set) + result = [] + for i in r_set: + result.append(i) + + unq_sal = sorted(result, reverse = True) + #invalid cases if len(unq_sal) < N or N <= 0: return pd.DataFrame({f'getNthHighestSalary({N})':[None]}) From 7e10e8c2642892da513739a257c26d120b4b514e Mon Sep 17 00:00:00 2001 From: Feminto Date: Thu, 12 Jun 2025 19:35:50 -0700 Subject: [PATCH 3/3] Adding comments within the code of Nth Highest Salary solution --- NthHighestSalary.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/NthHighestSalary.py b/NthHighestSalary.py index 405d1ab..4ebf315 100644 --- a/NthHighestSalary.py +++ b/NthHighestSalary.py @@ -1,3 +1,4 @@ +#Method 1 import pandas as pd def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame: @@ -6,11 +7,16 @@ def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame: #invalid cases if len(unq_sal) < N or N <= 0: return pd.DataFrame({f'getNthHighestSalary({N})':[None]}) - - nth_sal = unq_sal[N-1] # Nth highest will be calculated as N-1 since the Indexing starts at 0. Eg. 2nd hishest will be at Index 1 + else: + nth_sal = unq_sal[N-1] # Nth highest will be calculated as N-1 since the Indexing starts at 0. Eg. 2nd hishest will be at Index 1 + + # if invalid case is evaluated later, it causes Runtime Error: + # nth_sal will try to access an index that might not exist in unq_sal. + # If N is invalid (e.g., N > len(unq_sal) or N <= 0), then: unq_sal[N-1] throws an IndexError. + return pd.DataFrame({f'getNthHighestSalary({N})':[nth_sal]}) - + # Method 2 import pandas as pd