forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit.py
More file actions
44 lines (33 loc) · 1.02 KB
/
limit.py
File metadata and controls
44 lines (33 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Limit Array Values
Filter an array to include only elements within a specified minimum and
maximum range (inclusive).
Reference: https://en.wikipedia.org/wiki/Clipping_(signal_processing)
Complexity:
Time: O(n)
Space: O(n)
"""
from __future__ import annotations
def limit(
array: list[int],
min_lim: int | None = None,
max_lim: int | None = None,
) -> list[int]:
"""Return elements of array that fall within [min_lim, max_lim].
Args:
array: Source list of integers.
min_lim: Minimum value (inclusive). Defaults to the array minimum.
max_lim: Maximum value (inclusive). Defaults to the array maximum.
Returns:
A new list containing only values within the specified range.
Examples:
>>> limit([1, 2, 3, 4, 5], 2, 4)
[2, 3, 4]
"""
if len(array) == 0:
return array
if min_lim is None:
min_lim = min(array)
if max_lim is None:
max_lim = max(array)
return list(filter(lambda x: min_lim <= x <= max_lim, array))