Arithmetic mean for figure skaters#7368

Arithmetic mean for figure skaters (Editorial)

For each athlete, find the minimum and maximum scores. Then, compute the average score excluding and .

Example

For the first skater, the minimum score is , and the maximum score is . The average of the scores excluding and is (8 + 9 + 8) / 3 = 8.33.

For the last skater, the minimum score is , and the maximum score is . The average of the scores excluding and is (8 + 9) / 2 = 8.50.

Algorithm implementation

Read the number of judges and the number of athletes .

scanf("%d %d", &n, &m);

Process the data for each skater sequentially.

for (i = 0; i < m; i++)
{
  scanf("%d", &mas[0]);

Find the smallest and largest scores for the current -th skater.

  min = max = mas[0];
  for (j = 1; j < n; j++)
  {
    scanf("%d", &mas[j]);
    if (mas[j] > max) max = mas[j];
    if (mas[j] < min) min = mas[j];
  }

Compute:

  sum = cnt = 0;
  for (j = 0; j < n; j++)

The smallest and largest scores are excluded from the computations.

    if ((mas[j] != min) && (mas[j] != max))
    {
      sum += mas[j];
      cnt++;
    }

Compute and print the average score for the -th skater.

  res = 1.0 * sum / cnt;
  printf("%.2lf ", res);
}
printf("\n");

Python implementation

Read the number of judges and the number of athletes .

n, m = map(int,input().split())

Process the data for each skater sequentially.

for _ in range(m):
  lst = list(map(int,input().split()))

Find the smallest and largest scores for the current skater.

  min_el = min(lst)
  max_el = max(lst)

Compute:

  s = sum(x for x in lst if x != min_el and x != max_el)
  cnt = sum(1 for x in lst if x != min_el and x != max_el)

Compute and print the average score for the current skater.

  print("%.2f" %(s / cnt), end = ' ')