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

Published at Oct 21 2018
·
0 comments

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 Ruby resources 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.1.0 df75482
class WordyTest < Minitest::Test
def test_addition
# skip
problem = WordProblem.new("What is 1 plus 1?")
assert_equal(2, problem.answer)
end
def test_more_addition
skip
problem = WordProblem.new("What is 53 plus 2?")
assert_equal(55, problem.answer)
end
def test_addition_with_negative_numbers
skip
problem = WordProblem.new("What is -1 plus -10?")
assert_equal(-11, problem.answer)
end
def test_large_addition
skip
problem = WordProblem.new("What is 123 plus 45678?")
assert_equal(45801, problem.answer)
end
def test_subtraction
skip
problem = WordProblem.new("What is 4 minus -12?")
assert_equal(16, problem.answer)
end
def test_multiplication
skip
problem = WordProblem.new("What is -3 multiplied by 25?")
assert_equal(-75, problem.answer)
end
def test_division
skip
problem = WordProblem.new("What is 33 divided by -3?")
assert_equal(-11, problem.answer)
end
def test_multiple_additions
skip
problem = WordProblem.new("What is 1 plus 1 plus 1?")
assert_equal(3, problem.answer)
end
def test_addition_and_subtraction
skip
problem = WordProblem.new("What is 1 plus 5 minus -2?")
assert_equal(8, problem.answer)
end
def test_multiple_subtraction
skip
problem = WordProblem.new("What is 20 minus 4 minus 13?")
assert_equal(3, problem.answer)
end
def test_subtraction_then_addition
skip
problem = WordProblem.new("What is 17 minus 6 plus 3?")
assert_equal(14, problem.answer)
end
def test_multiple_multiplication
skip
problem = WordProblem.new("What is 2 multiplied by -2 multiplied by 3?")
assert_equal(-12, problem.answer)
end
def test_addition_and_multiplication
skip
problem = WordProblem.new("What is -3 plus 7 multiplied by -2?")
message = "You should ignore order of precedence. -3 + 7 * -2 = -8, not #{problem.answer}"
assert_equal(-8, problem.answer, message)
end
def test_multiple_division
skip
problem = WordProblem.new("What is -12 divided by 2 divided by -3?")
assert_equal(2, problem.answer)
end
def test_unknown_operation
skip
problem = WordProblem.new("What is 52 cubed?")
assert_raises(ArgumentError) do
problem.answer
end
end
def test_non_math_question
skip
problem = WordProblem.new("Who is the President of the United States?")
assert_raises(ArgumentError) do
problem.answer
end
end
end
```

```
require 'byebug'
class WordProblem
def initialize(string)
@string = string
end
def answer
result = []
arrays = @string.gsub!('?', '').split(' ')
switch_items = switch_element(arrays)
raise ArgumentError if switch_items.length < 3
index = 3
final = []
length = switch_items.length
while index < length
first = three_element(switch_items[0..2])
result << first
remain_array = remain_array(switch_items, switch_items[0..2])
switch_items = []
switch_items << [result, remain_array ].flatten
length = switch_items.flatten.length
index += 1
end
return three_element(switch_items.flatten)
end
def remain_array(exist_array, _minus_array)
index = exist_array.index(_minus_array.first)
length = _minus_array.length
exist_array.slice!(index, length)
exist_array
end
def switch_element(array)
result = []
array.each do |element|
if element.scan(/^\-?[0-9]+$/).size == 1
result << element
elsif element == 'plus'
result << "+"
elsif element == 'minus'
result << "-"
elsif element == 'multiplied'
result << "*"
elsif element == 'divided'
result << "/"
end
end
result
end
def three_element(array)
handle_simple_expression(array[0], array[1], array[2])
end
def handle_simple_expression(operand_1, operator, operand_2)
if operator == 0 && operand_2 == 0
return operand_1
else
case operator
when '*'
operand_1.to_i * operand_2.to_i
when '+'
operand_1.to_i + operand_2.to_i
when '/'
operand_1.to_i / operand_2.to_i
when '-'
operand_1.to_i - operand_2.to_i
end
end
end
end
puts WordProblem.new("What is 33 divided by -3?").answer
```

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,423 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.