Created
August 29, 2013 00:53
-
-
Save broguinn/6373134 to your computer and use it in GitHub Desktop.
wordly_calculator
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
class Calculator | |
def phrase_cleaner(phrase) | |
operands = phrase.match(/([0-9,-]+.*[\w])/)[0] | |
end | |
def number_getter(phrase) | |
numbers = phrase.scan(/[0-9,-]+/) | |
numbers.map { |number| number.to_i } | |
end | |
def operator_getter(phrase) | |
operators = phrase.scan(/[0-9,-]+[\s](\S+)/) | |
operators.map { |operator| operator.pop } | |
end | |
def ask(question) | |
operators_gloss = { | |
"plus" => :plus, | |
"add" => :plus, | |
"minus" => :minus, | |
"take away" => :minus, | |
"times" => :multiply, | |
"divided" => :divide, | |
"divide" => :divide, | |
"multiplied" => :multiply, | |
"squared" => :square, | |
"cubed" => :cube, | |
"to" => :power, | |
} | |
operands = phrase_cleaner(question) | |
operators = operator_getter(operands) | |
numbers = number_getter(operands) | |
total ||= numbers.shift | |
operators.each do |operator| | |
operator = operators_gloss[operator] | |
case operator | |
when :plus | |
total += numbers.shift | |
when :minus | |
total -= numbers.shift | |
when :multiply | |
total *= numbers.shift | |
when :divide | |
total /= numbers.shift | |
when :power | |
total = total ** numbers.shift | |
when :square | |
total = total ** 2 | |
when :cube | |
total = total ** 3 | |
end | |
end | |
total | |
end | |
end | |
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
require 'rspec' | |
require 'calculator' | |
describe 'calculator' do | |
describe 'ask' do | |
it 'calculates addition equations in English' do | |
calculator = Calculator.new | |
calculator.ask('What is 1 plus 1?').should eq 2 | |
end | |
it 'calculates subtraction equations in English' do | |
calculator = Calculator.new | |
calculator.ask('What is the value of 4 minus 5?').should eq -1 | |
end | |
it 'calculates multiplication equations in English' do | |
calculator = Calculator.new | |
calculator.ask('What is the value of 4 times 6?').should eq 24 | |
end | |
it 'calculates division equations in English' do | |
calculator = Calculator.new | |
calculator.ask('What is 5 divided by 5?').should eq 1 | |
end | |
it 'calculates expressions with multiple operators (e.g. 5 plus 6 plus 7)' do | |
calculator = Calculator.new | |
calculator.ask('What is 5 plus 6 plus 7').should eq 18 | |
end | |
it 'calculates with negative numbers' do | |
calculator = Calculator.new | |
calculator.ask('Tell me 5 minus -4!').should eq 9 | |
end | |
it 'calculates powers' do | |
calculator = Calculator.new | |
calculator.ask('What is 5 plus 6 to the power of 2?').should eq 121 | |
end | |
it 'calculates squares' do | |
calculator = Calculator.new | |
calculator.ask('What is 5 squared?').should eq 25 | |
end | |
it 'calculates cubes' do | |
calculator = Calculator.new | |
calculator.ask('What is 2 cubed?').should eq 8 | |
end | |
end | |
describe 'phrase_cleaner' do | |
it 'strips off ".", "!", "?", from the expression' do | |
calculator = Calculator.new | |
calculator.phrase_cleaner('7 times 8?!.').should eq '7 times 8' | |
end | |
it 'strips off superfulous words' do | |
calculator = Calculator.new | |
calculator.phrase_cleaner('What is 7 times 8').should eq '7 times 8' | |
end | |
end | |
describe 'number getter' do | |
it 'returns an array of all of the numbers in a string' do | |
calculator = Calculator.new | |
calculator.number_getter('1 to the power of 35 divided by -6').should eq [1, 35, -6] | |
end | |
end | |
describe 'operator getter' do | |
it 'returns an array of all of the operators in a string' do | |
calculator = Calculator.new | |
calculator.operator_getter('1 to the power of 35 divided by -6').should eq ['to', 'divided'] | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment