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.
1using System;
2using System.Collections.Generic;
3
4public class Employee {
5 public int EmployeeId { get; set; }
6 public string Name { get; set; }
7 public int Salary { get; set; }
8}
9
10public class Result {
11 public int EmployeeId { get; set; }
12 public int Bonus { get; set; }
13}
14
15public class CalculateBonus {
16 public static List<Result> GetBonuses(List<Employee> employees) {
17 List<Result> results = new List<Result>();
18 foreach (var employee in employees) {
19 int bonus = 0;
20 if (employee.EmployeeId % 2 != 0 && !employee.Name.StartsWith("M")) {
21 bonus = employee.Salary;
22 }
23 results.Add(new Result { EmployeeId = employee.EmployeeId, Bonus = bonus });
24 }
25 return results;
26 }
27
28 public static void Main() {
29 List<Employee> employees = new List<Employee> {
30 new Employee { EmployeeId = 2, Name = "Meir", Salary = 3000 },
31 new Employee { EmployeeId = 3, Name = "Michael", Salary = 3800 },
32 new Employee { EmployeeId = 7, Name = "Addilyn", Salary = 7400 },
33 new Employee { EmployeeId = 8, Name = "Juan", Salary = 6100 },
34 new Employee { EmployeeId = 9, Name = "Kannon", Salary = 7700 }
35 };
36
37 List<Result> results = GetBonuses(employees);
38
39 Console.WriteLine("+-------------+-------+");
40 Console.WriteLine("| employee_id | bonus |");
41 Console.WriteLine("+-------------+-------+");
42 foreach (var result in results) {
43 Console.WriteLine($"| {result.EmployeeId,11} | {result.Bonus,5} |");
44 }
45 Console.WriteLine("+-------------+-------+");
46 }
47}
This C# program defines Employee and Result classes. It iterates through a list of employees, evaluates the conditions for bonus eligibility, and stores the results in a list. The output is formatted and displayed to match the requirements.
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 def __init__(self, employee_id
This Python solution uses lambda functions and 'map' to apply the bonus logic across all employees in a functional style approach without using an explicit loop. It's concise and leverages built-in Python capabilities.