Skip to content

Commit 7a8079f

Browse files
seeman0306pre-commit-ci[bot]cclauss
authored
Improve documentation and educational comments in find_max.py (#13017)
* 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 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 4b9a069 commit 7a8079f

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

‎maths/find_max.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ def find_max_iterative(nums: list[int | float]) -> int | float:
2626

2727

2828
# Divide and Conquer algorithm
29+
# Using Divide and Conquer approach:
30+
# 1. Divide the array into two halves
31+
# 2. Recursively find the maximum in each half
32+
# 3. Combine the results by comparing the two maximums
2933
def find_max_recursive(nums: list[int | float], left: int, right: int) -> int | float:
3034
"""
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
35+
Find the maximum value in a list using Divide and Conquer approach
36+
:param nums: list of numbers to search through
37+
:param left: starting index of the current subarray (inclusive)
38+
:param right: ending index of the current subarray (inclusive)
39+
:return: maximum value found in the subarray nums[left:right+1]
3640
3741
>>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]):
3842
... find_max_recursive(nums, 0, len(nums) - 1) == max(nums)
@@ -69,7 +73,7 @@ def find_max_recursive(nums: list[int | float], left: int, right: int) -> int |
6973
raise IndexError("list index out of range")
7074
if left == right:
7175
return nums[left]
72-
mid = (left + right) >> 1 # the middle
76+
mid = (left + right) >> 1 # Equivalent to (left + right) // 2 - finds middle index
7377
left_max = find_max_recursive(nums, left, mid) # find max in range[left, mid]
7478
right_max = find_max_recursive(
7579
nums, mid + 1, right

0 commit comments

Comments
 (0)