Skip to content

Commit fb1b939

Browse files
CaedenPHgithub-actions
and
github-actions
authored
Consolidate find_min and find_min recursive and find_max and find_max_recursive (TheAlgorithms#8960)
* updating DIRECTORY.md * refactor(min-max): Consolidate implementations * updating DIRECTORY.md * refactor(min-max): Append _iterative to func name --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 2ab3bf2 commit fb1b939

File tree

5 files changed

+118
-130
lines changed

5 files changed

+118
-130
lines changed

Diff for: DIRECTORY.md

-2
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,7 @@
573573
* [Fermat Little Theorem](maths/fermat_little_theorem.py)
574574
* [Fibonacci](maths/fibonacci.py)
575575
* [Find Max](maths/find_max.py)
576-
* [Find Max Recursion](maths/find_max_recursion.py)
577576
* [Find Min](maths/find_min.py)
578-
* [Find Min Recursion](maths/find_min_recursion.py)
579577
* [Floor](maths/floor.py)
580578
* [Gamma](maths/gamma.py)
581579
* [Gamma Recursive](maths/gamma_recursive.py)

Diff for: maths/find_max.py

+59-6
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,83 @@
11
from __future__ import annotations
22

33

4-
def find_max(nums: list[int | float]) -> int | float:
4+
def find_max_iterative(nums: list[int | float]) -> int | float:
55
"""
66
>>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]):
7-
... find_max(nums) == max(nums)
7+
... find_max_iterative(nums) == max(nums)
88
True
99
True
1010
True
1111
True
12-
>>> find_max([2, 4, 9, 7, 19, 94, 5])
12+
>>> find_max_iterative([2, 4, 9, 7, 19, 94, 5])
1313
94
14-
>>> find_max([])
14+
>>> find_max_iterative([])
1515
Traceback (most recent call last):
1616
...
17-
ValueError: find_max() arg is an empty sequence
17+
ValueError: find_max_iterative() arg is an empty sequence
1818
"""
1919
if len(nums) == 0:
20-
raise ValueError("find_max() arg is an empty sequence")
20+
raise ValueError("find_max_iterative() arg is an empty sequence")
2121
max_num = nums[0]
2222
for x in nums:
2323
if x > max_num:
2424
max_num = x
2525
return max_num
2626

2727

28+
# Divide and Conquer algorithm
29+
def find_max_recursive(nums: list[int | float], left: int, right: int) -> int | float:
30+
"""
31+
find max value in list
32+
:param nums: contains elements
33+
:param left: index of first element
34+
:param right: index of last element
35+
:return: max in nums
36+
37+
>>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]):
38+
... find_max_recursive(nums, 0, len(nums) - 1) == max(nums)
39+
True
40+
True
41+
True
42+
True
43+
>>> nums = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
44+
>>> find_max_recursive(nums, 0, len(nums) - 1) == max(nums)
45+
True
46+
>>> find_max_recursive([], 0, 0)
47+
Traceback (most recent call last):
48+
...
49+
ValueError: find_max_recursive() arg is an empty sequence
50+
>>> find_max_recursive(nums, 0, len(nums)) == max(nums)
51+
Traceback (most recent call last):
52+
...
53+
IndexError: list index out of range
54+
>>> find_max_recursive(nums, -len(nums), -1) == max(nums)
55+
True
56+
>>> find_max_recursive(nums, -len(nums) - 1, -1) == max(nums)
57+
Traceback (most recent call last):
58+
...
59+
IndexError: list index out of range
60+
"""
61+
if len(nums) == 0:
62+
raise ValueError("find_max_recursive() arg is an empty sequence")
63+
if (
64+
left >= len(nums)
65+
or left < -len(nums)
66+
or right >= len(nums)
67+
or right < -len(nums)
68+
):
69+
raise IndexError("list index out of range")
70+
if left == right:
71+
return nums[left]
72+
mid = (left + right) >> 1 # the middle
73+
left_max = find_max_recursive(nums, left, mid) # find max in range[left, mid]
74+
right_max = find_max_recursive(
75+
nums, mid + 1, right
76+
) # find max in range[mid + 1, right]
77+
78+
return left_max if left_max >= right_max else right_max
79+
80+
2881
if __name__ == "__main__":
2982
import doctest
3083

Diff for: maths/find_max_recursion.py

-58
This file was deleted.

Diff for: maths/find_min.py

+59-6
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,86 @@
11
from __future__ import annotations
22

33

4-
def find_min(nums: list[int | float]) -> int | float:
4+
def find_min_iterative(nums: list[int | float]) -> int | float:
55
"""
66
Find Minimum Number in a List
77
:param nums: contains elements
88
:return: min number in list
99
1010
>>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]):
11-
... find_min(nums) == min(nums)
11+
... find_min_iterative(nums) == min(nums)
1212
True
1313
True
1414
True
1515
True
16-
>>> find_min([0, 1, 2, 3, 4, 5, -3, 24, -56])
16+
>>> find_min_iterative([0, 1, 2, 3, 4, 5, -3, 24, -56])
1717
-56
18-
>>> find_min([])
18+
>>> find_min_iterative([])
1919
Traceback (most recent call last):
2020
...
21-
ValueError: find_min() arg is an empty sequence
21+
ValueError: find_min_iterative() arg is an empty sequence
2222
"""
2323
if len(nums) == 0:
24-
raise ValueError("find_min() arg is an empty sequence")
24+
raise ValueError("find_min_iterative() arg is an empty sequence")
2525
min_num = nums[0]
2626
for num in nums:
2727
min_num = min(min_num, num)
2828
return min_num
2929

3030

31+
# Divide and Conquer algorithm
32+
def find_min_recursive(nums: list[int | float], left: int, right: int) -> int | float:
33+
"""
34+
find min value in list
35+
:param nums: contains elements
36+
:param left: index of first element
37+
:param right: index of last element
38+
:return: min in nums
39+
40+
>>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]):
41+
... find_min_recursive(nums, 0, len(nums) - 1) == min(nums)
42+
True
43+
True
44+
True
45+
True
46+
>>> nums = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
47+
>>> find_min_recursive(nums, 0, len(nums) - 1) == min(nums)
48+
True
49+
>>> find_min_recursive([], 0, 0)
50+
Traceback (most recent call last):
51+
...
52+
ValueError: find_min_recursive() arg is an empty sequence
53+
>>> find_min_recursive(nums, 0, len(nums)) == min(nums)
54+
Traceback (most recent call last):
55+
...
56+
IndexError: list index out of range
57+
>>> find_min_recursive(nums, -len(nums), -1) == min(nums)
58+
True
59+
>>> find_min_recursive(nums, -len(nums) - 1, -1) == min(nums)
60+
Traceback (most recent call last):
61+
...
62+
IndexError: list index out of range
63+
"""
64+
if len(nums) == 0:
65+
raise ValueError("find_min_recursive() arg is an empty sequence")
66+
if (
67+
left >= len(nums)
68+
or left < -len(nums)
69+
or right >= len(nums)
70+
or right < -len(nums)
71+
):
72+
raise IndexError("list index out of range")
73+
if left == right:
74+
return nums[left]
75+
mid = (left + right) >> 1 # the middle
76+
left_min = find_min_recursive(nums, left, mid) # find min in range[left, mid]
77+
right_min = find_min_recursive(
78+
nums, mid + 1, right
79+
) # find min in range[mid + 1, right]
80+
81+
return left_min if left_min <= right_min else right_min
82+
83+
3184
if __name__ == "__main__":
3285
import doctest
3386

Diff for: maths/find_min_recursion.py

-58
This file was deleted.

0 commit comments

Comments
 (0)