Slide 16
Slide 16 text
@calculations = {
0 => -1, 1 => -2, 2 => 2, 3 => 1, 4 => 0,
5 => -1, 6 => -2, 7 => 2, 8 => 1, 9 => 0
}
def estimate(price)
last_digit = price.round % 10
return price + @calculations[last_digit]
end
puts estimate(8)
assertEquals(estimate(12.2), 14.2)
Approach D: hash table for the last digit
What makes for a good onsite coding exercise?