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

Instructions

Test suite

Solution

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

Problems with no operations simply evaluate to the number given.

What is 5?

Evaluates to 5.

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)

The parser should reject:

- Unsupported operations ("What is 52 cubed?")
- Non-math questions ("Who is the President of the United States")
- Word problems with invalid syntax ("What is 1 plus plus 2?")

If you'd like, handle exponentials.

What is 2 raised to the 5th power?

32

- To parse the text, you could try to use the Sprache library. You can also find a good tutorial here.

To run the tests, run the command `dotnet test`

from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip`

property from the next test and work on getting that test passing.
Once none of the tests are skipped and they are all passing, you can submit your solution
using `exercism submit Wordy.cs`

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

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

```
// This file was auto-generated based on version 1.5.0 of the canonical data.
using System;
using Xunit;
public class WordyTest
{
[Fact]
public void Just_a_number()
{
Assert.Equal(5, Wordy.Answer("What is 5?"));
}
[Fact(Skip = "Remove to run test")]
public void Addition()
{
Assert.Equal(2, Wordy.Answer("What is 1 plus 1?"));
}
[Fact(Skip = "Remove to run test")]
public void More_addition()
{
Assert.Equal(55, Wordy.Answer("What is 53 plus 2?"));
}
[Fact(Skip = "Remove to run test")]
public void Addition_with_negative_numbers()
{
Assert.Equal(-11, Wordy.Answer("What is -1 plus -10?"));
}
[Fact(Skip = "Remove to run test")]
public void Large_addition()
{
Assert.Equal(45801, Wordy.Answer("What is 123 plus 45678?"));
}
[Fact(Skip = "Remove to run test")]
public void Subtraction()
{
Assert.Equal(16, Wordy.Answer("What is 4 minus -12?"));
}
[Fact(Skip = "Remove to run test")]
public void Multiplication()
{
Assert.Equal(-75, Wordy.Answer("What is -3 multiplied by 25?"));
}
[Fact(Skip = "Remove to run test")]
public void Division()
{
Assert.Equal(-11, Wordy.Answer("What is 33 divided by -3?"));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_additions()
{
Assert.Equal(3, Wordy.Answer("What is 1 plus 1 plus 1?"));
}
[Fact(Skip = "Remove to run test")]
public void Addition_and_subtraction()
{
Assert.Equal(8, Wordy.Answer("What is 1 plus 5 minus -2?"));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_subtraction()
{
Assert.Equal(3, Wordy.Answer("What is 20 minus 4 minus 13?"));
}
[Fact(Skip = "Remove to run test")]
public void Subtraction_then_addition()
{
Assert.Equal(14, Wordy.Answer("What is 17 minus 6 plus 3?"));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_multiplication()
{
Assert.Equal(-12, Wordy.Answer("What is 2 multiplied by -2 multiplied by 3?"));
}
[Fact(Skip = "Remove to run test")]
public void Addition_and_multiplication()
{
Assert.Equal(-8, Wordy.Answer("What is -3 plus 7 multiplied by -2?"));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_division()
{
Assert.Equal(2, Wordy.Answer("What is -12 divided by 2 divided by -3?"));
}
[Fact(Skip = "Remove to run test")]
public void Unknown_operation()
{
Assert.Throws<ArgumentException>(() => Wordy.Answer("What is 52 cubed?"));
}
[Fact(Skip = "Remove to run test")]
public void Non_math_question()
{
Assert.Throws<ArgumentException>(() => Wordy.Answer("Who is the President of the United States?"));
}
[Fact(Skip = "Remove to run test")]
public void Reject_problem_missing_an_operand()
{
Assert.Throws<ArgumentException>(() => Wordy.Answer("What is 1 plus?"));
}
[Fact(Skip = "Remove to run test")]
public void Reject_problem_with_no_operands_or_operators()
{
Assert.Throws<ArgumentException>(() => Wordy.Answer("What is?"));
}
[Fact(Skip = "Remove to run test")]
public void Reject_two_operations_in_a_row()
{
Assert.Throws<ArgumentException>(() => Wordy.Answer("What is 1 plus plus 2?"));
}
[Fact(Skip = "Remove to run test")]
public void Reject_two_numbers_in_a_row()
{
Assert.Throws<ArgumentException>(() => Wordy.Answer("What is 1 plus 2 1?"));
}
[Fact(Skip = "Remove to run test")]
public void Reject_postfix_notation()
{
Assert.Throws<ArgumentException>(() => Wordy.Answer("What is 1 2 plus?"));
}
[Fact(Skip = "Remove to run test")]
public void Reject_prefix_notation()
{
Assert.Throws<ArgumentException>(() => Wordy.Answer("What is plus 1 2?"));
}
}
```

```
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public static class Wordy
{
public static int Answer(string question)
{
var sb = new StringBuilder();
var dt = new DataTable();
var lastValueIsNumber = false;
var bracketCounter = 0;
Dictionary<string, string> tokens = new Dictionary<string, string>()
{
{"plus", "+"},
{"minus", "-"},
{"multiplied", "*"},
{"divided", "/"},
{"cubed", ""}
};
foreach(string str in question.Replace('?', ' ').Trim().Split(' '))
{
if(Regex.IsMatch(str, "[0-9]"))
{
if(lastValueIsNumber == false)
{
sb.Append(str);
bracketCounter++;
}
else
throw new ArgumentException();
lastValueIsNumber = true;
}
else if(tokens.ContainsKey(str))
{
if(lastValueIsNumber == true && tokens[str] != "cubed")
{
sb.Append(tokens[str]);
bracketCounter++;
}
else
throw new ArgumentException();
lastValueIsNumber = false;
}
if(bracketCounter != 0 && bracketCounter % 3 == 0)
{
sb.Insert(0, "(");
sb.Append(")");
}
}
Console.WriteLine(sb.ToString());
if(String.IsNullOrWhiteSpace(sb.ToString()) || !lastValueIsNumber) throw new ArgumentException();
return Convert.ToInt32(dt.Compute(sb.ToString(), ""));
}
}
```

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

## Community comments