S3Q1 · Employee Data Analysis¶
⚡ Quick Reference
Four functions on employee records {name, department, salary}:
def employees_with_salary_above(employees, min_salary):
return [e["name"] for e in employees if e["salary"] >= min_salary]
def total_salary_in_department(employees, department):
return sum(e["salary"] for e in employees if e["department"] == department)
def ceil_to_five_hundreds(num):
if num % 500 == 0:
return num
return (num // 500 + 1) * 500
def max_salary_after_increment_in_department(employees, department, inc_percent):
dept_salaries = [e["salary"] for e in employees if e["department"] == department]
max_sal = max(dept_salaries)
incremented = round(max_sal * (1 + inc_percent / 100))
return ceil_to_five_hundreds(incremented)
Key rules:
- employees_with_salary_above: salary >= min_salary (inclusive), preserve order
- ceil_to_five_hundreds: if already a multiple of 500 → unchanged; else round up to next 500
- Increment formula: salary × (1 + inc_percent/100), then round(), then ceiling
Problem Statement¶
Problem
Implement four functions to analyse employee records.
Sample data:
employees = [
{"name": "Alice", "department": "HR", "salary": 50000},
{"name": "Bob", "department": "Engineering", "salary": 70000},
{"name": "Charlie", "department": "HR", "salary": 45000},
{"name": "David", "department": "Engineering", "salary": 60000},
{"name": "Eve", "department": "Marketing", "salary": 55000},
]
Function 1 - employees_with_salary_above¶
Return names of employees with salary >= min_salary, in order of appearance:
def employees_with_salary_above(employees: list, min_salary: int):
return [e["name"] for e in employees if e["salary"] >= min_salary]
From sample: min_salary=60000 → Bob(70000), David(60000) → ["Bob", "David"] ✓
Function 2 - total_salary_in_department¶
Sum salaries for employees in the given department:
def total_salary_in_department(employees: list, department: str):
return sum(e["salary"] for e in employees if e["department"] == department)
From sample: HR → Alice(50000) + Charlie(45000) = 95000 ✓
Function 3 - ceil_to_five_hundreds¶
Round up to the next multiple of 500. If already a multiple - return unchanged:
ceil_to_five_hundreds(25100):
25100 % 500 = 100 ≠ 0
(25100 // 500 + 1) * 500 = (50 + 1) * 500 = 25500 ✓
ceil_to_five_hundreds(24500):
24500 % 500 = 0 → return 24500 ✓
ceil_to_five_hundreds(24600):
24600 % 500 = 100 ≠ 0
(24600 // 500 + 1) * 500 = (49 + 1) * 500 = 25000 ✓
Function 4 - max_salary_after_increment_in_department¶
Find max salary in department → apply increment → round → ceiling:
def max_salary_after_increment_in_department(employees: list, department: str, inc_percent: int):
dept_salaries = [e["salary"] for e in employees if e["department"] == department]
max_sal = max(dept_salaries)
incremented = round(max_sal * (1 + inc_percent / 100))
return ceil_to_five_hundreds(incremented)
From sample: Engineering → max is Bob(70000), increment 7%:
- 70000 × 1.07 = 74900.0 → round(74900.0) = 74900
- ceil_to_five_hundreds(74900): 74900 % 500 = 400 ≠ 0 → (149 + 1) × 500 = 75000 ✓
Complete solution approaches¶
def employees_with_salary_above(employees: list, min_salary: int):
return [e["name"] for e in employees if e["salary"] >= min_salary]
def total_salary_in_department(employees: list, department: str):
return sum(e["salary"] for e in employees if e["department"] == department)
def ceil_to_five_hundreds(num: int):
if num % 500 == 0:
return num
return (num // 500 + 1) * 500
def max_salary_after_increment_in_department(employees: list, department: str, inc_percent: int):
salaries = [e["salary"] for e in employees if e["department"] == department]
incremented = round(max(salaries) * (1 + inc_percent / 100))
return ceil_to_five_hundreds(incremented)
def employees_with_salary_above(employees: list, min_salary: int):
result = []
for e in employees:
if e["salary"] >= min_salary:
result.append(e["name"])
return result
def total_salary_in_department(employees: list, department: str):
total = 0
for e in employees:
if e["department"] == department:
total += e["salary"]
return total
def ceil_to_five_hundreds(num: int):
remainder = num % 500
if remainder == 0:
return num
return num + (500 - remainder)
def max_salary_after_increment_in_department(employees: list, department: str, inc_percent: int):
max_sal = 0
for e in employees:
if e["department"] == department:
if e["salary"] > max_sal:
max_sal = e["salary"]
incremented = round(max_sal * (1 + inc_percent / 100))
return ceil_to_five_hundreds(incremented)
def employees_with_salary_above(employees: list, min_salary: int):
return list(map(lambda e: e["name"],
filter(lambda e: e["salary"] >= min_salary, employees)))
def total_salary_in_department(employees: list, department: str):
dept = filter(lambda e: e["department"] == department, employees)
return sum(map(lambda e: e["salary"], dept))
def ceil_to_five_hundreds(num: int):
return num if num % 500 == 0 else (num // 500 + 1) * 500
def max_salary_after_increment_in_department(employees: list, department: str, inc_percent: int):
salaries = list(map(lambda e: e["salary"],
filter(lambda e: e["department"] == department, employees)))
return ceil_to_five_hundreds(round(max(salaries) * (1 + inc_percent / 100)))
Key takeaways¶
ceil_to_five_hundreds - two equivalent formulas
(num // 500 + 1) * 500 floors to the current 500-block then jumps to the next. Alternatively, num + (500 - num % 500) adds the gap to the next multiple. Both give the same result; always guard with if num % 500 == 0: return num first.
Increment order: multiply → round → ceiling
Apply the percentage first (salary × (1 + pct/100)), then round() to the nearest integer, then ceil_to_five_hundreds(). Changing the order produces different results - follow the problem's specified sequence.
Reuse helper functions
max_salary_after_increment_in_department calls ceil_to_five_hundreds directly. Building on your own functions keeps logic in one place and avoids duplicating the ceiling logic.