Skip to content

Instantly share code, notes, and snippets.

@tlehman
Created October 21, 2014 19:25
Show Gist options
  • Select an option

  • Save tlehman/8c8a3b36309e53cbbba3 to your computer and use it in GitHub Desktop.

Select an option

Save tlehman/8c8a3b36309e53cbbba3 to your computer and use it in GitHub Desktop.
GPA calculation
def gpa(grades)
points = {:A => 4.0, :B => 3.0, :C => 2.0, :D => 1.0, :F => 0}
grade_values = grades.merge(points) { |grade,value,number| (number.to_f)*value }
numer = grades.values.inject(:+).to_f
denom = grade_values.values.inject(:+).to_f
((numer/denom)*10).round(3)
end
# ==> nil
gpa({:A => 740, :B => 89, :C => 38, :D => 34, :F => 34}) # 2013 Mar 27
# ==> 2.802
gpa({:A => 862, :B => 85, :C => 53, :D => 35, :F => 37}) # 2013 Sep 13
# ==> 2.789
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment