freeCodeCamp/curriculum/challenges/spanish/08-coding-interview-prep/rosetta-code/averages-pythagorean-means....

4.5 KiB

title id localeTitle challengeType
Averages-Pythagorean means 594d966a1467eb84194f0086 594d966a1467eb84194f0086 5

Description

Calcule los tres medios pitagóricos del conjunto de enteros del 1 al 10 (inclusive).

Muestre que $ A (x_1, \ ldots, x_n) \ geq G (x_1, \ ldots, x_n) \ geq H (x_1, \ ldots, x_n) $ para este conjunto de enteros positivos.

El más común de los tres medios, la media aritmética , es la suma de la lista dividida por su longitud: $ A (x_1, \ ldots, x_n) = \ frac {x_1 + \ cdots + x_n} {n} $ La geometría media es la raíz $ n $ th del producto de la lista: $ G (x_1, \ ldots, x_n) = \ sqrt [n] {x_1 \ cdots x_n} $ La media armónica es $ n $ dividida por la suma de el recíproco de cada elemento en la lista: $ H (x_1, \ ldots, x_n) = \ frac {n} {\ frac {1} {x_1} + \ cdots + \ frac {1} {x_n}} $

Suponga que la entrada es una matriz ordenada de todos los números inclusivos.

Para la respuesta, envíe un objeto en el siguiente formato:

 
{ 
valores: { 
Aritmética: 5.5, 
Geométrica: 4.528728688116765, 
Armónica: 3.414171521474055 
}, 
prueba: 'es A> = G> = H? si ' 
} 

Instructions

Tests

tests:
  - text: <code>pythagoreanMeans</code> es una función.
    testString: 'assert(typeof pythagoreanMeans === "function", "<code>pythagoreanMeans</code> is a function.");'
  - text: &#39; <code>pythagoreanMeans([1, 2, ..., 10])</code> debe ser igual a la salida anterior.&#39;
    testString: 'assert.deepEqual(pythagoreanMeans(range1), answer1, "<code>pythagoreanMeans([1, 2, ..., 10])</code> should equal the same output above.");'

Challenge Seed

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

After Test

console.info('after the test');

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'}`
  };
}