langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
|
|
@ -0,0 +1,17 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
using Nemerle.Collections;
|
||||
|
||||
module Mean
|
||||
{
|
||||
ArithmeticMean(x : list[int]) : double
|
||||
{
|
||||
|[] => 0.0
|
||||
|_ =>(x.FoldLeft(0, _+_) :> double) / x.Length
|
||||
}
|
||||
|
||||
Main() : void
|
||||
{
|
||||
WriteLine("Mean of [1 .. 10]: {0}", ArithmeticMean($[1 .. 10]));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
launchSample()
|
||||
return
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method arithmeticMean(vv = Vector) public static signals DivideException returns Rexx
|
||||
sum = 0
|
||||
n_ = Rexx
|
||||
loop n_ over vv
|
||||
sum = sum + n_
|
||||
end n_
|
||||
mean = sum / vv.size()
|
||||
|
||||
return mean
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method launchSample() public static
|
||||
TRUE_ = 1 == 1
|
||||
FALSE_ = \TRUE_
|
||||
tracing = FALSE_
|
||||
vectors = getSampleData()
|
||||
loop v_ = 0 to vectors.length - 1
|
||||
say 'Average of:' vectors[v_].toString()
|
||||
do
|
||||
say ' =' arithmeticMean(vectors[v_])
|
||||
catch dex = DivideException
|
||||
say 'Caught "Divide By Zero"; bypassing...'
|
||||
if tracing then dex.printStackTrace()
|
||||
catch xex = RuntimeException
|
||||
say 'Caught unspecified run-time exception; bypassing...'
|
||||
if tracing then xex.printStackTrace()
|
||||
end
|
||||
say
|
||||
end v_
|
||||
return
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method getSampleData() private static returns Vector[]
|
||||
seed = 1066
|
||||
rng = Random(seed)
|
||||
vectors =[ -
|
||||
Vector(Arrays.asList([Rexx 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])), -
|
||||
Vector(), -
|
||||
Vector(Arrays.asList([Rexx rng.nextInt(seed), rng.nextInt(seed), rng.nextInt(seed), rng.nextInt(seed), rng.nextInt(seed), rng.nextInt(seed)])), -
|
||||
Vector(Arrays.asList([Rexx rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), rng.nextDouble()])), -
|
||||
Vector(Arrays.asList([Rexx '1.0', '2.0', 3.0])), -
|
||||
Vector(Arrays.asList([Rexx '1.0', 'not a number', 3.0])) -
|
||||
]
|
||||
return vectors
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(define (Mean Lst)
|
||||
(if (empty? Lst)
|
||||
0
|
||||
(/ (apply + Lst) (length Lst))))
|
||||
|
||||
(Mean (sequence 1 1000))-> 500
|
||||
(Mean '()) -> 0
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
mean is / [sum, tally]
|
||||
|
||||
mean 6 2 4
|
||||
= 4
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
dtally is recur [ empty rest, 1 first, 1 first, plus, rest ]
|
||||
mean is / [sum, dtally]
|
||||
|
||||
mean []
|
||||
=0
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
[ [ , len 1 - at ! ] len 3 - times swap , ] 'map ; ( a Lisp like map, to sum the stack )
|
||||
[ len 'n ; [ + ] 0 n swap-at map n / ] 'avg ;
|
||||
|
||||
1 2 3 4 5 avg .
|
||||
=> 3
|
||||
3.4 2.3 .01 2.0 2.1 avg .
|
||||
=> 1.9619999999999997
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
let mean_floats = function
|
||||
| [] -> 0.
|
||||
| xs -> List.fold_left (+.) 0. xs /. float_of_int (List.length xs)
|
||||
|
||||
let mean_ints xs = mean_floats (List.map float_of_int xs)
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
let mean_floats xs =
|
||||
if xs = [] then
|
||||
invalid_arg "empty list"
|
||||
else
|
||||
let total, length =
|
||||
List.fold_left
|
||||
(fun (tot,len) x -> (x +. tot), len +. 1.)
|
||||
(0., 0.) xs
|
||||
in
|
||||
(total /. length)
|
||||
;;
|
||||
|
||||
|
||||
let mean_ints xs =
|
||||
if xs = [] then
|
||||
invalid_arg "empty list"
|
||||
else
|
||||
let total, length =
|
||||
List.fold_left
|
||||
(fun (tot,len) x -> (x + tot), len +. 1.)
|
||||
(0, 0.) xs
|
||||
in
|
||||
(float total /. length)
|
||||
;;
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
MODULE AvgMean;
|
||||
IMPORT Out;
|
||||
CONST MAXSIZE = 10;
|
||||
PROCEDURE Avg(a: ARRAY OF REAL; items: INTEGER): REAL;
|
||||
VAR
|
||||
i: INTEGER;
|
||||
total: REAL;
|
||||
BEGIN
|
||||
total := 0.0;
|
||||
FOR i := 0 TO LEN(a) - 1 DO
|
||||
total := total + a[i]
|
||||
END;
|
||||
RETURN total/LEN(a)
|
||||
END Avg;
|
||||
VAR
|
||||
ary: ARRAY MAXSIZE OF REAL;
|
||||
BEGIN
|
||||
ary[0] := 10.0;
|
||||
ary[1] := 11.01;
|
||||
ary[2] := 12.02;
|
||||
ary[3] := 13.03;
|
||||
ary[4] := 14.04;
|
||||
ary[5] := 15.05;
|
||||
ary[6] := 16.06;
|
||||
ary[7] := 17.07;
|
||||
ary[8] := 18.08;
|
||||
ary[9] := 19.09;
|
||||
Out.Fixed(Avg(ary),4,2);Out.Ln
|
||||
END AvgMean.
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
function : native : PrintAverage(values : FloatVector) ~ Nil {
|
||||
values->Average()->PrintLine();
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
function m = omean(l)
|
||||
if ( numel(l) == 0 )
|
||||
m = 0;
|
||||
else
|
||||
m = mean(l);
|
||||
endif
|
||||
endfunction
|
||||
|
||||
disp(omean([]));
|
||||
disp(omean([1,2,3]));
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
function m = omean(l)
|
||||
n = sum(~isnan(l));
|
||||
l(isnan(l))=0;
|
||||
s = sum(l);
|
||||
m = s./n;
|
||||
end;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
declare
|
||||
fun {Mean Xs}
|
||||
{FoldL Xs Number.'+' 0.0} / {Int.toFloat {Length Xs}}
|
||||
end
|
||||
in
|
||||
{Show {Mean [3. 1. 4. 1. 5. 9.]}}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
avg(v)={
|
||||
if(#v,vecsum(v)/#v)
|
||||
};
|
||||
|
|
@ -0,0 +1 @@
|
|||
arithmetic_mean = sum(A)/dimension(A,1);
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
Program Mean;
|
||||
|
||||
function DoMean(vector: array of double): double;
|
||||
var
|
||||
sum: double;
|
||||
i, len: integer;
|
||||
begin
|
||||
sum := 0;
|
||||
len := length(vector);
|
||||
if len > 0 then
|
||||
begin
|
||||
for i := low(vector) to high(vector) do
|
||||
sum := sum + vector[i];
|
||||
sum := sum / len;
|
||||
end;
|
||||
DoMean := sum;
|
||||
end;
|
||||
|
||||
const
|
||||
vector: array [3..8] of double = (3.0, 1.0, 4.0, 1.0, 5.0, 9.0);
|
||||
var
|
||||
i: integer;
|
||||
begin
|
||||
writeln('Calculating the arithmetic mean of a series of numbers:');
|
||||
write('Numbers: [ ');
|
||||
for i := low(vector) to high(vector) do
|
||||
write (vector[i]:3:1, ' ');
|
||||
writeln (']');
|
||||
writeln('Mean: ', DoMean(vector):10:8);
|
||||
end.
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
Program DoMean;
|
||||
uses math;
|
||||
const
|
||||
vector: array [3..8] of double = (3.0, 1.0, 4.0, 1.0, 5.0, 9.0);
|
||||
var
|
||||
i: integer;
|
||||
mean: double;
|
||||
begin
|
||||
writeln('Calculating the arithmetic mean of a series of numbers:');
|
||||
write('Numbers: [ ');
|
||||
for i := low(vector) to high(vector) do
|
||||
write (vector[i]:3:1, ' ');
|
||||
writeln (']');
|
||||
mean := 0;
|
||||
if length(vector) > 0 then
|
||||
mean := sum(vector)/length(vector);
|
||||
writeln('Mean: ', mean:10:8);
|
||||
end.
|
||||
|
|
@ -0,0 +1 @@
|
|||
sub mean (@a) { ([+] @a) / (@a || 1) }
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
define mean(v);
|
||||
lvars n = length(v), i, s = 0;
|
||||
if n = 0 then
|
||||
return(0);
|
||||
else
|
||||
for i from 1 to n do
|
||||
s + v(i) -> s;
|
||||
endfor;
|
||||
endif;
|
||||
return(s/n);
|
||||
enddefine;
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
/findmean{
|
||||
/x exch def
|
||||
/sum 0 def
|
||||
/i 0 def
|
||||
x length 0 eq
|
||||
{}
|
||||
{
|
||||
x length{
|
||||
/sum sum x i get add def
|
||||
/i i 1 add def
|
||||
}repeat
|
||||
/sum sum x length div def
|
||||
}ifelse
|
||||
sum ==
|
||||
}def
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
/avg {
|
||||
dup length
|
||||
{0 gt} {
|
||||
exch 0 {add} fold exch div
|
||||
} {
|
||||
exch pop
|
||||
} ifte
|
||||
}.
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
function mean ($x) {
|
||||
if ($x.Count -eq 0) {
|
||||
return 0
|
||||
} else {
|
||||
$sum = 0
|
||||
foreach ($i in $x) {
|
||||
$sum += $i
|
||||
}
|
||||
return $sum / $x.Count
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
function mean ($x) {
|
||||
if ($x.Count -eq 0) {
|
||||
return 0
|
||||
} else {
|
||||
return ($x | Measure-Object -Average).Average
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
Procedure.d mean(List number())
|
||||
Protected sum=0
|
||||
|
||||
ForEach number()
|
||||
sum + number()
|
||||
Next
|
||||
ProcedureReturn sum / ListSize(number())
|
||||
; Depends on programm if zero check needed, returns nan on division by zero
|
||||
EndProcedure
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
rebol [
|
||||
Title: "Arithmetic Mean (Average)"
|
||||
Author: oofoe
|
||||
Date: 2009-12-11
|
||||
URL: http://rosettacode.org/wiki/Average/Arithmetic_mean
|
||||
]
|
||||
|
||||
average: func [v /local sum][
|
||||
if empty? v [return 0]
|
||||
|
||||
sum: 0
|
||||
forall v [sum: sum + v/1]
|
||||
sum / length? v
|
||||
]
|
||||
|
||||
; Note precision loss as spread increased.
|
||||
|
||||
print [mold x: [] "->" average x]
|
||||
print [mold x: [3 1 4 1 5 9] "->" average x]
|
||||
print [mold x: [1000 3 1 4 1 5 9 -1000] "->" average x]
|
||||
print [mold x: [1e20 3 1 4 1 5 9 -1e20] "->" average x]
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
print "Gimme the number in the array:";input numArray
|
||||
dim value(numArray)
|
||||
for i = 1 to numArray
|
||||
value(i) = i * 1.5
|
||||
next
|
||||
|
||||
for i = 1 to total
|
||||
totValue = totValue +value(numArray)
|
||||
next
|
||||
if totValue <> 0 then mean = totValue/numArray
|
||||
print "The mean is: ";mean
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
define('avg(a)i,sum') :(avg_end)
|
||||
avg i = i + 1; sum = sum + a<i> :s(avg)
|
||||
avg = 1.0 * sum / prototype(a) :(return)
|
||||
avg_end
|
||||
|
||||
* # Fill arrays
|
||||
str = '1 2 3 4 5 6 7 8 9 10'; arr = array(10)
|
||||
loop i = i + 1; str len(p) span('0123456789') . arr<i> @p :s(loop)
|
||||
empty = array(1) ;* Null vector
|
||||
|
||||
* # Test and display
|
||||
output = '[' str '] -> ' avg(arr)
|
||||
output = '[ ] -> ' avg(empty)
|
||||
end
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
$ include "seed7_05.s7i";
|
||||
include "float.s7i";
|
||||
|
||||
const array float: numVector is [] (1.0, 2.0, 3.0, 4.0, 5.0);
|
||||
|
||||
const func float: mean (in array float: numbers) is func
|
||||
result
|
||||
var float: result is 0.0;
|
||||
local
|
||||
var float: total is 0.0;
|
||||
var float: num is 0.0;
|
||||
begin
|
||||
if length(numbers) <> 0 then
|
||||
for num range numbers do
|
||||
total +:= num;
|
||||
end for;
|
||||
result := total / flt(length(numbers));
|
||||
end if;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
begin
|
||||
writeln(mean(numVector));
|
||||
end func;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
[|:list| (list reduce: #+ `er ifEmpty: [0]) / (list isEmpty ifTrue: [1] ifFalse: [list size])] applyWith: #(3 1 4 1 5 9).
|
||||
[|:list| (list reduce: #+ `er ifEmpty: [0]) / (list isEmpty ifTrue: [1] ifFalse: [list size])] applyWith: {}.
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
fun mean_reals [] = 0.0
|
||||
| mean_reals xs = foldl op+ 0.0 xs / real (length xs);
|
||||
|
||||
val mean_ints = mean_reals o (map real);
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
fun mean_reals [] = raise Empty
|
||||
| mean_reals xs = let
|
||||
val (total, length) =
|
||||
foldl
|
||||
(fn (x, (tot,len)) => (x + tot, len + 1.0))
|
||||
(0.0, 0.0) xs
|
||||
in
|
||||
(total / length)
|
||||
end;
|
||||
|
||||
|
||||
fun mean_ints [] = raise Empty
|
||||
| mean_ints xs = let
|
||||
val (total, length) =
|
||||
foldl
|
||||
(fn (x, (tot,len)) => (x + tot, len + 1.0))
|
||||
(0, 0.0) xs
|
||||
in
|
||||
(real total / length)
|
||||
end;
|
||||
|
|
@ -0,0 +1 @@
|
|||
Define rcmean(nums) = when(dim(nums) = 0, 0, mean(nums))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
: mean dup empty? [drop 0] [dup [+] foldl1 swap length /] branch ;
|
||||
|
||||
[3 1 4 1 5 9] mean
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
mean() {
|
||||
if expr $# >/dev/null; then
|
||||
(count=0
|
||||
sum=0
|
||||
while expr $# \> 0 >/dev/null; do
|
||||
sum=`expr $sum + "$1"`
|
||||
result=$?
|
||||
expr $result \> 1 >/dev/null && exit $result
|
||||
|
||||
count=`expr $count + 1`
|
||||
shift
|
||||
done
|
||||
expr $sum / $count)
|
||||
else
|
||||
echo 0
|
||||
fi
|
||||
}
|
||||
|
||||
printf "test 1: "; mean # 0
|
||||
printf "test 2: "; mean 300 # 300
|
||||
printf "test 3: "; mean 300 100 400 # 266
|
||||
printf "test 4: "; mean -400 400 -1300 200 # -275
|
||||
printf "test 5: "; mean - # expr: syntax error
|
||||
printf "test 6: "; mean 1 2 A 3 # expr: non-numeric argument
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
term() {
|
||||
b=$1;res=$2
|
||||
echo "scale=5;$res+$b" | bc
|
||||
}
|
||||
|
||||
sum() {
|
||||
(read B; res=$1;
|
||||
test -n "$B" && (term $B $res) || (term 0 $res))
|
||||
}
|
||||
|
||||
fold() {
|
||||
func=$1
|
||||
(while read a ; do
|
||||
fold $func | $func $a
|
||||
done)
|
||||
}
|
||||
|
||||
mean() {
|
||||
tee >(wc -l > count) | fold sum | xargs echo "scale=5;(1/" $(cat count) ") * " | bc
|
||||
}
|
||||
|
||||
(echo 3; echo 1; echo 4) | mean
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
#import nat
|
||||
#import flo
|
||||
|
||||
mean = ~&?\0.! div^/plus:-0. float+ length
|
||||
|
||||
#cast %e
|
||||
|
||||
example = mean <5.,3.,-2.,6.,-4.>
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
[mean
|
||||
[sum 0 [+] fold].
|
||||
dup sum
|
||||
swap size [[1 <] [1]] when /
|
||||
].
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
double arithmetic(double[] list){
|
||||
double mean;
|
||||
double sum = 0;
|
||||
|
||||
if (list.length == 0)
|
||||
return 0.0;
|
||||
foreach(double number in list){
|
||||
sum += number;
|
||||
} // foreach
|
||||
|
||||
mean = sum / list.length;
|
||||
|
||||
return mean;
|
||||
} // end arithmetic mean
|
||||
|
||||
public static void main(){
|
||||
double[] test = {1.0, 2.0, 5.0, -5.0, 9.5, 3.14159};
|
||||
double[] zero_len = {};
|
||||
|
||||
double mean = arithmetic(test);
|
||||
double mean_zero = arithmetic(zero_len);
|
||||
|
||||
stdout.printf("%s\n", mean.to_string());
|
||||
stdout.printf("%s\n", mean_zero.to_string());
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
#1 = 0 // Sum
|
||||
#2 = 0 // Count
|
||||
BOF
|
||||
While(!At_EOF) {
|
||||
#1 += Num_Eval(SIMPLE)
|
||||
#2++
|
||||
Line(1, ERRBREAK)
|
||||
}
|
||||
if (#2) { #1 /= #2 }
|
||||
Num_Type(#1)
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
code CrLf=9;
|
||||
code real RlOut=48;
|
||||
|
||||
func real Mean(A, N);
|
||||
real A; int N;
|
||||
real S; int I;
|
||||
[if N=0 then return 0.0;
|
||||
S:= 0.0;
|
||||
for I:= 0 to N-1 do
|
||||
S:= S+A(I);
|
||||
return S/float(N);
|
||||
]; \Mean
|
||||
|
||||
real Test;
|
||||
[Test:= [1.0, 2.0, 5.0, -5.0, 9.5, 3.14159];
|
||||
RlOut(0, Mean(Test, 6)); CrLf(0);
|
||||
]
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
func mean(x) {
|
||||
if(is_void(x)) return 0;
|
||||
return x(*)(avg);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue