Advertisement
dimipan80

Advanced Topics 13. Average Load Time Calculator

Jul 4th, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. // We have a report that holds dates, web site URLs and load times (in seconds) in the same format like in the examples below. Your tasks is to calculate the average load time for each URL. Print the URLs in the same order as they first appear in the input report. Use double floating-point precision.
  2.  
  3. namespace _13.AverageLoadTimeCalculator
  4. {
  5.     using System;
  6.     using System.Collections.Generic;
  7.     using System.Linq;    
  8.  
  9.     public class AverageLoadTimeCalculator
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.             checked
  14.             {
  15.                 Console.WriteLine("Enter your Web sites load times Report:");
  16.                 string reportStr = Console.ReadLine();
  17.  
  18.                 Dictionary<string, List<double>> websitesTimes = new Dictionary<string, List<double>>();
  19.                 while (reportStr != string.Empty)
  20.                 {
  21.                     char[] separators = new char[] { ' ', ',', ';' };
  22.                     string[] reportArr = reportStr.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  23.                     for (int i = 2; i < reportArr.Length; i++)
  24.                     {
  25.                         if (i == 2)
  26.                         {
  27.                             string key = reportArr[i];
  28.                             double time = double.Parse(reportArr[i + 1]);
  29.                             if (!websitesTimes.Keys.Contains(key))
  30.                             {
  31.                                 websitesTimes.Add(key, new List<double>());
  32.                             }
  33.  
  34.                             websitesTimes[key].Add(time);
  35.                         }
  36.                     }
  37.  
  38.                     reportStr = Console.ReadLine();
  39.                 }
  40.  
  41.                 Console.WriteLine("The Average Load Time for each Web Site in that report is:");
  42.                 foreach (var url in websitesTimes)
  43.                 {
  44.                     Console.WriteLine("{0} -> {1}", url.Key, url.Value.Average());
  45.                 }
  46.             }
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement