Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
23
Task/Averages-Arithmetic-mean/D/averages-arithmetic-mean-1.d
Normal file
23
Task/Averages-Arithmetic-mean/D/averages-arithmetic-mean-1.d
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
real mean(Range)(Range r) pure nothrow @nogc {
|
||||
real sum = 0.0;
|
||||
int count;
|
||||
|
||||
foreach (item; r) {
|
||||
sum += item;
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count == 0)
|
||||
return 0.0;
|
||||
else
|
||||
return sum / count;
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio;
|
||||
|
||||
int[] data;
|
||||
writeln("Mean: ", data.mean);
|
||||
data = [3, 1, 4, 1, 5, 9];
|
||||
writeln("Mean: ", data.mean);
|
||||
}
|
||||
10
Task/Averages-Arithmetic-mean/D/averages-arithmetic-mean-2.d
Normal file
10
Task/Averages-Arithmetic-mean/D/averages-arithmetic-mean-2.d
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
real mean(Range)(Range r) pure nothrow @nogc {
|
||||
return r.sum / max(1.0L, r.count);
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln("Mean: ", (int[]).init.mean);
|
||||
writeln("Mean: ", [3, 1, 4, 1, 5, 9].mean);
|
||||
}
|
||||
14
Task/Averages-Arithmetic-mean/D/averages-arithmetic-mean-3.d
Normal file
14
Task/Averages-Arithmetic-mean/D/averages-arithmetic-mean-3.d
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import std.stdio, std.conv, std.algorithm, std.math, std.traits;
|
||||
|
||||
CommonType!(T, real) mean(T)(T[] n ...) if (isNumeric!T) {
|
||||
alias E = CommonType!(T, real);
|
||||
auto num = n.dup;
|
||||
num.schwartzSort!(abs, "a > b");
|
||||
return num.map!(to!E).sum(0.0L) / max(1, num.length);
|
||||
}
|
||||
|
||||
void main() {
|
||||
writefln("%8.5f", mean((int[]).init));
|
||||
writefln("%8.5f", mean( 0, 3, 1, 4, 1, 5, 9, 0));
|
||||
writefln("%8.5f", mean([-1e20, 3, 1, 4, 1, 5, 9, 1e20]));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue