Files
freeCodeCamp/curriculum/challenges/russian/08-coding-interview-prep/rosetta-code/averages-pythagorean-means.russian.md

5.5 KiB
Raw Blame History

title, id, challengeType, forumTopicId, localeTitle
title id challengeType forumTopicId localeTitle
Averages/Pythagorean means 594d966a1467eb84194f0086 5 302227 Средние значения - пифагорейские средства

Description

Вычислите все три пифагорейских средства набора целых чисел от 1 до 10 (включительно).

Покажите, что для этого набора натуральных чисел $ A (x_1, \ ldots, x_n) \ geq G (x_1, \ ldots, x_n) \ geq H (x_1, \ ldots, x_n) $ .

Наиболее распространенным из трех означает среднее арифметическое - это сумма списка, деленная на его длину: $ A (x_1, \ ldots, x_n) = \ frac {x_1 + \ cdots + x_n} {n} $ Геометрическая Среднее означает $ n $ -ый корень из произведения списка: $ G (x_1, \ ldots, x_n) = \ sqrt [n] {x_1 \ cdots x_n} $ Гармоническое среднее $ n $, деленное на сумму обратный каждому элементу в списке: $ H (x_1, \ ldots, x_n) = \ frac {n} {\ frac {1} {x_1} + \ cdots + \ frac {1} {x_n}} $

Предположим, что вход представляет собой упорядоченный массив всех включенных чисел.

Для ответа, пожалуйста, выведите объект в следующем формате:

 {
  значения: {
    Арифметика: 5.5,
    Геометрический: 4.528728688116765,
    Гармонический: 3.414171521474055
  },
  test: 'является A> = G> = H? да'
}

Instructions

When writing your function, assume the input is an ordered array of all inclusive numbers. For the answer, please output an object in the following format:
{
  values: {
    Arithmetic: 5.5,
    Geometric: 4.528728688116765,
    Harmonic: 3.414171521474055
  },
  test: 'is A >= G >= H ? yes'
}

Tests

tests:
  - text: <code>pythagoreanMeans</code> is a function.
    testString: assert(typeof pythagoreanMeans === 'function');
  - text: <code>pythagoreanMeans([1, 2, ..., 10])</code> should equal the same output above.
    testString: assert.deepEqual(pythagoreanMeans(range1), answer1);

Challenge Seed

function pythagoreanMeans(rangeArr) {
  // Good luck!
}

After Tests

const range1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const answer1 = {
  values: {
    Arithmetic: 5.5,
    Geometric: 4.528728688116765,
    Harmonic: 3.414171521474055
  },
  test: 'is A >= G >= H ? yes'
};

Solution

function pythagoreanMeans(rangeArr) {
  // arithmeticMean :: [Number] -> Number
  const arithmeticMean = xs =>
    foldl((sum, n) => sum + n, 0, xs) / length(xs);

  // geometricMean :: [Number] -> Number
  const geometricMean = xs =>
    raise(foldl((product, x) => product * x, 1, xs), 1 / length(xs));

  // harmonicMean :: [Number] -> Number
  const harmonicMean = xs =>
    length(xs) / foldl((invSum, n) => invSum + (1 / n), 0, xs);

  // GENERIC FUNCTIONS ------------------------------------------------------

  // A list of functions applied to a list of arguments
  // <*> :: [(a -> b)] -> [a] -> [b]
  const ap = (fs, xs) => //
    Array.prototype.concat(...fs.map(f => //
      Array.prototype.concat(...xs.map(x => [f(x)]))));

  // foldl :: (b -> a -> b) -> b -> [a] -> b
  const foldl = (f, a, xs) => xs.reduce(f, a);

  // length :: [a] -> Int
  const length = xs => xs.length;

  // mapFromList :: [(k, v)] -> Dictionary
  const mapFromList = kvs =>
    foldl((a, [k, v]) =>
      (a[(typeof k === 'string' && k)] = v, a), {}, kvs);

  // raise :: Num -> Int -> Num
  const raise = (n, e) => Math.pow(n, e);
/*
  // show :: a -> String
  // show :: a -> Int -> String
  const show = (...x) =>
    JSON.stringify.apply(
      null, x.length > 1 ? [x[0], null, x[1]] : x
    );
*/
  // zip :: [a] -> [b] -> [(a,b)]
  const zip = (xs, ys) =>
    xs.slice(0, Math.min(xs.length, ys.length))
      .map((x, i) => [x, ys[i]]);

  // TEST -------------------------------------------------------------------
  // mean :: Dictionary
  const mean = mapFromList(zip(
    ['Arithmetic', 'Geometric', 'Harmonic'],
    ap([arithmeticMean, geometricMean, harmonicMean], [
      rangeArr
    ])
  ));

  return {
    values: mean,
    test: `is A >= G >= H ? ${mean.Arithmetic >= mean.Geometric &&
      mean.Geometric >= mean.Harmonic ? 'yes' : 'no'}`
  };
}