Spaces:
Sleeping
Sleeping
File size: 1,451 Bytes
03a907a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | def calculate_employee_bonus(employees: list[dict], metrics: dict) -> list[dict]:
"""
Calculate employee bonuses based on their base salary, performance rating,
and company-wide metrics.
employees: list of dicts with 'id', 'role', 'base_salary', 'rating' (1-5)
metrics: dict with 'company_multiplier' and 'department_multipliers'
Returns a list of dicts with 'id' and 'bonus'.
"""
results = []
for emp in employees:
# BUG 1: Division by zero risk if rating is 0 or missing, and type mismatch if salary is string
base = emp.get('base_salary', 0)
rating = emp.get('rating', 1)
# BUG 2: Incorrect logic for role based multiplier, using assignment instead of lookup
role_mult = metrics.get('department_multipliers', {})[emp.get('role')] # will raise KeyError if role not found
# Calculate base bonus
if rating > 3:
base_bonus = base * 0.1
elif rating == 3:
base_bonus = base * 0.05
else:
base_bonus = 0
# BUG 3: Does not apply company multiplier correctly to the total
total_bonus = base_bonus * role_mult + metrics.get('company_multiplier', 1)
# BUG 4: mutating original dict instead of creating new one
emp['bonus'] = total_bonus
results.append(emp)
return results
|