I have a piece of code and want them to find max sum for float and complex number too. however i am getting error
```lang-python
def max_subarray_sum(nums: list) -> complex:
if not nums:
return 0
max_sum = current_sum = nums[0]
for num in nums[1:]:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum
print(max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4])) # 6
print(max_subarray_sum([1])) # 1
print(max_subarray_sum([5, 4, -1, 7, 8])) # 23
print(max_subarray_sum([-1, -2, -3, -4])) # -1 (the largest single element)
print(max_subarray_sum([1, 2, 3, 4, 5])) # 15 (the entire array)`
```
I tried to create a array with float and complex number however its value did not take up.
what are the other way to find sum max and min in one dimensional array.
Rephrasing my question on same line:
def max_subarray_sum(nums: list) -> complex:
x = cmath.sqrt(2)
y = cmath.sqrt(3)
print(x, y, type(x), type(y)) # Output: (2+0j) (3+0j)
max_sum = current_sum = nums[0]
for num in nums[1:]:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum
print(max_subarray_sum([x, y])) # Output: (5+0j)
Error which i got is "
< /code>
typeerror traceback (последний вызов последний)
cell в [77], строка 11
8 max_sum = max (max_sum, current_sum)
9 возвращает max_sum
---> 11 print (max_subarray_sum (x, y]) #) #) #) #). 'комплекс'> < /p>
Cell In[77], line 7
5 max_sum = current_sum = nums[0]
6 for num in nums[1:]:
----> 7 current_sum = max(num, current_sum + num)
8 max_sum = max(max_sum, current_sum)
9 return max_sum
TypeError: '>' not supported between instances of 'complex' and 'float'
Подробнее здесь: https://stackoverflow.com/questions/796 ... ver-can-we
В одномерном числовом массиве мы можем получить наибольшее значение суммы. Однако мы можем получить его для различного т ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение