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

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

There is a test suite and module included with the exercise.
The test suite (a file with the extension `.t`

) will attempt to run routines
from the module (a file with the extension `.pm6`

).
Add/modify routines in the module so that the tests will pass! You can view the
test data by executing the command `perl6 --doc *.t`

(* being the name of the
test suite), and run the test suite for the exercise by executing the command
`prove6 .`

in the exercise directory.

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.

```
#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib $?FILE.IO.dirname;
use Wordy;
plan 16;
my $c-data = from-json $=pod.pop.contents;
for @($c-data<cases>) {
if .<expected> === False {
throws-like {.<input><question>.&answer}, Exception, .<description>;
} else {
is .<input><question>.&answer, |.<expected description>;
}
}
=head2 Canonical Data
=begin code
{
"exercise": "wordy",
"version": "1.1.0",
"comments": [
"The tests that expect 'false' should be implemented to raise",
"an error, or indicate a failure. Implement this in a way that",
"makes sense for your language."
],
"cases": [
{
"description": "addition",
"property": "answer",
"input": {
"question": "What is 1 plus 1?"
},
"expected": 2
},
{
"description": "more addition",
"property": "answer",
"input": {
"question": "What is 53 plus 2?"
},
"expected": 55
},
{
"description": "addition with negative numbers",
"property": "answer",
"input": {
"question": "What is -1 plus -10?"
},
"expected": -11
},
{
"description": "large addition",
"property": "answer",
"input": {
"question": "What is 123 plus 45678?"
},
"expected": 45801
},
{
"description": "subtraction",
"property": "answer",
"input": {
"question": "What is 4 minus -12?"
},
"expected": 16
},
{
"description": "multiplication",
"property": "answer",
"input": {
"question": "What is -3 multiplied by 25?"
},
"expected": -75
},
{
"description": "division",
"property": "answer",
"input": {
"question": "What is 33 divided by -3?"
},
"expected": -11
},
{
"description": "multiple additions",
"property": "answer",
"input": {
"question": "What is 1 plus 1 plus 1?"
},
"expected": 3
},
{
"description": "addition and subtraction",
"property": "answer",
"input": {
"question": "What is 1 plus 5 minus -2?"
},
"expected": 8
},
{
"description": "multiple subtraction",
"property": "answer",
"input": {
"question": "What is 20 minus 4 minus 13?"
},
"expected": 3
},
{
"description": "subtraction then addition",
"property": "answer",
"input": {
"question": "What is 17 minus 6 plus 3?"
},
"expected": 14
},
{
"description": "multiple multiplication",
"property": "answer",
"input": {
"question": "What is 2 multiplied by -2 multiplied by 3?"
},
"expected": -12
},
{
"description": "addition and multiplication",
"property": "answer",
"input": {
"question": "What is -3 plus 7 multiplied by -2?"
},
"expected": -8
},
{
"description": "multiple division",
"property": "answer",
"input": {
"question": "What is -12 divided by 2 divided by -3?"
},
"expected": 2
},
{
"description": "unknown operation",
"property": "answer",
"input": {
"question": "What is 52 cubed?"
},
"expected": false
},
{
"description": "Non math question",
"property": "answer",
"input": {
"question": "Who is the President of the United States?"
},
"expected": false
}
]
}
=end code
```

```
sub answer(Str $input) is export {
my @words = $input.lc.subst(/'what is ' (.*?) '?'/, {$0}).subst(/by/, '', :g).words;
my &ret = { @words.splice(0, 3, $^result) };
while @words.elems > 1 {
given @words[1] {
when 'plus' { ret(@words[0] + @words[2]) }
when 'minus' { ret(@words[0] - @words[2]) }
when 'multiplied' { ret(@words[0] * @words[2]) }
when 'divided' { ret(@words[0] / @words[2]) }
default { die "Invalid operation: @words[1]" }
}
}
return @words[0];
}
```

A huge amount can be learnt 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 I could read more about to develop my understanding?