Parsing
Algorithms
Logic
Strings
Data Types
using System; using System.Linq; using System.Text.RegularExpressions; public class ArtihmeticParser { private static readonly Regex WhitespaceRegex = new Regex(@"\s+", RegexOptions.Compiled); private static readonly Regex BracketsRegex = new Regex(@"\(([^\)]*)\)", RegexOptions.Compiled); public static int Evaluate(string input) { input = EvaluateBrackets(input); input = RemoveWhitespaces(input); input = SimplifySubtraction(input); return input.Split('+').Select(EvaluateProduct).Sum(); } private static int EvaluateProduct(string input) => input.Split('*').Select(int.Parse).Aggregate((a, b) => a * b); private static string EvaluateBrackets(string input) => BracketsRegex.Replace(input, x => Evaluate(x.Groups[1].ToString()).ToString()); private static string RemoveWhitespaces(string input) => WhitespaceRegex.Replace(input, ""); private static string SimplifySubtraction(string input) => input.Replace("-", "+-"); }
- using System;
- using System.Linq;
- using System.Text.RegularExpressions;
- public class ArtihmeticParser
- {
- private static readonly Regex WhitespaceRegex = new Regex(@"\s+", RegexOptions.Compiled);
- private static readonly Regex BracketsRegex = new Regex(@"\(([^\)]*)\)", RegexOptions.Compiled);
- public static int Evaluate(string input)
- {
return WhitespaceRegex.Replace(input, "").Replace("-", "+-").Split('+').Select(EvaluateProduct).Sum();}private static int EvaluateProduct(string input){return input.Split('*').Select(int.Parse).Aggregate((a,b) => a * b);- input = EvaluateBrackets(input);
- input = RemoveWhitespaces(input);
- input = SimplifySubtraction(input);
- return input.Split('+').Select(EvaluateProduct).Sum();
- }
- private static int EvaluateProduct(string input) =>
- input.Split('*').Select(int.Parse).Aggregate((a, b) => a * b);
- private static string EvaluateBrackets(string input) =>
- BracketsRegex.Replace(input, x => Evaluate(x.Groups[1].ToString()).ToString());
- private static string RemoveWhitespaces(string input) =>
- WhitespaceRegex.Replace(input, "");
- private static string SimplifySubtraction(string input) =>
- input.Replace("-", "+-");
- }
using NUnit.Framework; using System; [TestFixture] public class SolutionTest { [Test] public void TestOneNumber3() { Assert.AreEqual(ArtihmeticParser.Evaluate("3"), 3); } [Test] public void TestOneNumber10() { Assert.AreEqual(ArtihmeticParser.Evaluate("10"), 10); } [Test] public void TestOneNumber729() { Assert.AreEqual(ArtihmeticParser.Evaluate("729"), 729); } [Test] public void TestOneNumber0() { Assert.AreEqual(ArtihmeticParser.Evaluate("0"), 0); } [Test] public void TestAdditionOfTwoNumbers2() { Assert.AreEqual(ArtihmeticParser.Evaluate("1+1"), 2); } [Test] public void TestAdditionOfTwoNumbers22() { Assert.AreEqual(ArtihmeticParser.Evaluate("10+12"), 22); } [Test] public void TestAdditionOfThreeNumbers6() { Assert.AreEqual(ArtihmeticParser.Evaluate("1+2+3"), 6); } [Test] public void TestAdditionOfFiveNumbers() { Assert.AreEqual(ArtihmeticParser.Evaluate("1+2+3+4+5"), 15); } [Test] public void TestProductOfTwoNumbers1() { Assert.AreEqual(ArtihmeticParser.Evaluate("1*1"), 1); } [Test] public void TestProductOfTwoNumbers120() { Assert.AreEqual(ArtihmeticParser.Evaluate("10*12"), 120); } [Test] public void TestProductOfThreeNumbers6() { Assert.AreEqual(ArtihmeticParser.Evaluate("1*2*3"), 6); } [Test] public void TestProductOfFiveNumbers() { Assert.AreEqual(ArtihmeticParser.Evaluate("1*2*3*4*5"), 120); } [Test] public void TestSumAndProduct() { Assert.AreEqual(ArtihmeticParser.Evaluate("2+4*1"), 6); } [Test] public void TestOrderOfOperations() { Assert.AreEqual(ArtihmeticParser.Evaluate("2+4*6"), 26); } [Test] public void TestLongerInput() { Assert.AreEqual(ArtihmeticParser.Evaluate("1000*3*2+100*2*2+2*2*2*2*5+9"), 6489); } [Test] public void TestBrackets() { Assert.AreEqual(ArtihmeticParser.Evaluate("(2+4)*6"), 36); } [Test] public void TestWhitespace() { Assert.AreEqual(ArtihmeticParser.Evaluate(" 2 + 4 *6 "), 26); } [Test] public void TestSubtraction() { Assert.AreEqual(ArtihmeticParser.Evaluate("1+3-2+4"), 6); } [Test] public void TestOrderOfOperationsWithSubtraction() { Assert.AreEqual(ArtihmeticParser.Evaluate("10+15-2*2+10-3*4"), 19); } [Test] public void TestBracketsAndSubtraction() { Assert.AreEqual(ArtihmeticParser.Evaluate("100-2*(2+10-3)*4"), 28); } }
- using NUnit.Framework;
- using System;
- [TestFixture]
- public class SolutionTest
- {
- [Test]
- public void TestOneNumber3()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("3"), 3);
- }
- [Test]
- public void TestOneNumber10()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("10"), 10);
- }
- [Test]
- public void TestOneNumber729()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("729"), 729);
- }
- [Test]
- public void TestOneNumber0()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("0"), 0);
- }
- [Test]
- public void TestAdditionOfTwoNumbers2()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1+1"), 2);
- }
- [Test]
- public void TestAdditionOfTwoNumbers22()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("10+12"), 22);
- }
- [Test]
- public void TestAdditionOfThreeNumbers6()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1+2+3"), 6);
- }
- [Test]
- public void TestAdditionOfFiveNumbers()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1+2+3+4+5"), 15);
- }
- [Test]
- public void TestProductOfTwoNumbers1()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1*1"), 1);
- }
- [Test]
- public void TestProductOfTwoNumbers120()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("10*12"), 120);
- }
- [Test]
- public void TestProductOfThreeNumbers6()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1*2*3"), 6);
- }
- [Test]
- public void TestProductOfFiveNumbers()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1*2*3*4*5"), 120);
- }
- [Test]
- public void TestSumAndProduct()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("2+4*1"), 6);
- }
- [Test]
- public void TestOrderOfOperations()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("2+4*6"), 26);
- }
- [Test]
- public void TestLongerInput()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1000*3*2+100*2*2+2*2*2*2*5+9"), 6489);
- }
- [Test]
[Ignore("Brackets")]- public void TestBrackets()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("(2+4)*6"), 36);
- }
- [Test]
- public void TestWhitespace()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate(" 2 + 4 *6 "), 26);
- }
- [Test]
- public void TestSubtraction()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1+3-2+4"), 6);
- }
- [Test]
- public void TestOrderOfOperationsWithSubtraction()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("10+15-2*2+10-3*4"), 19);
- }
- [Test]
[Ignore("Subtraction and brackets")]- public void TestBracketsAndSubtraction()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("100-2*(2+10-3)*4"), 28);
- }
- }
Parsing
Algorithms
Logic
Strings
Data Types
using System; using System.Linq; using System.Text.RegularExpressions; public class ArtihmeticParser { private static readonly Regex WhitespaceRegex = new Regex(@"\s+", RegexOptions.Compiled); public static int Evaluate(string input) { return WhitespaceRegex.Replace(input, "").Replace("-", "+-").Split('+').Select(EvaluateProduct).Sum(); } private static int EvaluateProduct(string input) { return input.Split('*').Select(int.Parse).Aggregate((a,b) => a * b); } }
- using System;
- using System.Linq;
- using System.Text.RegularExpressions;
- public class ArtihmeticParser
- {
- private static readonly Regex WhitespaceRegex = new Regex(@"\s+", RegexOptions.Compiled);
- public static int Evaluate(string input)
- {
- return WhitespaceRegex.Replace(input, "").Replace("-", "+-").Split('+').Select(EvaluateProduct).Sum();
- }
- private static int EvaluateProduct(string input)
- {
return input.Split('*').Select(int.Parse).Aggregate(1,(a,b) => a*b);- return input.Split('*').Select(int.Parse).Aggregate((a,b) => a * b);
- }
- }
Parsing
Algorithms
Logic
Strings
Data Types
using System; using System.Linq; using System.Text.RegularExpressions; public class ArtihmeticParser { private static readonly Regex WhitespaceRegex = new Regex(@"\s+", RegexOptions.Compiled); public static int Evaluate(string input) { return WhitespaceRegex.Replace(input, "").Replace("-", "+-").Split('+').Select(EvaluateProduct).Sum(); } private static int EvaluateProduct(string input) { return input.Split('*').Select(int.Parse).Aggregate(1,(a,b) => a*b); } }
- using System;
- using System.Linq;
- using System.Text.RegularExpressions;
- public class ArtihmeticParser
- {
- private static readonly Regex WhitespaceRegex = new Regex(@"\s+", RegexOptions.Compiled);
- public static int Evaluate(string input)
- {
return input.Split("+").Select(EvaluateProduct).Aggregate(0,(a,b) => a+b);- return WhitespaceRegex.Replace(input, "").Replace("-", "+-").Split('+').Select(EvaluateProduct).Sum();
- }
- private static int EvaluateProduct(string input)
- {
return input.Split("*").Select(EvaluateInteger).Aggregate(1,(a,b) => a*b);}private static int EvaluateInteger(string input){return int.Parse(input);- return input.Split('*').Select(int.Parse).Aggregate(1,(a,b) => a*b);
- }
- }
using NUnit.Framework; using System; [TestFixture] public class SolutionTest { [Test] public void TestOneNumber3() { Assert.AreEqual(ArtihmeticParser.Evaluate("3"), 3); } [Test] public void TestOneNumber10() { Assert.AreEqual(ArtihmeticParser.Evaluate("10"), 10); } [Test] public void TestOneNumber729() { Assert.AreEqual(ArtihmeticParser.Evaluate("729"), 729); } [Test] public void TestOneNumber0() { Assert.AreEqual(ArtihmeticParser.Evaluate("0"), 0); } [Test] public void TestAdditionOfTwoNumbers2() { Assert.AreEqual(ArtihmeticParser.Evaluate("1+1"), 2); } [Test] public void TestAdditionOfTwoNumbers22() { Assert.AreEqual(ArtihmeticParser.Evaluate("10+12"), 22); } [Test] public void TestAdditionOfThreeNumbers6() { Assert.AreEqual(ArtihmeticParser.Evaluate("1+2+3"), 6); } [Test] public void TestAdditionOfFiveNumbers() { Assert.AreEqual(ArtihmeticParser.Evaluate("1+2+3+4+5"), 15); } [Test] public void TestProductOfTwoNumbers1() { Assert.AreEqual(ArtihmeticParser.Evaluate("1*1"), 1); } [Test] public void TestProductOfTwoNumbers120() { Assert.AreEqual(ArtihmeticParser.Evaluate("10*12"), 120); } [Test] public void TestProductOfThreeNumbers6() { Assert.AreEqual(ArtihmeticParser.Evaluate("1*2*3"), 6); } [Test] public void TestProductOfFiveNumbers() { Assert.AreEqual(ArtihmeticParser.Evaluate("1*2*3*4*5"), 120); } [Test] public void TestSumAndProduct() { Assert.AreEqual(ArtihmeticParser.Evaluate("2+4*1"), 6); } [Test] public void TestOrderOfOperations() { Assert.AreEqual(ArtihmeticParser.Evaluate("2+4*6"), 26); } [Test] public void TestLongerInput() { Assert.AreEqual(ArtihmeticParser.Evaluate("1000*3*2+100*2*2+2*2*2*2*5+9"), 6489); } [Test] [Ignore("Brackets")] public void TestBrackets() { Assert.AreEqual(ArtihmeticParser.Evaluate("(2+4)*6"), 36); } [Test] public void TestWhitespace() { Assert.AreEqual(ArtihmeticParser.Evaluate(" 2 + 4 *6 "), 26); } [Test] public void TestSubtraction() { Assert.AreEqual(ArtihmeticParser.Evaluate("1+3-2+4"), 6); } [Test] public void TestOrderOfOperationsWithSubtraction() { Assert.AreEqual(ArtihmeticParser.Evaluate("10+15-2*2+10-3*4"), 19); } [Test] [Ignore("Subtraction and brackets")] public void TestBracketsAndSubtraction() { Assert.AreEqual(ArtihmeticParser.Evaluate("100-2*(2+10-3)*4"), 28); } }
- using NUnit.Framework;
- using System;
- [TestFixture]
- public class SolutionTest
- {
- [Test]
- public void TestOneNumber3()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("3"), 3);
- }
- [Test]
- public void TestOneNumber10()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("10"), 10);
- }
- [Test]
- public void TestOneNumber729()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("729"), 729);
- }
- [Test]
- public void TestOneNumber0()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("0"), 0);
- }
- [Test]
- public void TestAdditionOfTwoNumbers2()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1+1"), 2);
- }
- [Test]
- public void TestAdditionOfTwoNumbers22()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("10+12"), 22);
- }
- [Test]
- public void TestAdditionOfThreeNumbers6()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1+2+3"), 6);
- }
- [Test]
- public void TestAdditionOfFiveNumbers()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1+2+3+4+5"), 15);
- }
- [Test]
- public void TestProductOfTwoNumbers1()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1*1"), 1);
- }
- [Test]
- public void TestProductOfTwoNumbers120()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("10*12"), 120);
- }
- [Test]
- public void TestProductOfThreeNumbers6()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1*2*3"), 6);
- }
- [Test]
- public void TestProductOfFiveNumbers()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1*2*3*4*5"), 120);
- }
- [Test]
- public void TestSumAndProduct()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("2+4*1"), 6);
- }
- [Test]
- public void TestOrderOfOperations()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("2+4*6"), 26);
- }
- [Test]
- public void TestLongerInput()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1000*3*2+100*2*2+2*2*2*2*5+9"), 6489);
- }
- [Test]
- [Ignore("Brackets")]
- public void TestBrackets()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("(2+4)*6"), 36);
- }
- [Test]
[Ignore("Whitespace")]- public void TestWhitespace()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate(" 2 + 4 *6 "), 26);
- }
- [Test]
[Ignore("Subtraction")]- public void TestSubtraction()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("1+3-2+4"), 6);
- }
- [Test]
[Ignore("Subtraction")]- public void TestOrderOfOperationsWithSubtraction()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("10+15-2*2+10-3*4"), 19);
- }
- [Test]
- [Ignore("Subtraction and brackets")]
- public void TestBracketsAndSubtraction()
- {
- Assert.AreEqual(ArtihmeticParser.Evaluate("100-2*(2+10-3)*4"), 28);
- }
- }