From 214db785a8c03f911d7b359cd4d8de63559d42df Mon Sep 17 00:00:00 2001 From: ankitabmungalpara Date: Tue, 4 Mar 2025 14:22:23 -0500 Subject: [PATCH 1/2] Create department-highest-salary.py --- department-highest-salary.py | 85 ++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 department-highest-salary.py diff --git a/department-highest-salary.py b/department-highest-salary.py new file mode 100644 index 0000000..8f4b6c9 --- /dev/null +++ b/department-highest-salary.py @@ -0,0 +1,85 @@ +""" + +Table: Employee + ++--------------+---------+ +| Column Name | Type | ++--------------+---------+ +| id | int | +| name | varchar | +| salary | int | +| departmentId | int | ++--------------+---------+ +id is the primary key (column with unique values) for this table. +departmentId is a foreign key (reference columns) of the ID from the Department table. +Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department. + + +Table: Department + ++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| id | int | +| name | varchar | ++-------------+---------+ +id is the primary key (column with unique values) for this table. It is guaranteed that department name is not NULL. +Each row of this table indicates the ID of a department and its name. + + +Write a solution to find employees who have the highest salary in each of the departments. + +Return the result table in any order. + +The result format is in the following example. + + +Example 1: + +Input: +Employee table: ++----+-------+--------+--------------+ +| id | name | salary | departmentId | ++----+-------+--------+--------------+ +| 1 | Joe | 70000 | 1 | +| 2 | Jim | 90000 | 1 | +| 3 | Henry | 80000 | 2 | +| 4 | Sam | 60000 | 2 | +| 5 | Max | 90000 | 1 | ++----+-------+--------+--------------+ +Department table: ++----+-------+ +| id | name | ++----+-------+ +| 1 | IT | +| 2 | Sales | ++----+-------+ +Output: ++------------+----------+--------+ +| Department | Employee | Salary | ++------------+----------+--------+ +| IT | Jim | 90000 | +| Sales | Henry | 80000 | +| IT | Max | 90000 | ++------------+----------+--------+ +Explanation: Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department. + +""" + +# This function finds the employees with the highest salary in each department. +# It merges the employee and department tables, then identifies the maximum salary per department. +# Finally, it filters the employees who earn the highest salary in their respective departments and returns the relevant columns. + +import pandas as pd + +def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: + + # Merge tables and rename + df = employee.merge(department, left_on='departmentId', right_on='id', how='left') + df.rename(columns={'name_x': 'Employee', 'name_y': 'Department', 'salary': 'Salary'}, inplace=True) + + # Select employees whose salary is equal to the department highest salary + max_salary = df.groupby('Department')['Salary'].transform('max') + df = df[df['Salary'] == max_salary] + + return df[['Department', 'Employee', 'Salary']] From 24c492d24b4345d56ada148c6ada27eca3243886 Mon Sep 17 00:00:00 2001 From: ankitabmungalpara Date: Tue, 4 Mar 2025 14:23:44 -0500 Subject: [PATCH 2/2] Create rank-scores.py --- rank-scores.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 rank-scores.py diff --git a/rank-scores.py b/rank-scores.py new file mode 100644 index 0000000..216072b --- /dev/null +++ b/rank-scores.py @@ -0,0 +1,63 @@ +""" + +Table: Scores + ++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| id | int | +| score | decimal | ++-------------+---------+ +id is the primary key (column with unique values) for this table. +Each row of this table contains the score of a game. Score is a floating point value with two decimal places. + + +Write a solution to find the rank of the scores. The ranking should be calculated according to the following rules: + +The scores should be ranked from the highest to the lowest. +If there is a tie between two scores, both should have the same ranking. +After a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no holes between ranks. +Return the result table ordered by score in descending order. + +The result format is in the following example. + + +Example 1: + +Input: +Scores table: ++----+-------+ +| id | score | ++----+-------+ +| 1 | 3.50 | +| 2 | 3.65 | +| 3 | 4.00 | +| 4 | 3.85 | +| 5 | 4.00 | +| 6 | 3.65 | ++----+-------+ + +Output: ++-------+------+ +| score | rank | ++-------+------+ +| 4.00 | 1 | +| 4.00 | 1 | +| 3.85 | 2 | +| 3.65 | 3 | +| 3.65 | 3 | +| 3.50 | 4 | ++-------+------+ + +""" + +# This function ranks scores in descending order using the 'dense' ranking method, where ties receive the same rank and the next rank is sequential. +# It assigns ranks to the 'score' column, then selects only 'score' and 'rank' columns for the final output. +# The result is sorted by 'score' in descending order to maintain clarity. + +import pandas as pd + +def order_scores(scores: pd.DataFrame) -> pd.DataFrame: + scores['rank'] = scores['score'].rank(method='dense', ascending=False) + return scores[['score', 'rank']].sort_values('score', ascending=False) +