example: add arithmetic mean example from rosetta code
authorAlexandre Terrasa <alexandre@moz-code.org>
Wed, 24 Jun 2015 18:45:38 +0000 (14:45 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Wed, 24 Jun 2015 18:45:38 +0000 (14:45 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

examples/rosettacode/arithmetic_mean.nit [new file with mode: 0644]
tests/sav/arithmetic_mean.res [new file with mode: 0644]

diff --git a/examples/rosettacode/arithmetic_mean.nit b/examples/rosettacode/arithmetic_mean.nit
new file mode 100644 (file)
index 0000000..cb5d71b
--- /dev/null
@@ -0,0 +1,24 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Arithmetic mean
+# SEE: <http://rosettacode.org/wiki/Averages/Arithmetic_mean>
+module arithmetic_mean
+
+# Computes the meam or the `array` values.
+fun mean(array: Array[Float]): Float do
+       var sum = 0.0
+       if array.is_empty then return sum
+       for i in array do sum += i
+       return sum / array.length.to_f
+end
+
+print mean([3.0, 1.0, 4.0, 1.0, 5.0, 9.0])
+print mean([3.0, 1.0, 4.0, 1.0, 5.0])
+print mean([3.0, 1.0, 4.0, 1.0])
+print mean([3.0, 1.0, 4.0])
+print mean([3.0, 1.0])
+print mean([3.0])
+print mean(new Array[Float])
diff --git a/tests/sav/arithmetic_mean.res b/tests/sav/arithmetic_mean.res
new file mode 100644 (file)
index 0000000..784b1b2
--- /dev/null
@@ -0,0 +1,7 @@
+3.833
+2.8
+2.25
+2.667
+2.0
+3.0
+0.0