Last active
December 26, 2015 03:08
-
-
Save tundal45/7083368 to your computer and use it in GitHub Desktop.
Revisions
-
tundal45 revised this gist
Oct 21, 2013 . 3 changed files with 36 additions and 26 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ def get_grade(array) sum = 0 array.each do |x| sum += x end average = sum / array.length case average when 90...100 grade = 'A' when 80...90 grade = 'B' when 70...80 grade = 'C' when 60...70 grade = 'D' when 50...60 grade = 'E' when 0...50 grade = 'F' else "Error" end end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ def get_grade(hash) # replace array with a hash as a parameter hash = {A: 90..100, B: 80...90, C: 70...80, D: 60...70, E: 50...60, F: 0...50} # create a hash that pairs keys (letter grade) with values (numerical range). Can you use "..." as a value in a hash? sum = 0 # default the sum to 0 hash.each do |key, val| # iterate and say that in the hash, for each value, you're going to add that value to the sum sum += val # incrimenting the value to the sum end average = sum / val.length # defining average, which is the sum divided by the value length (val.length) # not sure what to do here! end end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,32 +7,7 @@ For example, # How studious! get_grade([100, 100, 100]) # => 'A' Submitted the code in the gist below and it works: Now I have to create a hash per the new exercise: -
gsperka created this gist
Oct 20, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ Create a method get_grade that accepts an Array of test scores. Each score in the array should be between 0 and 100, where 100 is the max score. Compute the average score and return the letter grade as a String, i.e., 'A', 'B', 'C', 'D', 'E', or 'F'. For example, # How studious! get_grade([100, 100, 100]) # => 'A' Submitted this and it works: def get_grade(array) sum = 0 array.each do |x| sum += x end average = sum / array.length case average when 90...100 grade = 'A' when 80...90 grade = 'B' when 70...80 grade = 'C' when 60...70 grade = 'D' when 50...60 grade = 'E' when 0...50 grade = 'F' else "Error" end end Now I have to create a hash per the new exercise: If you implemented the chosen challenge with an Array, is it possible to implement with a Hash? Or vice versa? Follow the same objectives below, and answer the questions in bold in your gist. def get_grade(hash) # replace array with a hash as a parameter hash = {A: 90..100, B: 80...90, C: 70...80, D: 60...70, E: 50...60, F: 0...50} # create a hash that pairs keys (letter grade) with values (numerical range). Can you use "..." as a value in a hash? sum = 0 # default the sum to 0 hash.each do |key, val| # iterate and say that in the hash, for each value, you're going to add that value to the sum sum += val # incrimenting the value to the sum end average = sum / val.length # defining average, which is the sum divided by the value length (val.length) # not sure what to do here! end end