-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrise_people.rb
141 lines (112 loc) · 3.76 KB
/
rise_people.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
organization_employees = [
{
"name": "Frank",
"lastName": "Lu",
"role": "VP of Sales",
"userId": 1,
"managerId": 7,
"salary": 100000
},
{
"name": "Holly",
"lastName": "Wilson",
"role": "Head of HR",
"userId": 2,
"managerId": 7,
"salary": 60000
},
{
"name": "Jack",
"lastName": "Ryan",
"role": "Receptionist",
"userId": 3,
"managerId": 2,
"salary": 40000
},
{
"name": "Mike",
"lastName": "Rochester",
"role": "Account Executive",
"userId": 4,
"managerId": 1,
"salary": 70000
},
{
"name": "Priya",
"lastName": "Sylva",
"role": "Account Executive",
"userId": 5,
"managerId": 1,
"salary": 68000
},
{
"name": "Randy",
"lastName": "Shaw",
"role": "Account Executive",
"userId": 6,
"managerId": 1,
"salary": 65000
},
{
"name": "Alice",
"lastName": "Olson",
"role": "CEO",
"userId": 7,
"managerId": nil,
"salary": 200000
}
]
# An organization list of employees and their relationship is stored in a JSON object with the following format with no particular order.
# The above is just an example, the list can be much larger.
# userId is a numeric value showing a unique identified for the employee. ManagerId is the user ID of the person that an employee reports to. For instance, Priya Sylva reports to Frank Lu because her managerId is 1 and Frank’s userId is 1.
# managerId of null signifies that the person reports to nobody.
# 1) Write a function called printTopPaid that gets the organization JSON object (array of objects) and a role as the input and
# prints out the 2 top paid people’s first and last name. For instance, if we ran the function like the following with organizationEmployees being the example JSON above:
# printTopPaid(organizationEmployees, “Account Executive“)
# the output of the function should be
# Mike Rochester
# Priya Sylva
def print_top_paid(employees, role)
first = {salary: 0}
second = {salary: 0}
employees.each do |employee|
next if employee[:role] != role
if(employee[:salary] > first[:salary])
second = first.dup
first[:salary] = employee[:salary]
first[:name] = employee[:name] + " " + employee[:lastName]
elsif employee[:salary] > second[:salary] && employee[:salary] < first[:salary]
second[:salary] = employee[:salary]
second[:name] = employee[:name]
end
end
puts first[:name]
puts second[:name]
end
# print_top_paid(organization_employees, "Account Executive")
# 2) Write a function called printRoleSalarySum that gets the organization JSON (array of objects) as the input
# and prints out the roles ordered by the sum of salary paid. For the above example object the results would be:
# Account Executive - 203000
# CEO - 200000
# VP of Sales - 100000
# Head of HR - 60000
# Receptionist - 40000
def print_role_salary_sum(employees)
output = Hash.new(0)
employees.each do |employee|
role = employee[:role]
output[role] += employee[:salary]
end
output.sort_by{|key, value| value}.reverse.each{|ar| puts "#{ar[0]} - #{ar[1]}" }
end
print_role_salary_sum(organization_employees)
3) Write a function called printOrg that gets the organization JSON (array of objects) as input and outputs
the organizational chart in a textual format respecting hierarchy. For the example above the org chart would
look like this.
Alice Olson - CEO
Frank Lu - VP of Sales
Holly Wilson - Head of HR
Mike Rochester - Account Executive
Priya Sylva - Account Executive
Randy Shaw - Account Executive
Jack Ryan - Receptionist