Sponsored
Sponsored
This approach involves iterating through each employee, checking if their employee_id is odd and their name does not start with 'M'. If both conditions are met, the bonus is set to their full salary; otherwise, it is set to 0. Loop through each employee and apply these checks systematically.
Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(n), for storing the results.
1class Employee:
2 def __init__(self, employee_id, name, salary):
3 self.employee_id = employee_id
4 self.name = name
5 self.salary = salary
6
7
8def calculate_bonus(employees):
9 results = []
10 for employee in employees:
11 bonus = employee.salary if employee.employee_id % 2 == 1 and not employee.name.startswith('M') else 0
12 results.append((employee.employee_id, bonus))
13 return results
14
15
16employees = [
17 Employee(2, "Meir", 3000),
18 Employee(3, "Michael", 3800),
19 Employee(7, "Addilyn", 7400),
20 Employee(8, "Juan", 6100),
21 Employee(9, "Kannon", 7700)
22]
23
24results = calculate_bonus(employees)
25
26print("+-------------+-------+")
27print("| employee_id | bonus |")
28print("+-------------+-------+")
29for employee_id, bonus in results:
30 print(f"| {employee_id:11} | {bonus:5} |")
31print("+-------------+-------+")
This Python code defines an Employee class. It iterates through each employee and checks the bonus logic using a conditional expression. Results are stored in a list of tuples, which are printed at the end.
This approach utilizes functional programming paradigms such as map/filter. It maps through each row of data, filters the rows based on the bonus eligibility criteria, and transforms them with the mapped values.
Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(n), for storing the results.
1class Employee {
2 constructor(employeeId, name
In this JavaScript solution, map is used similarly to apply the transformation across the employee list. The solution calculates the bonus conditionally and stores it in the result array.