π Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io π

Published at Jul 13 2018
·
1 comment

Instructions

Test suite

Solution

Parse and evaluate simple math word problems returning the answer as an integer.

Add two numbers together.

What is 5 plus 13?

Evaluates to 18.

Handle large numbers and negative numbers.

Now, perform the other three operations.

What is 7 minus 5?

2

What is 6 multiplied by 4?

24

What is 25 divided by 5?

5

Handle a set of operations, in sequence.

Since these are verbal word problems, evaluate the expression from
left-to-right, *ignoring the typical order of operations.*

What is 5 plus 13 plus 6?

24

What is 3 plus 2 multiplied by 3?

15 (i.e. not 9)

If you'd like, handle exponentials.

What is 2 raised to the 5th power?

32

For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

```
gem install minitest
```

If you would like color output, you can `require 'minitest/pride'`

in
the test file, or note the alternative instruction, below, for running
the test file.

Run the tests from the exercise directory using the following command:

```
ruby wordy_test.rb
```

To include color from the command line:

```
ruby -r minitest/pride wordy_test.rb
```

Inspired by one of the generated questions in the Extreme Startup game. https://github.com/rchatley/extreme_startup

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
require 'minitest/autorun'
require_relative 'wordy'
# Common test data version: 1.0.0 5b8ad58
class WordyTest < Minitest::Test
def test_addition
# skip
question = 'What is 1 plus 1?'
assert_equal(2, WordProblem.new(question).answer)
end
def test_more_addition
skip
question = 'What is 53 plus 2?'
assert_equal(55, WordProblem.new(question).answer)
end
def test_addition_with_negative_numbers
skip
question = 'What is -1 plus -10?'
assert_equal(-11, WordProblem.new(question).answer)
end
def test_large_addition
skip
question = 'What is 123 plus 45678?'
assert_equal(45801, WordProblem.new(question).answer)
end
def test_subtraction
skip
question = 'What is 4 minus -12?'
assert_equal(16, WordProblem.new(question).answer)
end
def test_multiplication
skip
question = 'What is -3 multiplied by 25?'
assert_equal(-75, WordProblem.new(question).answer)
end
def test_division
skip
question = 'What is 33 divided by -3?'
assert_equal(-11, WordProblem.new(question).answer)
end
def test_multiple_additions
skip
question = 'What is 1 plus 1 plus 1?'
assert_equal(3, WordProblem.new(question).answer)
end
def test_addition_and_subtraction
skip
question = 'What is 1 plus 5 minus -2?'
assert_equal(8, WordProblem.new(question).answer)
end
def test_multiple_subtraction
skip
question = 'What is 20 minus 4 minus 13?'
assert_equal(3, WordProblem.new(question).answer)
end
def test_subtraction_then_addition
skip
question = 'What is 17 minus 6 plus 3?'
assert_equal(14, WordProblem.new(question).answer)
end
def test_multiple_multiplication
skip
question = 'What is 2 multiplied by -2 multiplied by 3?'
assert_equal(-12, WordProblem.new(question).answer)
end
def test_addition_and_multiplication
skip
question = 'What is -3 plus 7 multiplied by -2?'
answer = WordProblem.new(question).answer
message = "You should ignore order of precedence. -3 + 7 * -2 = -8, not #{answer}"
assert_equal(-8, answer, message)
end
def test_multiple_division
skip
question = 'What is -12 divided by 2 divided by -3?'
assert_equal(2, WordProblem.new(question).answer)
end
def test_unknown_operation
skip
question = 'What is 52 cubed?'
assert_raises ArgumentError do
WordProblem.new(question).answer
end
end
def test_non_math_question
skip
question = 'Who is the President of the United States?'
assert_raises ArgumentError do
WordProblem.new(question).answer
end
end
# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
# not your solution.
#
# Define a constant named VERSION inside of the top level BookKeeping
# module, which may be placed near the end of your file.
#
# In your file, it will look like this:
#
# module BookKeeping
# VERSION = 1 # Where the version number matches the one in the test.
# end
#
# If you are curious, read more about constants on RubyDoc:
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
def test_bookkeeping
skip
assert_equal 1, BookKeeping::VERSION
end
end
```

```
class WordProblem
def initialize(problem)
ops = problem.scan(/plus|minus|multiplied|divided/)
raise ArgumentError if ops.empty?
@problem=problem.split(' ')
str=@problem.join(' ').delete "?"
@problem=str.split(' ')
for i in (0...(@problem.length))
case @problem[i]
when "plus"
@problem[i]=:+
when "minus"
@problem[i]=:-
when "divided" || "divided by "
@problem[i]=:/
when "multiplied" || "times" || "multiplied by "
@problem[i]=:*
end
end
end
def answer
result=@problem.join(' ').delete "What is by"
ar=[]
flag = 0
ar=result.scan(/\d+|[\+\-\/\*]/)
ar.each_with_index{|x,i|
if x.match(/\d/)
ar[i]=x.to_i
else
ar[i]=x.to_sym
end
}
ar.each_with_index{|x,i|
if ar[i].is_a?(Symbol) && ((!ar[i-1].is_a?(Integer))||(i==0))
ar[i]=nil
ar[i+1]=ar[i+1] * (-1)
flag=1
end
}
ar=ar.compact
eval=[]
i=1
while i < ar.length
if ar[i].is_a?(Symbol) && ar[i-1].is_a?(Integer) && ar[i+1].is_a?(Integer)
ar[i+1]=ar[i-1].send(ar[i],ar[i+1])
i += 2
end
end
if flag==1
return ar[i-1]
else
return eval result
end
end
end
```

A huge amount can be learned from reading other peopleβs code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,428 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments

The unconditional returns in the conditional branches seems to be, well, unconditional. What happens if you remove them? I ends up returning the evaluation of that if statement, as it is the last statement evaluated in that method. The early returns doesn't let the if statement finish.

The space around operators is inconsistent. This is more readable:

a = 1 + 1 than a=1+1

And you use spaces around the assignment operator on line 25, and around the < on line 44 and around the += on line 48, and around the || operators on lines 14 and 16 (twice). I would even say that having that space helps to clarify what lines 11, 13, 15 and 17 are trying to convey. Is that @problem[i]=: * or @problem[i]= :* or even more clear... @problem[i] = :*

The method answer is doing a lot of things... and probably doing all of those things well, (or at least well enough), but it is far from "doing one thing well" I think. The answer is likely meant to present that answer. It can call other methods to do so. If you can break up each of the pieces to the individual functionality, it may help to simplify the logic as well.