How can I use linq when calculating the response from a character and two numbers

I recently had an interview in which I was asked to write a method for calculating a response from a string with prefix formatting.

The response to the interview was that I had to use Linq

I am a little confused where I had to use linq and wondered if anyone could help me.

Problem

The signature method should be:

int Calculate(string expression)

I did not need to check the input, but some examples were given

  • "+ 3 4" should give 7
  • "- 2 10" must indicate -8
  • "* 10 -20" should indicate -200
  • "/ 6 2" should give 3

My solution is from memory

So, I created unit tests and developed the code at TDD Manor

[TestFixture]
public class UnitTest1
{
    [Test]
    public void TestMethod_Add()
    {
        var calculator = new Calculator();
        var result = calculator.Calculate("+ 4 2");
        var expected = 6;

        Assert.AreEqual(expected, result);
    }

    [Test]
    public void TestMethod_Sub()
    {
        var calculator = new Calculator();
        var result = calculator.Calculate("- 2 10");
        var expected = -8;

        Assert.AreEqual(expected, result);
    }

    [Test]
    public void TestMethod1_mult()
    {

        var calculator = new Calculator();
        var result = calculator.Calculate("* 10 -20");
        var expected = -200;

        Assert.AreEqual(expected, result);
    }

    [Test]
    public void TestMethod1_div()
    {
        var calculator = new Calculator();
        var result = calculator.Calculate("/ 6 2");
        var expected = 3;

        Assert.AreEqual(expected, result);
    }

and created the following code something like this:

public class Calculator
{
    public int Calculate(string expression)
    {
        var tokens = expression.Split(' ');

        var symbol = tokens[0];
        var num1 = int.Parse(tokens[1]);
        var num2 = int.Parse(tokens[2]);

        switch (symbol)
        {
            case "+":
                return num1 + num2;

            case "-":
                return num1 - num2;

            case "*":
                return num1 * num2;

            case "/":
                return num1 / num2;
            default:
                throw new NotImplementedException("Symbol has not been implemented");
        }

    }
}
+4
2

LINQ, :

int Calculate(string expression) {
    var operations = new Dictionary<char, Func<int, int, int>> {
        { '+', (a, b) => a + b },
        { '-', (a, b) => a - b },
        { '*', (a, b) => a * b },
        { '/', (a, b) => a / b },
    };
    return expression
        .Substring(2)
        .Split(' ')
        .Select(o => int.Parse(o))
        .Aggregate(operations[expression[0]])
    ;
}

, "* 1 2 3 4 5" (5!, , , 120) "/ 1729 7 13 19" ( , ).

. Calculate, . , - , . ( , - , , - .)

( : - , SelectMany -.)

+8

, - ( ;-))

int Calculate(string expression)
    {
        char symbol = expression.First();
        int[] numbers = Array.ConvertAll(expression.Substring(2).Split(' '), p => int.Parse(p)); 

        switch (symbol)
        {
            case '+':
                return numbers.Aggregate((a, b) => a + b);
            case '-':
                return numbers.Aggregate((a, b) => a - b);
            case '*':
                return numbers.Aggregate((a, b) => a * b);
            case '/':
                return numbers.Aggregate((a, b) => a / b);
            default:
                return 0;
        }
    }
+5

All Articles