Advertisement
dimipan80

Biggest Table Row

May 27th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. /* You are given an HTML table with 4 columns: Town, Store1, Store2 and Store3. It consists of sequence of text lines: the "<table>" tag, the header row, several data rows, and "</table>" tag (see the examples below). The Store1, Store2, and Store3 columns hold either numbers or "-" (which means "no data"). Your task is to write a program which parses the table data rows and finds the row with a maximal sum of its values.
  2.  * The input is read from the console on several lines, each holding the table rows. The last row will always hold the string "</table>".
  3.  * Print at the console a single line, holding the data row values with maximal sum in format: "sum = value1 + value2 + …". Print the values exactly as they were found in the input (no rounding, no reformatting). If all rows contain no data, print "no data". If two rows have the same maximal sum, print the first of them. */
  4.  
  5. namespace Biggest_Table_Row
  6. {
  7.     using System;
  8.     using System.Collections.Generic;
  9.     using System.Text.RegularExpressions;
  10.  
  11.     class BiggestTableRow
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             const string pattern = @"<tr><td>[A-Za-z]+<\/td><td>([\d.-]+)<\/td><td>([\d.-]+)<\/td><td>([\d.-]+)<\/td><\/tr>";
  16.             Dictionary<double, List<string>> rowSums = new Dictionary<double, List<string>>();
  17.             double maxSum = Double.MinValue;
  18.             Regex rgx = new Regex(pattern);
  19.             string inputLine = Console.ReadLine();
  20.             int lineCounter = 0;
  21.             while (inputLine != "</table>")
  22.             {
  23.                 if (lineCounter > 1)
  24.                 {
  25.                     List<string> valuesList = new List<string>();
  26.                     Match match = rgx.Match(inputLine);
  27.                     double sum = 0;
  28.                     for (int i = 1; i < 4; i++)
  29.                     {
  30.                         if (match.Groups[i].Value == "-") continue;
  31.  
  32.                         sum += double.Parse(match.Groups[i].Value);
  33.                         valuesList.Add(match.Groups[i].Value);
  34.                     }
  35.  
  36.                     if (valuesList.Count > 0)
  37.                     {
  38.                         if (!rowSums.ContainsKey(sum) && sum > maxSum)
  39.                         {
  40.                             rowSums.Add(sum, valuesList);
  41.                             maxSum = sum;
  42.                         }
  43.                     }
  44.                 }
  45.  
  46.                 lineCounter++;
  47.                 inputLine = Console.ReadLine();
  48.             }
  49.  
  50.             if (rowSums.Count == 0)
  51.             {
  52.                 Console.WriteLine("no data");
  53.             }
  54.             else
  55.             {
  56.                 Console.WriteLine("{0} = {1}",
  57.                 maxSum, string.Join(" + ", rowSums[maxSum]));
  58.             }
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement