“从给定的集合中找到最高的连续工资”

huangapple go评论81阅读模式
英文:

Find highest consecutive salary from given collection

问题

例如,我们有一个集合:

[
  {1, 'A', 100},
  {2, 'X', 200},
  {3, 'D', 300},
  {5, 'B', 300},
  {6, 'Z', 300},
  {7, 'G', 300},
]

同样的方式。

我从最后一个对象开始,如果我的限制是3,那么我会选择最后三个来计算整数的平均值。

在这个例子中,首先是1, 2, 3,然后是2, 3, 4,但是集合中缺少4,所以我会跳过它,然后选择3, 4, 5,同样,我需要跳过这个,最后我会选择5, 6, 7,以此类推,直到达到N个记录。

然后,我需要选择一个包含最高平均数的集合。

英文:

e.g. We have a collection

[ 
  {1,' A,' 100},
  {2, 'X', 200},
  {3, 'D', 300},
  {5, 'B', 300},
  {6, 'Z', 300},
  {7, 'G', 300}, 
]

likewise.

I start with the last object first then If my limit is 3 then I'll pick the last three to find the average of int num.

In this 1, 2, 3 then 2, 3, 4 but 4 is missing from the collection then I will skip this and pick 3, 4, 5 same I need to skip this finally I will start 5, 6, 7 wise I will go N record.

Then I need to pick a set that contains the highest average of the number.

答案1

得分: 1

以下是您要求的翻译:

"Are you looking for a moving average? Having a window of size == 3 you compute averages of 0..2 items, then of 1..3 items, etc:"

"您是否正在寻找滑动平均?使用大小为3的窗口,您会计算0..2项的平均值,然后计算1..3项的平均值,依此类推:"

If it's your task, then you can do it as follow:

如果这是您的任务,那么您可以按照以下方式完成:

Demo:

演示:

Output:

输出:

Having moving average computed, you can easily obtain max record from it with the help of Linq:

计算了移动平均值后,您可以轻松地使用Linq获取最大记录:

Output:

输出:

英文:

Are you looking for a moving average? Having a window of size == 3 you compute averages of 0..2 items, then of 1..3 items, etc:

  (1, 'A', 100), -> not enough data to compute moving average
  (2, 'X', 200), -> not enough data to compute moving average
  (3, 'D', 300), -> (100 + 200 + 300) / 3 == 200
  (5, 'B', 300), -> (200 + 300 + 300) / 3 == 267
  (6, 'Z', 300), -> (300 + 300 + 300) / 3 == 300
  (7, 'G', 300), -> (300 + 300 + 300) / 3 == 300

If it's your task, then you can do it as follow:

  private static IEnumerable<(T item, double average)> MovingAverage<T>(
    IEnumerable<T> source, Func<T, double> selector, int windowSize) {

    if (source is null)
      throw new ArgumentNullException(nameof(source));
    if (selector is null)
      throw new ArgumentNullException(nameof(selector));
    if (selector is null)
      throw new ArgumentNullException(nameof(selector));
    if (windowSize <= 0)
      throw new ArgumentOutOfRangeException(nameof(windowSize));

    double total = 0.0;
    var priors = new Queue<double>();

    foreach (var item in source) {
      double value = selector(item);
      priors.Enqueue(value);
	  total += value;	
  
      while (priors.Count > windowSize)
        total -= priors.Dequeue();

      if (priors.Count == windowSize)    
        yield return (item, total / windowSize);
    }
  }	

Demo:

var data = new (int id, char name, int value)[] { 
  (1, 'A', 100),
  (2, 'X', 200),
  (3, 'D', 300),
  (5, 'B', 300),
  (6, 'Z', 300),
  (7, 'G', 300),
};
	  
var result = string.Join(Environment.NewLine, 
   MovingAverage(data, item => item.value, 3)
  .Select(item => $"{item.item} :: {Math.Round(item.average)}"));

Console.Write(result);

Output:

(3, D, 300) :: 200   // (100 + 200 + 300) / 3
(5, B, 300) :: 267   // (200 + 300 + 300) / 3
(6, Z, 300) :: 300   // (300 + 300 + 300) / 3
(7, G, 300) :: 300   // (300 + 300 + 300) / 3

Having moving average computed, you can easily obtain max record from it with a help of Linq:

    using System.Linq;

    ...

	var max = MovingAverage(data, item => item.value, 3)
	  .MaxBy(pair => pair.average);	  
		
	Console.WriteLine($"Max: {max.item} :: {max.average}");   

Output:

Max: (6, Z, 300) :: 300

which means that max moving average (that is 300) group starts from (6, Z, 300) item.

Fiddle

huangapple
  • 本文由 发表于 2023年5月18日 11:58:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定