Last active
February 9, 2022 14:23
-
-
Save h2rashee/620a076def224903d2cf366a7dbd1026 to your computer and use it in GitHub Desktop.
Rippling: Perform operations on employee salaries with certain fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rippling is at its core an employee database. | |
# Imagine if you’re at the early days of Rippling and you have admins asking for features around | |
# data reporting. | |
# The idea is that we want to build a system that allows admins to calculate values across many dimensions. | |
# Write a function that takes data blob as input (see example below) and calculates the sum | |
# of salaries in each department. | |
# [ | |
# { | |
# "id": "583fd999a2a3a4121dd6b6fc", | |
# "name": "Brook Quigley", | |
# "employmentType": "HOURLY_FT", | |
# "dob": "1993-04-31", | |
# "identifiedGender": "FEMALE", | |
# "title": "Sales Development Rep", | |
# "compensation": { | |
# "currency": "USD", | |
# "annualSalary": 65000.00 | |
# }, | |
# "department": "Sales", | |
# "workLocation": "San Francisco", | |
# "startDate": "2020-05-31" | |
# }, | |
# { | |
# "id": "583fd982a2a3a4121dd6b429", | |
# "name": "John Grady", | |
# "employmentType": "HOURLY_FT", | |
# "dob": "1996-05-31", | |
# "identifiedGender": "MALE", | |
# "title": "Customer Support Associate", | |
# "compensation": { | |
# "currency": "USD", | |
# "annualSalary": 52000.00 | |
# }, | |
# "department": "Customer Support", | |
# "workLocation": "San Francisco", | |
# "startDate": "2019-05-31" | |
# } | |
# ] | |
CURRENCY_CONVERSION = { | |
"INR" : 70, | |
"EUR": 0.89 | |
} | |
class EmployeeSalaries: | |
def __init__(self, employee_db): | |
self.department_salaries = {} | |
for employee in employee_db: | |
employee_currency = employee['compensation']['currency'] | |
employee_annual_salary = employee['compensation']['annualSalary'] | |
employee_department = employee['department'] | |
salary_in_usd = employee_annual_salary if employee_currency == "USD" else employee_annual_salary * CURRENCY_CONVERSION[employee_currency] | |
if employee_department not in self.department_salaries: | |
self.department_salaries[employee_department] = [salary_in_usd] | |
else: | |
self.department_salaries[employee_department].append(salary_in_usd) | |
def display(self): | |
return self.department_salaries | |
def get_total(self): | |
department_total_salary = {} | |
for dept in self.department_salaries.keys(): | |
department_total_salary[dept] = sum(self.department_salaries[dept]) | |
return department_total_salary | |
def get_mean(self): | |
department_mean_salary = {} | |
for dept in self.department_salaries.keys(): | |
total = sum(self.department_salaries[dept]) | |
department_mean_salary[dept] = total / len(self.department_salaries) | |
return department_mean_salary | |
def get_min(self): | |
department_min_salary = {} | |
for dept in self.department_salaries.keys(): | |
department_min_salary[dept] = min(self.department_salaries[dept]) | |
return department_min_salary | |
def get_max(self): | |
department_max_salary = {} | |
for dept in self.department_salaries.keys(): | |
department_max_salary[dept] = max(self.department_salaries[dept]) | |
return department_max_salary | |
def get_mode(self): | |
department_mode_salary = {} | |
for dept in self.department_salaries.keys(): | |
salary_count = {} | |
for salary in self.department_salaries[dept]: | |
if salary in salary_count: | |
salary_count[salary] = salary_count[salary] + 1 | |
else: | |
salary_count[salary] = 1 | |
# Salary -> Number of times shown | |
max_occurring_salary = None | |
for occurrence in salary_count.items(): | |
if max_occurring_salary is None or occurrence[1] > max_occurring_salary[1]: | |
max_occurring_salary = occurrence | |
department_mode_salary[dept] = max_occurring_salary[0] | |
return department_mode_salary | |
# average (mean), min, max, mode | |
test_1 = [ | |
{ | |
"id": "583fd999a2a3a4121dd6b6fc", | |
"name": "Brook Quigley", | |
"employmentType": "HOURLY_FT", | |
"dob": "1993-04-31", | |
"identifiedGender": "FEMALE", | |
"title": "Sales Development Rep", | |
"compensation": { | |
"currency": "USD", | |
"annualSalary": 65000.00 | |
}, | |
"department": "Sales", | |
"workLocation": "San Francisco", | |
"startDate": "2020-05-31" | |
}, | |
{ | |
"id": "583fd982a2a3a4121dd6b429", | |
"name": "John Grady", | |
"employmentType": "HOURLY_FT", | |
"dob": "1996-05-31", | |
"identifiedGender": "MALE", | |
"title": "Customer Support Associate", | |
"compensation": { | |
"currency": "USD", | |
"annualSalary": 52000.00 | |
}, | |
"department": "Customer Support", | |
"workLocation": "San Francisco", | |
"startDate": "2019-05-31" | |
} | |
] | |
test_3 = [ | |
{ | |
"id": "583fd999a2a3a4121dd6b6fc", | |
"name": "Brook Quigley", | |
"employmentType": "HOURLY_FT", | |
"dob": "1993-04-31", | |
"identifiedGender": "FEMALE", | |
"title": "Sales Development Rep", | |
"compensation": { | |
"currency": "INR", | |
"annualSalary": 10.00 | |
}, | |
"department": "Sales", | |
"workLocation": "San Francisco", | |
"startDate": "2020-05-31" | |
}, | |
{ | |
"id": "583fd999a2a3a4121dd6b6fc", | |
"name": "Brook Quigley", | |
"employmentType": "HOURLY_FT", | |
"dob": "1993-04-31", | |
"identifiedGender": "FEMALE", | |
"title": "Sales Development Rep", | |
"compensation": { | |
"currency": "USD", | |
"annualSalary": 10.00 | |
}, | |
"department": "Sales", | |
"workLocation": "San Francisco", | |
"startDate": "2020-05-31" | |
}, | |
{ | |
"id": "583fd982a2a3a4121dd6b429", | |
"name": "John Grady", | |
"employmentType": "HOURLY_FT", | |
"dob": "1996-05-31", | |
"identifiedGender": "MALE", | |
"title": "Customer Support Associate", | |
"compensation": { | |
"currency": "USD", | |
"annualSalary": 5000.00 | |
}, | |
"department": "Customer Support", | |
"workLocation": "San Francisco", | |
"startDate": "2019-05-31" | |
}, | |
{ | |
"id": "583fd982a2a3a4121dd6b429", | |
"name": "John Grady", | |
"employmentType": "HOURLY_FT", | |
"dob": "1996-05-31", | |
"identifiedGender": "MALE", | |
"title": "Customer Support Associate", | |
"compensation": { | |
"currency": "USD", | |
"annualSalary": 1.00 | |
}, | |
"department": "Customer Support", | |
"workLocation": "San Francisco", | |
"startDate": "2019-05-31" | |
}, | |
{ | |
"id": "583fd982a2a3a4121dd6b429", | |
"name": "John Grady", | |
"employmentType": "HOURLY_FT", | |
"dob": "1996-05-31", | |
"identifiedGender": "MALE", | |
"title": "Customer Support Associate", | |
"compensation": { | |
"currency": "USD", | |
"annualSalary": 5000.00 | |
}, | |
"department": "Customer Support", | |
"workLocation": "San Francisco", | |
"startDate": "2019-05-31" | |
} | |
] | |
test_2 = [] | |
es = EmployeeSalaries(test_3) | |
print(es.display()) | |
print(es.get_total()) | |
print(es.get_mean()) | |
print(es.get_min()) | |
print(es.get_max()) | |
print(es.get_mode()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment