Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions math/mean.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn Mean(slice: []int): f64 {
let l = len(slice)
if l == 0 {
return 0
}

let mut total: int = 0
for _, n in slice {
total += n
}

return f64(total) / f64(l)
}
10 changes: 10 additions & 0 deletions math/mean_test.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#build test

use "std/testing"

#test
fn testMean(t: &testing::T) {
t.Assert(Mean([2, 4, 6, 8]) == 5, "mean should be 5")
t.Assert(Mean([-3, 0, 3]) == 0, "mean should be 0")
t.Assert(Mean([]) == 0, "mean of empty slice should be 0")
}