Skip to content

Commit cdae8e4

Browse files
committed
Add new file
1 parent edc2b0b commit cdae8e4

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
1873. Calculate Special Bonus
3+
Solved
4+
Easy
5+
Topics
6+
Companies
7+
SQL Schema
8+
Pandas Schema
9+
10+
Table: Employees
11+
12+
+-------------+---------+
13+
| Column Name | Type |
14+
+-------------+---------+
15+
| employee_id | int |
16+
| name | varchar |
17+
| salary | int |
18+
+-------------+---------+
19+
employee_id is the primary key (column with unique values) for this table.
20+
Each row of this table indicates the employee ID, employee name, and salary.
21+
22+
Write a solution to calculate the bonus of each employee. The bonus of an employee is 100% of their salary if the ID of the employee is an odd number and the employee's name does not start with the character 'M'. The bonus of an employee is 0 otherwise.
23+
24+
Return the result table ordered by employee_id.
25+
26+
The result format is in the following example.
27+
28+
Example 1:
29+
30+
Input:
31+
Employees table:
32+
+-------------+---------+--------+
33+
| employee_id | name | salary |
34+
+-------------+---------+--------+
35+
| 2 | Meir | 3000 |
36+
| 3 | Michael | 3800 |
37+
| 7 | Addilyn | 7400 |
38+
| 8 | Juan | 6100 |
39+
| 9 | Kannon | 7700 |
40+
+-------------+---------+--------+
41+
Output:
42+
+-------------+-------+
43+
| employee_id | bonus |
44+
+-------------+-------+
45+
| 2 | 0 |
46+
| 3 | 0 |
47+
| 7 | 7400 |
48+
| 8 | 0 |
49+
| 9 | 7700 |
50+
+-------------+-------+
51+
Explanation:
52+
The employees with IDs 2 and 8 get 0 bonus because they have an even employee_id.
53+
The employee with ID 3 gets 0 bonus because their name starts with 'M'.
54+
The rest of the employees get a 100% bonus.
55+
"""
56+
57+
import pandas as pd
58+
59+
def calculate_special_bonus(employees: pd.DataFrame) -> pd.DataFrame:
60+
employees['bonus'] = 0
61+
mask = (employees['employee_id'] % 2 == 1) & (~employees['name'].str.startswith('M'))
62+
employees.loc[mask, 'bonus'] = employees.loc[mask, 'salary']
63+
return employees[['employee_id','bonus']].sort_values('employee_id')
64+
65+
"""
66+
for i, row in employees.iterrows():
67+
if (row['employee_id'] % 2 == 1) & (~row['name'].startswith('M')):
68+
employees.at[i, 'bonus'] = employees.at[i, 'salary']
69+
else:
70+
employees.at[i, 'bonus'] = 0
71+
return employees[['employee_id','bonus']].sort_values('employee_id')
72+
"""

0 commit comments

Comments
 (0)