Advertisement
dimipan80

Sum of All Values

May 26th, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. /* You are given a keys string and a text string. Write a program that finds the start key and the end key from the keys string and then applies them to the text string.
  2.  * The start key will always stay at the beginning of the keys string. It can contain only letters and underscore and ends just before the first found digit. The end key will always stay at the end of the keys string. It can contain only letters and underscore and starts just after the last found digit.
  3.  * Print at the console the sum of all values (only floating-point numbers with dot as a separator are considered valid) in the text string, between a start key and an end key. If the value is 0 then print “The total value is: nothing”. If no start key or no end key is found then print “A key is missing”.
  4.  * The output should hold the result text, printed in an HTML paragraph. The actual value of the sum should be italic. */
  5.  
  6. namespace Sum_of_All_Values
  7. {
  8.     using System;
  9.     using System.Globalization;
  10.     using System.Linq;
  11.     using System.Text.RegularExpressions;
  12.  
  13.     class SumOfAllValues
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             string[] keys = GetStartAndEndKeysFromInput();
  18.             if (string.IsNullOrEmpty(keys[0]) || string.IsNullOrEmpty(keys[1]))
  19.             {
  20.                 Console.WriteLine("<p>A key is missing</p>");
  21.                 return;
  22.             }
  23.  
  24.             string keyValuesPattern = keys[0] + @"(\d+|\d+\.\d+|\.\d+)" + keys[1];
  25.             string textString = Console.ReadLine();
  26.             MatchCollection matches = Regex.Matches(textString, keyValuesPattern);
  27.             double totalSum = 0;
  28.             bool isFoundValues = false;
  29.             foreach (Match match in matches)
  30.             {
  31.                 isFoundValues = true;
  32.                 totalSum += double.Parse(match.Groups[1].Value);
  33.             }
  34.  
  35.             Console.WriteLine("<p>The total value is: <em>{0}</em></p>",
  36.                 (isFoundValues) ? totalSum.ToString(CultureInfo.InvariantCulture) : "nothing");
  37.         }
  38.  
  39.         private static string[] GetStartAndEndKeysFromInput()
  40.         {
  41.             const string startKeyPattern = @"^[A-Za-z_]+$";
  42.             const string endKeyPattern = @"(?:^|\d)([A-Za-z_]+)$";
  43.             const string allDigitsStr = "0123456789";
  44.  
  45.             string keyString = Console.ReadLine();
  46.             int indexFirstDigit = 0;
  47.             for (int i = 0; i < keyString.Length; i++)
  48.             {
  49.                 if (!allDigitsStr.Contains(keyString[i])) continue;
  50.                 indexFirstDigit = i;
  51.                 break;
  52.             }
  53.  
  54.             string firstPart = keyString.Substring(0, indexFirstDigit);
  55.             string lastPart = keyString.Substring(indexFirstDigit + 1);
  56.  
  57.             string[] keysArr = new string[2];
  58.             if (Regex.IsMatch(firstPart, startKeyPattern))
  59.             {
  60.                 keysArr[0] = firstPart;
  61.             }
  62.  
  63.             Regex rgx = new Regex(endKeyPattern);
  64.             Match match = rgx.Match(lastPart);
  65.             if (match.Groups.Count == 2)
  66.             {
  67.                 keysArr[1] = match.Groups[1].Value;
  68.             }
  69.  
  70.             return keysArr;
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement