top of page
Calculate Special Bonus
LeetCode Problem 1873
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.
Return the result table ordered by employee_id.
import pandas as pd
data = [[2, 'Meir', 3000], [3, 'Michael', 3800], [7, 'Addilyn', 7400], [8, 'Juan', 6100], [9, 'Kannon', 7700]]
employees = pd.DataFrame(data, columns=['employee_id', 'name', 'salary']).astype({'employee_id':'int64', 'name':'object', 'salary':'int64'})
def calculate_special_bonus(employees):
employees['bonus'] = employees.apply(lambda row: row['salary'] if row['employee_id'] % 2 != 0 and not row['name'].startswith('M') else 0, axis=1)
return employees[['employee_id', 'bonus']].sort_values(by='employee_id')
result = calculate_special_bonus(employees)
print(result)
bottom of page