Averages/Root mean square: Difference between revisions

Content added Content deleted
No edit summary
(→‎{{header|Python}}: Added a single fold version using `reduce`)
Line 1,406: Line 1,406:
<small>Note that function [http://docs.python.org/release/3.2/library/functions.html#range range] in Python includes the first limit of 1, excludes the second limit of 11, and has a default increment of 1.</small>
<small>Note that function [http://docs.python.org/release/3.2/library/functions.html#range range] in Python includes the first limit of 1, excludes the second limit of 11, and has a default increment of 1.</small>


The Python 2 version is nearly identical, except you must cast the sum to a float to get float division instead of integer division; or better, do a <code>from __future__ import division</code>, which works on Python 2.2+ as well as Python 3, and makes division work consistently like it does in Python 3.
The Python 2 version of this is nearly identical, except you must cast the sum to a float to get float division instead of integer division; or better, do a <code>from __future__ import division</code>, which works on Python 2.2+ as well as Python 3, and makes division work consistently like it does in Python 3.


Alternatively in terms of '''reduce''':
<lang python>from functools import (reduce)
from math import (sqrt)


# rootMeanSquare :: [Num] -> Float
def rootMeanSquare(xs):
return sqrt(reduce(lambda a, x: a + x * x, xs, 0) / len(xs))


print(
rootMeanSquare([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
)</lang>
{{Out}}
<pre>6.2048368229954285</pre>


=={{header|Qi}}==
=={{header|Qi}}==