8000 add limit algorithm · dori-dev/python-algorithms@c3fd744 · GitHub
[go: up one dir, main page]

Skip to content

Commit c3fd744

Browse files
committed
add limit algorithm
1 parent 541b6fa commit c3fd744

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

arrays/limit.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
limit([1, 2, 3, 4, 5], None, 3) = [1, 2, 3]
3+
complexity = O(n)
4+
"""
5+
6+
7+
def min_check(value: int, min_limit: int) -> bool:
8+
if min_limit is None:
9+
return True
10+
return min_limit <= value
11+
12+
13+
def max_check(value: int, max_limit: int) -> bool:
14+
if max_limit is None:
15+
return True
16+
return value <= max_limit
17+
18+
19+
def limit(array: list, min_limit: int = None, max_limit: int = None) -> list:
20+
result = []
21+
for value in array:
22+
if min_check(value, min_limit) and max_check(value, max_limit):
23+
result.append(value)
24+
return result
25+
26+
27+
print(limit([1, 2, 3, 4, 5], None, 3))

0 commit comments

Comments
 (0)
0