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");
|
||||
}
|
||||
}
|
||||
|
|
@ -1,24 +1,18 @@
|
|||
global list[] .
|
||||
proc mklist n . .
|
||||
proc mklist n .
|
||||
list[] = [ ]
|
||||
for i = 1 to n
|
||||
list[] &= randomf
|
||||
.
|
||||
for i = 1 to n : list[] &= randomf
|
||||
.
|
||||
func mean .
|
||||
for v in list[]
|
||||
sum += v
|
||||
.
|
||||
for v in list[] : sum += v
|
||||
return sum / len list[]
|
||||
.
|
||||
func stddev .
|
||||
avg = mean
|
||||
for v in list[]
|
||||
squares += (avg - v) * (avg - v)
|
||||
.
|
||||
for v in list[] : squares += (avg - v) * (avg - v)
|
||||
return sqrt (squares / len list[])
|
||||
.
|
||||
proc histo . .
|
||||
proc histo .
|
||||
len hist[] 10
|
||||
for v in list[]
|
||||
ind = floor (v * 10) + 1
|
||||
|
|
@ -30,8 +24,8 @@ proc histo . .
|
|||
print v & " " & s$
|
||||
.
|
||||
.
|
||||
numfmt 4 5
|
||||
proc stats size . .
|
||||
numfmt 5 4
|
||||
proc stats size .
|
||||
mklist size
|
||||
print "Size: " & size
|
||||
print "Mean: " & mean
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue