Advertisement
_IlyaVasilev_

Untitled

Mar 13th, 2021 (edited)
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Delegates.PairsAnalysis
  8. {
  9.     public static class EnumerableExtentions
  10.     {
  11.         public static IEnumerable<Tuple<T, T>> GetPairs<T>(this IEnumerable<T> sequence)
  12.         {
  13.             if (sequence.Count() < 2)
  14.                 throw new InvalidOperationException();
  15.             var isFirstElement = true;
  16.             var previous = default(T);
  17.             foreach(var val in sequence)
  18.             {
  19.                 if (isFirstElement)
  20.                 {
  21.                     previous = val;
  22.                     isFirstElement = false;
  23.                     continue;
  24.                 }
  25.                 yield return Tuple.Create(previous, val);
  26.                 previous = val;
  27.             }
  28.         }
  29.  
  30.         public static int GetMaxIndex<T>(this IEnumerable<T> sequence)
  31.             where T : IComparable<T>
  32.         {
  33.             if (sequence.Count() == 0)
  34.                 throw new InvalidOperationException();
  35.             var i = 0;
  36.             var max = sequence.First();
  37.             var maxIndex = 0;
  38.             foreach (var val in sequence)
  39.             {
  40.                 if (val.CompareTo(max) > 0)
  41.                 {
  42.                     max = val;
  43.                     maxIndex = i;
  44.                 }
  45.                 i++;
  46.             }
  47.             return maxIndex;
  48.         }
  49.     }
  50.  
  51.     public static class Analysis
  52.     {
  53.         public static int FindMaxPeriodIndex(params DateTime[] data)
  54.         {
  55.             return data
  56.                 .GetPairs()
  57.                 .Select((pair) => (pair.Item2 - pair.Item1).TotalSeconds)
  58.                 .GetMaxIndex();
  59.         }
  60.  
  61.         public static double FindAverageRelativeDifference(params double[] data)
  62.         {
  63.             var sequenceRelativeDifferences = data
  64.                 .GetPairs()
  65.                 .Select((pair) => (pair.Item2 - pair.Item1) / pair.Item1);
  66.             return sequenceRelativeDifferences.Sum() / sequenceRelativeDifferences.Count();
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement