Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Delegates.PairsAnalysis
- {
- public static class EnumerableExtentions
- {
- public static IEnumerable<Tuple<T, T>> GetPairs<T>(this IEnumerable<T> sequence)
- {
- if (sequence.Count() < 2)
- throw new InvalidOperationException();
- var isFirstElement = true;
- var previous = default(T);
- foreach(var val in sequence)
- {
- if (isFirstElement)
- {
- previous = val;
- isFirstElement = false;
- continue;
- }
- yield return Tuple.Create(previous, val);
- previous = val;
- }
- }
- public static int GetMaxIndex<T>(this IEnumerable<T> sequence)
- where T : IComparable<T>
- {
- if (sequence.Count() == 0)
- throw new InvalidOperationException();
- var i = 0;
- var max = sequence.First();
- var maxIndex = 0;
- foreach (var val in sequence)
- {
- if (val.CompareTo(max) > 0)
- {
- max = val;
- maxIndex = i;
- }
- i++;
- }
- return maxIndex;
- }
- }
- public static class Analysis
- {
- public static int FindMaxPeriodIndex(params DateTime[] data)
- {
- return data
- .GetPairs()
- .Select((pair) => (pair.Item2 - pair.Item1).TotalSeconds)
- .GetMaxIndex();
- }
- public static double FindAverageRelativeDifference(params double[] data)
- {
- var sequenceRelativeDifferences = data
- .GetPairs()
- .Select((pair) => (pair.Item2 - pair.Item1) / pair.Item1);
- return sequenceRelativeDifferences.Sum() / sequenceRelativeDifferences.Count();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement