Skip to content

Instantly share code, notes, and snippets.

@tundal45
Last active December 26, 2015 03:08
Show Gist options
  • Select an option

  • Save tundal45/7083368 to your computer and use it in GitHub Desktop.

Select an option

Save tundal45/7083368 to your computer and use it in GitHub Desktop.

Revisions

  1. tundal45 revised this gist Oct 21, 2013. 3 changed files with 36 additions and 26 deletions.
    23 changes: 23 additions & 0 deletions get_grade_array.rb
    Original 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
    12 changes: 12 additions & 0 deletions get_grade_hash.rb
    Original 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
    27 changes: 1 addition & 26 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -7,32 +7,7 @@ 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

    Submitted the code in the gist below and it works:

    Now I have to create a hash per the new exercise:

  2. @gsperka gsperka created this gist Oct 20, 2013.
    52 changes: 52 additions & 0 deletions gistfile1.txt
    Original 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