# 4d47's solution

## to Wordy in the Perl 6 Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

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

What is 5 plus 13?

Evaluates to 18.

Handle large numbers and negative numbers.

## Iteration 2 — Subtraction, Multiplication and Division

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

## Iteration 3 — Multiple Operations

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)

## Bonus — Exponentials

If you'd like, handle exponentials.

What is 2 raised to the 5th power?

32

## Resources

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

## Running the tests

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.

## Source

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

## Submitting Incomplete Solutions

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

### wordy.t

``````#!/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 {
} else {
}
}

=begin code
{
"exercise": "wordy",
"version": "1.1.0",
"The tests that expect 'false' should be implemented to raise",
"an error, or indicate a failure. Implement this in a way that",
],
"cases": [
{
"input": {
"question": "What is 1 plus 1?"
},
"expected": 2
},
{
"input": {
"question": "What is 53 plus 2?"
},
"expected": 55
},
{
"input": {
"question": "What is -1 plus -10?"
},
"expected": -11
},
{
"input": {
"question": "What is 123 plus 45678?"
},
"expected": 45801
},
{
"description": "subtraction",
"input": {
"question": "What is 4 minus -12?"
},
"expected": 16
},
{
"description": "multiplication",
"input": {
"question": "What is -3 multiplied by 25?"
},
"expected": -75
},
{
"description": "division",
"input": {
"question": "What is 33 divided by -3?"
},
"expected": -11
},
{
"input": {
"question": "What is 1 plus 1 plus 1?"
},
"expected": 3
},
{
"input": {
"question": "What is 1 plus 5 minus -2?"
},
"expected": 8
},
{
"description": "multiple subtraction",
"input": {
"question": "What is 20 minus 4 minus 13?"
},
"expected": 3
},
{
"input": {
"question": "What is 17 minus 6 plus 3?"
},
"expected": 14
},
{
"description": "multiple multiplication",
"input": {
"question": "What is 2 multiplied by -2 multiplied by 3?"
},
"expected": -12
},
{
"input": {
"question": "What is -3 plus 7 multiplied by -2?"
},
"expected": -8
},
{
"description": "multiple division",
"input": {
"question": "What is -12 divided by 2 divided by -3?"
},
"expected": 2
},
{
"description": "unknown operation",
"input": {
"question": "What is 52 cubed?"
},
"expected": false
},
{
"description": "Non math question",
"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];
}``````