Skip to content

Commit 658a1fe

Browse files
authored
Improve documentation and educational comments in find_max.py
- Added clear explanation of Divide and Conquer approach - Improved parameter descriptions for better understanding - Enhanced comments to explain the algorithm steps - Better documentation for educational purposes
1 parent a71618f commit 658a1fe

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

maths/find_max.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
from __future__ import annotations
2-
3-
42
def find_max_iterative(nums: list[int | float]) -> int | float:
53
"""
64
>>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]):
@@ -24,15 +22,19 @@ def find_max_iterative(nums: list[int | float]) -> int | float:
2422
max_num = x
2523
return max_num
2624

27-
25+
2826
# Divide and Conquer algorithm
27+
# Using Divide and Conquer approach:
28+
# 1. Divide the array into two halves
29+
# 2. Recursively find the maximum in each half
30+
# 3. Combine the results by comparing the two maximums
2931
def find_max_recursive(nums: list[int | float], left: int, right: int) -> int | float:
3032
"""
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
33+
Find the maximum value in a list using Divide and Conquer approach
34+
:param nums: list of numbers to search through
35+
:param left: starting index of the current subarray (inclusive)
36+
:param right: ending index of the current subarray (inclusive)
37+
:return: maximum value found in the subarray nums[left:right+1]
3638
3739
>>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]):
3840
... find_max_recursive(nums, 0, len(nums) - 1) == max(nums)
@@ -69,7 +71,7 @@ def find_max_recursive(nums: list[int | float], left: int, right: int) -> int |
6971
raise IndexError("list index out of range")
7072
if left == right:
7173
return nums[left]
72-
mid = (left + right) >> 1 # the middle
74+
mid = (left + right) >> 1 # Equivalent to (left + right) // 2 - finds middle index
7375
left_max = find_max_recursive(nums, left, mid) # find max in range[left, mid]
7476
right_max = find_max_recursive(
7577
nums, mid + 1, right

0 commit comments

Comments
 (0)