Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding average mean algorithm #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions maths/average_mean.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## Average mean

fn mean(nums: List[Int]) raises -> Float64:
"""
Find mean of a list of numbers.
Wiki: https://en.wikipedia.org/wiki/Mean.

Parameters:
- nums: A list of integers.

Returns:
- The mean of the list of numbers.

```mojo
from testing import assert_almost_equal, assert_raises
from average_mean import mean
DELTA = 1e-6
assert_almost_equal(mean(List(5, 10, 15, 20, 25, 30, 35)), 20.0, atol=DELTA)
assert_almost_equal(mean(List(1, 2, 3, 4, 5, 6, 7, 8)), 4.5, atol=DELTA)
assert_almost_equal(mean(List(3, 6, 9, 12, 15, 18, 21)), 12.0, atol=DELTA)
with assert_raises():
var empty = List[Int]()
_ = mean(empty)
```
"""
if len(nums) == 0:
raise Error("List is empty")

var list_sum = 0.0
for num in nums:
list_sum += num[]

return list_sum / len(nums)