Sobes.tech
Junior — Middle+

Audit of the salary average parameters calculation code

livecode

Task condition

Conduct a code review of the project that:

  • accepts a payload with employee information (age, salary);
  • calculates:
    • average age of staff;
    • average salary;
    • average salary increase compared to given baseline values;
    • employee with the highest salary;
    • employee with the lowest salary;
  • verifies correctness through assert.
import json
import math

payload = [
    {"id": 1, "name": "Laith", "surname": "Simmons", "age": 68, "salary": "£27888"},
    {"id": 2, "name": "Mikayla", "surname": "Henry", "age": 49, "salary": "¥67137"},
    {"id": 3, "name": "Garth", "surname": "Fields", "age": 70, "salary": "¥70472"}
]

class Data:
    def do(self, data, iage, isalary):
        average_age_increase = math.floor(
            sum(e['age'] for e in data) / len(data)
        ) - iage
        average_salary_increase = math.floor(
            sum(int(e['salary'][1:]) for e in data) / len(data)
        ) - isalary
        yearly_avg_increase = math.floor(
            average_salary_increase / average_age_increase
        )
        salaries = [int(e['salary'][1:]) for e in data]
        threshold_max = str(max(salaries))
        max_salary_person = [e for e in data if e['salary'][1:] == threshold_max]
        threshold_min = str(min(salaries))
        min_salary = [e for e in data if e['salary'][1:] == threshold_min]
        
        return json.dumps({
            'avg_age': math.floor(sum(e['age'] for e in data) / len(data)),
            'avgSalary': math.floor(
                sum(int(e['salary'][1:]) for e in data) / len(data)
            ),
            'avg_yearly_increase': yearly_avg_increase,
            'max_salary': max_salary_person,
            'min_salary': min_salary,
        })

ds = Data()
calculation_result = ds.do(payload, 20, 20000)
test_result = json.dumps({
    'avg_age': 62,
    'avgSalary': 55165,
    'avg_yearly_increase': 837,
    'max_salary': [{
        'id': 3,
        'name': 'Garth',
        'surname': 'Fields',
        'age': 70,
        'salary': '¥70472'
    }],
    'min_salary': [{
        'id': 1,
        'name': 'Laith',
        'surname': 'Simmons',
        'age': 68,
        'salary': '£27888'
    }]
})
assert calculation_result == test_result  # True