Skip to content

Commit 8e21b67

Browse files
committed
added misc.integer.get_local_maxima with tests
1 parent aaf1c6d commit 8e21b67

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

ml/misc/integer.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,35 @@
33
"""
44

55

6+
def get_local_maxima(inputs: list) -> list:
7+
'''
8+
Find all local maxima from the input list of integers.
9+
10+
Examples:
11+
- inputs: [11, 5, 10, 6, 4, 7, 4, 10, 1]
12+
output: [11, 10, 7, 10]
13+
- inputs: [1, 2, 1]
14+
output; [2]
15+
- inputs: [1, 2, 3, 4, 3]
16+
output: [4]
17+
- inputs: [1, 2, 3, 4, 4, 4, 4]
18+
output: [4]
19+
- inputs: [1, 4, 3, 4]
20+
output: [4, 4]
21+
'''
22+
prev = None
23+
results = []
24+
siz = len(inputs)
25+
for idx, v in enumerate(inputs):
26+
# larger than both neighbors, or larger than left and at the end
27+
larger_than_prev = prev is None or v > prev # always True for the 1st item
28+
larger_than_next = v >= inputs[idx+1] if idx+1 < siz else False
29+
if larger_than_prev and (larger_than_next or idx == siz - 1):
30+
results.append(v)
31+
prev = v
32+
return results
33+
34+
635
def get_perfect_squares(num: int) -> list:
736
"""
837
Get a mininum number of squares sum up to specific integer num.

tests/test_misc_integer.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"""
44
import logging
55
import unittest
6+
import sys
67

8+
from ml.misc.integer import get_local_maxima
79
from ml.misc.integer import get_perfect_squares
810

911

@@ -24,6 +26,64 @@ def tearDown(self):
2426
"""tearing down at the end of the test"""
2527
pass
2628

29+
def test_get_local_maxima(self):
30+
tests = [{
31+
"input": [-1],
32+
"output": [-1]
33+
}, {
34+
"input": [0],
35+
"output": [0]
36+
}, {
37+
"input": [-sys.maxsize],
38+
"output": [-sys.maxsize]
39+
}, {
40+
"input": [sys.maxsize],
41+
"output": [sys.maxsize]
42+
}, {
43+
"input": [3, sys.maxsize, 0],
44+
"output": [sys.maxsize]
45+
}, {
46+
"input": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
47+
"output": [0]
48+
}, {
49+
"input": [7, 7, 7, 7, 7, 7, 7],
50+
"output": [7]
51+
}, {
52+
"input": [1, 2, 1],
53+
"output": [2]
54+
}, {
55+
"input": [-55, -2, -5],
56+
"output": [-2]
57+
}, {
58+
"input": [-5, 2],
59+
"output": [2]
60+
}, {
61+
"input": [5, 2],
62+
"output": [5]
63+
}, {
64+
"input": [-1, 0, 2, 100, 10000, 123456],
65+
"output": [123456]
66+
}, {
67+
"input": [3, 2, 1],
68+
"output": [3]
69+
}, {
70+
"input": [1, 2, 3],
71+
"output": [3]
72+
}, {
73+
"input": [1, 2, 1],
74+
"output": [2]
75+
}, {
76+
"input": [1, 2, 3, 4, 3],
77+
"output": [4]
78+
}, {
79+
"input": [1, 2, 3, 4, 4, 4, 2, 5],
80+
"output": [4, 5]
81+
}]
82+
for test in tests:
83+
expected = test["output"]
84+
result = get_local_maxima(test["input"])
85+
self.assertListEqual(result, expected)
86+
2787
def test_get_perfect_squares(self):
2888
import random
2989
tests = [random.randint(1, 65535) for i in range(0, 100)]

0 commit comments

Comments
 (0)