>>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]): ... find_max_iterative(nums) == max(nums) True True True True >>> find_max_iterative([2, 4, 9, 7, 19, 94, 5]) 94 >>> find_max_iterative([]) Traceback (most recent call last):
(nums: list[int | float])
| 2 | |
| 3 | |
| 4 | def find_max_iterative(nums: list[int | float]) -> int | float: |
| 5 | """ |
| 6 | >>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]): |
| 7 | ... find_max_iterative(nums) == max(nums) |
| 8 | True |
| 9 | True |
| 10 | True |
| 11 | True |
| 12 | >>> find_max_iterative([2, 4, 9, 7, 19, 94, 5]) |
| 13 | 94 |
| 14 | >>> find_max_iterative([]) |
| 15 | Traceback (most recent call last): |
| 16 | ... |
| 17 | ValueError: find_max_iterative() arg is an empty sequence |
| 18 | """ |
| 19 | if len(nums) == 0: |
| 20 | raise ValueError("find_max_iterative() arg is an empty sequence") |
| 21 | max_num = nums[0] |
| 22 | for x in nums: |
| 23 | if x > max_num: # noqa: PLR1730 |
| 24 | max_num = x |
| 25 | return max_num |
| 26 | |
| 27 | |
| 28 | # Divide and Conquer algorithm |
nothing calls this directly
no outgoing calls
no test coverage detected