Advertisement
Guest User

Untitled

a guest
Apr 13th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5.  
  6. namespace _13_LoadTimeCalculator
  7. {
  8.     class LoadTimeCalculator
  9.     {
  10.        
  11.         static void Main()
  12.        
  13.         {
  14.             string logData = @"2014-Apr-01 02:01 http://softuni.bg 8.37725
  15. 2014-Apr-01 02:05 http://www.nakov.com 11.622
  16. 2014-Apr-01 02:06 http://softuni.bg 4.33
  17. 2014-Apr-01 02:11 http://www.google.com 1.94
  18. 2014-Apr-01 02:11 http://www.google.com 2.011
  19. 2014-Apr-01 02:12 http://www.google.com 4.882
  20. 2014-Apr-01 02:34 http://softuni.bg 4.885
  21. 2014-Apr-01 02:36 http://www.nakov.com 10.74
  22. 2014-Apr-01 02:36 http://www.nakov.com 11.75
  23. 2014-Apr-01 02:38 http://softuni.bg 3.886
  24. 2014-Apr-01 02:44 http://www.google.com 1.04
  25. 2014-Apr-01 02:48 http://www.google.com 1.4555
  26. 2014-Apr-01 02:55 http://www.google.com 1.977
  27.  
  28.                            ";
  29.  
  30.             string logLine = null;
  31.             StringReader strReader = new StringReader(logData);
  32.             List<LogObject> log = new List<LogObject>();
  33.             bool loop = true;
  34.             while(loop)
  35.             {
  36.                 logLine = strReader.ReadLine();
  37.                 if (logLine != null)
  38.                 {
  39.                     //check if the logLine is empty
  40.                     if (!string.IsNullOrWhiteSpace(logLine))
  41.                     {
  42.                         string[] logLineParsed = logLine.Trim().Split(' ');
  43.                         string url = logLineParsed[2];
  44.                         double loadTime = double.Parse(logLineParsed[3], CultureInfo.CreateSpecificCulture("en-GB"));
  45.                         bool existsInLog = false;
  46.                         int logID = 0;
  47.                         for (int i = 0; i < log.Count; i++)
  48.                         {
  49.                             if (log[i].url == url)
  50.                             {
  51.                                 existsInLog = true;
  52.                                 logID = i;
  53.                                 break;
  54.                             }
  55.                         }
  56.                         //if the url exist in the log
  57.                         if (existsInLog)
  58.                         {
  59.                             double avTime = (log[logID].loadTime * log[logID].count + loadTime) / (log[logID].count + 1);
  60.                             log[logID].loadTime = avTime;
  61.                             log[logID].count = log[logID].count + 1;
  62.                         }
  63.                         else
  64.                         {
  65.                             LogObject data = new LogObject();
  66.                             data.url = url;
  67.                             data.loadTime = loadTime;
  68.                             data.count = 1;
  69.                             log.Add(data);
  70.                         }
  71.                     }                  
  72.                 }
  73.                 else
  74.                 {
  75.                     //exit from loop
  76.                     loop = false;
  77.                 }
  78.             }
  79.  
  80.             PrintResult(log);
  81.         }
  82.  
  83.         static void PrintResult(List<LogObject> log)
  84.         {
  85.             for (int i = 0; i < log.Count; i++)
  86.             {
  87.                 Console.WriteLine("{0} -> {1}", log[i].url, log[i].loadTime);
  88.             }
  89.         }
  90.     }
  91.  
  92.     public class LogObject
  93.     {
  94.         public string url { get; set; }
  95.         public double loadTime { get; set; }
  96.         public int count { get; set; }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement