Ad
Parsing
Algorithms
Logic
Strings
Data Types
Code
Diff
  • 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("-", "+-");
    • }
Parsing
Algorithms
Logic
Strings
Data Types
Code
Diff
  • 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
Code
Diff
  • 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);
    • }
    • }