Class: AdditionOperationDifficultyCalculator

Inherits:
OperationDifficultyCalculator show all
Defined in:
lib/hypatia/difficulty-calculator.rb

Constant Summary collapse

DIFFICULTIES =
{
  digit_zero: 0,   # any digit added to zero
  under_ten: 3,   # sum of even digits
  ten: 2,   # sum of odd digits   # sum of even and odd digits
  above_ten: 4    # difficulty of carry (for remembering and then adding)
}

Instance Attribute Summary

Attributes inherited from OperationDifficultyCalculator

#difficulty

Instance Method Summary collapse

Methods inherited from OperationDifficultyCalculator

#initialize

Constructor Details

This class inherits a constructor from OperationDifficultyCalculator

Instance Method Details

#compare(numbersa, numbersb) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/hypatia/difficulty-calculator.rb', line 179

def compare(numbersa, numbersb)
  numbersa.reverse.each_with_index do |a , index|
    b = numbersb.reverse[index]
    case
    when a == nil || b == nil
      @difficulty += DIFFICULTIES[:digit_zero]
    when a == 0 || b == 0
      @difficulty += DIFFICULTIES[:digit_zero]
    when (a + b) == 10
      @difficulty += DIFFICULTIES[:ten]
    when (a + b) > 10
      @difficulty += DIFFICULTIES[:above_ten]
    else
      @difficulty += DIFFICULTIES[:under_ten]
    end
  end
end

#compute_difficultyObject



170
171
172
173
174
175
176
177
# File 'lib/hypatia/difficulty-calculator.rb', line 170

def compute_difficulty
  if @numbers1.length >= @numbers2.length
    compare(@numbers1, @numbers2)
  else
    compare(@numbers2, @numbers1)
  end
  @difficulty
end