Spaces:
Sleeping
Sleeping
| 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 | |