Skip to content

Commit c96063b

Browse files
authored
Overloaded mode function to support i64 (#1558)
Added a test.
1 parent fe07cbc commit c96063b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

integration_tests/test_statistics.py

+5
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ def test_mode():
213213
k = mode(d)
214214
assert k == -3
215215

216+
e: list[i64]
217+
e = [i64(-1), i64(2), i64(-3), i64(-5), i64(-3), i64(-1), i64(4), i64(-2), i64(4), i64(-5), i64(-3), i64(4), i64(-3)]
218+
l: i64 = mode(e)
219+
assert l == i64(-3)
220+
216221

217222
def check():
218223
test_mean()

src/runtime/statistics.py

+24
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def harmonic_mean(x: list[f64]) -> f64:
222222

223223

224224
# TODO: Use generics to support other types.
225+
@overload
225226
def mode(x: list[i32]) -> i32:
226227
k: i32 = len(x)
227228
c: i32
@@ -243,6 +244,29 @@ def mode(x: list[i32]) -> i32:
243244
ans = x[c]
244245
return ans
245246

247+
@overload
248+
def mode(x: list[i64]) -> i64:
249+
k: i32 = len(x)
250+
c: i32
251+
count: dict[i64, i32] = {i64(0): 0}
252+
253+
# insert keys in the dictionary
254+
for c in range(k):
255+
count[x[c]] = 0
256+
257+
# update the frequencies
258+
for c in range(k):
259+
count[x[c]] = count[x[c]] + 1
260+
261+
max_count: i32 = 0
262+
ans: i64
263+
for c in range(k):
264+
if max_count < count[x[c]]:
265+
max_count = count[x[c]]
266+
ans = x[c]
267+
return ans
268+
269+
246270
@overload
247271
def variance(x: list[f64]) -> f64:
248272
"""

0 commit comments

Comments
 (0)