 svmartin
9/2/2016 - 7:10 PM

## math_using_english.rb

``````NUMBERS = { "zero" => 0, "one" => 1, "two" => 2, "three" => 3, "four" => 4,
"five" => 5, "six" => 6, "seven" => 7, "eight" => 8, "nine" => 9}

OPERATIONS = { "plus" => :+, "minus" => :-, "divided" => :/, "times" => :* }

def remove_by!(string)
string.delete!('by')
end

def get_integers_and_operators(string)
string.split.map do |word|
if NUMBERS.include?(word)
NUMBERS[word]
elsif OPERATIONS.include?(word)
OPERATIONS[word]
else
word.to_i
end
end
end

def computer(string)
remove_by!(string)
array = get_integers_and_operators(string)
eval(array.join(' '))
end

p computer("two plus two") # => 4
p computer("seven minus six") # => 1
p computer("zero plus eight") # => 8
p computer("two plus two minus three") # => 1
p computer("three minus one plus five minus 4 plus six plus 10 minus 4") # => 15
p computer("eight times four plus six divided by two minus two") # => 33

p "====="

p computer("one plus four times two minus two") # => 7
p computer("nine divided by three times six") # => 18
p computer("seven plus four divided by two") # => 9
p computer("seven times four plus one divided by three minus two") # => 26
p computer("one plus four times three divided by two minus two") # => 5
p computer("nine divided by three times six") # => 18``````