Last active
December 21, 2015 23:18
-
-
Save ybur-yug/6380910 to your computer and use it in GitHub Desktop.
Takes input in the form of "what is 5 plus 9" "what is 9 multiplied by 13" etc. words with * - / + for single operations or multiple of the same operation.
This file contains 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 characters
def wordy_calc(question) | |
i = 0 | |
operation_plus = question.match(/(plus)/).to_s | |
operation_minus = question.match(/(minus)/).to_s | |
operation_times = question.match(/(multiplied)/).to_s | |
operation_div = question.match(/(divided)/).to_s | |
question_arr = question.split(" ") | |
question_arr.each do |word| | |
word.gsub!(/\D/, '') | |
end | |
question_arr.delete("") | |
question_arr.map! do |num| | |
num = num.to_f | |
end | |
operation_plus == "plus" ? answer = question_arr.inject{|sum,x| sum + x } : i += 1 | |
operation_minus == "minus" ? answer = question_arr.inject{|diff,x| diff - x } : i += 1 | |
operation_times == "multiplied" ? answer = question_arr.inject{|prod,x| prod * x } : i += 1 | |
operation_div == "divided" ? answer = question_arr.inject{|div,x| div / x } : i += 1 | |
answer | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment