Commit bb441714 authored by Owen Ilagan's avatar Owen Ilagan

Hashes exercise

parent 81b59891
# EXERCISE #4
data = {'2010' =>
{'Anabelle' => {'output' => 80, 'training' => 24, 'salary' => 8384},
'Benedict' => {'output' => 15, 'training' => 34, 'salary' => 7090},
'Charlie' => {'output' => 81, 'training' => 82, 'salary' => 3482},
'Dorothy' => {'output' => 63, 'training' => 8, 'salary' => 6057}
},
'2011' =>
{'Anabelle' => {'output' => 67, 'training' => 68, 'salary' => 6858},
'Benedict' => {'output' => 9, 'training' => 49, 'salary' => 9505},
'Charlie' => {'output' => 48, 'training' => 48, 'salary' => 9987},
'Dorothy' => {'output' => 25, 'training' => 28, 'salary' => 5002}
},
'2012' =>
{'Anabelle' => {'output' => 33, 'training' => 24, 'salary' => 3580},
'Benedict' => {'output' => 27, 'training' => 54, 'salary' => 2191},
'Charlie' => {'output' => 11, 'training' => 10, 'salary' => 6340},
'Dorothy' => {'output' => 93, 'training' => 99, 'salary' => 5454}
},
'2013' =>
{'Anabelle' => {'output' => 31, 'training' => 81, 'salary' => 1450},
'Benedict' => {'output' => 5, 'training' => 42, 'salary' => 5791},
'Charlie' => {'output' => 35, 'training' => 15, 'salary' => 415},
'Dorothy' => {'output' => 52, 'training' => 98, 'salary' => 2366}
},
'2014' =>
{'Anabelle' => {'output' => 54, 'training' => 32, 'salary' => 9086},
'Benedict' => {'output' => 8, 'training' => 32, 'salary' => 1750},
'Charlie' => {'output' => 16, 'training' => 74, 'salary' => 9400},
'Dorothy' => {'output' => 19, 'training' => 52, 'salary' => 7050}
},
'2015' =>
{'Anabelle' => {'output' => 4, 'training' => 37, 'salary' => 3054},
'Benedict' => {'output' => 81, 'training' => 83, 'salary' => 2808},
'Charlie' => {'output' => 33, 'training' => 36, 'salary' => 5547},
'Dorothy' => {'output' => 53, 'training' => 42, 'salary' => 3677}
}
}
def get_average_salary_per_employee(data)
result = Hash.new
years = 0
data.each do |key, yeardata|
yeardata.each do |employee, empdata|
sum = result[employee]
if sum.nil?
sum = 0
end
sum += empdata['salary']
result[employee] = sum
end
years += 1
end
result.each do |employee, salarydata|
result[employee] /= years
end
return result
end
puts "Average Salary Per Employee: " + get_average_salary_per_employee(data).to_s
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment