Skip to content

Commit

Permalink
functions impoved
Browse files Browse the repository at this point in the history
  • Loading branch information
Madhav2310 committed Jul 15, 2022
1 parent 1bae84c commit 13c5291
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
6 changes: 0 additions & 6 deletions integration_tests/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
eps = 1e-12

def test_mean():
a: list[i32]
a = [7, 4, 19]
i: i32
i = mean(a)
assert abs(i - 10) < eps

b: list[i32]
b = [9, 4, 10]
j: f64
Expand Down
24 changes: 8 additions & 16 deletions src/runtime/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@ def mean(x: list[i32]) -> f64:
sum += x[i]
ans: f64
ans = sum/k
return float(ans)

@overload
def mean(x: list[i32]) -> i32:
k: i32 = len(x)
if k == 0:
return 0
sum: i32
sum = 0
i: i32

for i in range(k):
sum += x[i]
ans: i32
ans = sum/k
return ans

@overload
Expand All @@ -53,6 +38,8 @@ def geometric_mean(x: list[i32]) -> f64:
product = 1.0
i: i32
for i in range(k):
if x[i] < 1:
raise ValueError('geometric mean requires a non-empty dataset containing positive numbers')
product *= x[i]
ans: f64
ans = product**(1/k)
Expand All @@ -65,8 +52,13 @@ def harmonic_mean(x: list[i32]) -> f64:
return 0.0
sum: f64
sum = 0.0
i: i32
for i in range(k):
if x[i] < 0:
raise ValueError('harmonic mean does not support negative values')
if x[i] ==0:
return 0.0
sum += 1 / x[i]
ans: f64
ans = k/sum
return float(ans)
return ans

0 comments on commit 13c5291

Please sign in to comment.