Data update
This commit is contained in:
parent
72eb4943cb
commit
4d5544505c
2347 changed files with 62432 additions and 16731 deletions
44
Task/Statistics-Basic/Ballerina/statistics-basic.ballerina
Normal file
44
Task/Statistics-Basic/Ballerina/statistics-basic.ballerina
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import ballerina/io;
|
||||
import ballerina/random;
|
||||
|
||||
function mean(float[] a) returns float {
|
||||
return float:sum(...a) / a.length();
|
||||
}
|
||||
|
||||
function popStdDev(float[] a) returns float {
|
||||
float m = mean(a);
|
||||
var sumSq = function(float total, float next) returns float {
|
||||
return total + next * next;
|
||||
};
|
||||
return (a.reduce(sumSq, 0.0) / a.length() - m * m).sqrt();
|
||||
}
|
||||
|
||||
function repeat(string s, int times) returns string {
|
||||
string r = "";
|
||||
foreach int i in 1...times { r += s; }
|
||||
return r;
|
||||
}
|
||||
|
||||
public function main() {
|
||||
foreach int i in [100, 1000, 10000] {
|
||||
float[] a = [];
|
||||
a.setLength(i);
|
||||
foreach int j in 0..<i { a[j] = random:createDecimal(); }
|
||||
io:println(`For ${i} random numbers:`);
|
||||
io:println(" mean = ", mean(a));
|
||||
io:println(" std/dev = ", popStdDev(a));
|
||||
int scale = i / 100;
|
||||
io:println(" scale = ", scale, " per asterisk");
|
||||
int[10] sums = [];
|
||||
foreach float e in a {
|
||||
int f = <int>(e*10).floor();
|
||||
sums[f] += 1;
|
||||
}
|
||||
foreach int j in 0...8 {
|
||||
sums[j] = <int>((<float>sums[j] / scale).round());
|
||||
io:println(` 0.${j} - 0.${j+1}: `, repeat("*", sums[j]));
|
||||
}
|
||||
sums[9] = 100 - int:sum(...sums.slice(0, 9));
|
||||
io:println(" 0.9 - 1.0: ", repeat("*", sums[9]), "\n");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue