2021-06-15 00:49:18 -07:00
---
id: 594d966a1467eb84194f0086
2021-08-02 23:05:44 +09:00
title: Média/Médias pitagóricas
2021-06-15 00:49:18 -07:00
challengeType: 5
forumTopicId: 302227
dashedName: averagespythagorean-means
---
# --description--
2021-08-02 23:05:44 +09:00
Calcule as três [médias pitagóricas ](https://en.wikipedia.org/wiki/Pythagorean means "wp: Pythagorean means" ) do conjunto de inteiros de $1$ a $10$ (inclusive).
2021-06-15 00:49:18 -07:00
2021-08-02 23:05:44 +09:00
Exiba $A(x_1,\\ldots,x_n) \\geq G(x_1,\\ldots,x_n) \\geq H(x_1,\\ldots,x_n)$ para este conjunto de inteiros positivos.
2021-06-15 00:49:18 -07:00
< ul >
2021-08-02 23:05:44 +09:00
< li > A mais comum das três médias, a < a class = 'rosetta__link--rosetta' href = 'https://rosettacode.org/wiki/Averages/Arithmetic mean' title = 'Averages/Arithmetic mean' target = '_blank' > média aritmética< / a > , é a soma da lista dividida pelo seu tamanho:< br >
2021-06-15 00:49:18 -07:00
< big > $ A(x_1, \ldots, x_n) = \frac{x_1 + \cdots + x_n}{n}$</ big ></ li >
2021-08-02 23:05:44 +09:00
< li > A < a class = 'rosetta__link--wiki' href = 'https://en.wikipedia.org/wiki/Geometric mean' title = 'wp: Geometric mean' target = '_blank' > média geométrica< / a > é a $n$-ésima raiz do produto da lista:< br >
2021-06-15 00:49:18 -07:00
< big > $ G(x_1, \ldots, x_n) = \sqrt[n]{x_1 \cdots x_n} $</ big ></ li >
2021-08-02 23:05:44 +09:00
< li > A < a class = 'rosetta__link--wiki' href = 'https://en.wikipedia.org/wiki/Harmonic mean' title = 'wp: Harmonic mean' target = '_blank' > média harmônica< / a > é $n$ dividido pela soma dos recíprocos de cada item da lista:< br >
2021-06-15 00:49:18 -07:00
< big > $ H(x_1, \ldots, x_n) = \frac{n}{\frac{1}{x_1} + \cdots + \frac{1}{x_n}} $</ big ></ li >
< / ul >
# --instructions--
2021-08-02 23:05:44 +09:00
Ao escrever sua função, assuma que a entrada é um array ordenado incluindo todos os números.
2021-06-15 00:49:18 -07:00
2021-08-02 23:05:44 +09:00
Para a resposta, dê como resultado um objeto com o seguinte formato:
2021-06-15 00:49:18 -07:00
```js
{
values: {
Arithmetic: 5.5,
Geometric: 4.528728688116765,
Harmonic: 3.414171521474055
},
test: 'is A >= G >= H ? yes'
}
```
# --hints--
2021-08-02 23:05:44 +09:00
`pythagoreanMeans` deve ser uma função.
2021-06-15 00:49:18 -07:00
```js
assert(typeof pythagoreanMeans === 'function');
```
2021-08-02 23:05:44 +09:00
`pythagoreanMeans([1, 2, ..., 10])` deve ser igual ao mesmo resultado acima.
2021-06-15 00:49:18 -07:00
```js
assert.deepEqual(pythagoreanMeans(range1), answer1);
```
# --seed--
## --after-user-code--
```js
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'
};
```
## --seed-contents--
```js
function pythagoreanMeans(rangeArr) {
}
```
# --solutions--
```js
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'}`
};
}
```