Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions NthHighestSalary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#Method 1
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]})
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

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]})

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]})
11 changes: 11 additions & 0 deletions SecondHighestSalary.py
Original file line number Diff line number Diff line change
@@ -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]})