User talk:Onewikibarnes
Empty subarrays
[edit]Please stop making your bad edits to maximum subarray problem before this escalates beyond that article.
As the article clearly states, that version of the algorithm allows empty subarrays, and an empty subarray has a zero sum. That means that there is always a subarray whose sum is zero: the empty subarray. There is no reason for the algorithm to consider values less than zero, because the empty subarray will always win. Changing the starting value to something less than zero, as you have done, is a bad change, because the empty subarray has a better value. Changing max(0,current+x) to max(x,current+x) is a bad change, because it only makes a difference when x is less than zero, and the empty subarray has a better value. Your edits are breaking that behavior, which is the intended behavior. It is not a bug.
—David Eppstein (talk) 04:33, 1 April 2023 (UTC)
- Well at least your using context now. Programmatically, your explanation doesn't make sense, and this is a code program on Wikipedia, hence, it should be programmatically correct. An empty subarray does NOT have a sum of zero. It's empty. NULL (or None as the case in Python) doesn't equal zero. Furthermore, this concept of "... the empty subarray has a better value." is specious reasoning at best. What is a better value? In addition, the solution is still broken for arrays of non-positive values. Given that the first example that was put forth has negative values you'd have to concede that the real possibility of a completely negative (or non-positive) array as an option. Is an array of non-positive integers not a legitimate numeric array? The explanation is very odd as you say that the code used will give zero (which is incorrect) in a case of all negative integers.
- I'm glad you're finally engaging in the argument and not just being condescending, but the argument doesn't hold. I will keep reverting back the change as it is incorrect and makes Wikipedia look bad. People will go to wikipedia copy this code and use it finding it wrong. If this escalates, so be it. I think people would like to have the code correct, and not some odd reasoning to justify why it should stay incorrect.
- Here's code to test in Jupyter if you care.
- numbers1 = [6, 7, -1, -1, -2, 4, 1]
- numbers2 = [-8, 5, 9, 0, 1, -6, 7, -2]
- numbers3 = [-1, -2, -4, -3]
- numbers4 = [-10, -4, -8, -2, -4, -3]
- numbers5 = [ 0, -9, -8, -1]
- numbers6 = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
- numbers7 = [-7, -4, -1, 2, -5]
- numbers8 = [-7, -4, -1, 2, -1, 2]
- numbers9 = []
- def max_subarray1(numbers):
- """Find the largest sum of any contiguous subarray."""
- if not numbers:
- """Return nothing when an empty array is passed."""
- return None
- """Setting initial values to max negative in Python (-2 ** 31)"""
- best_sum = (-2 ** 31)
- current_sum = (-2 ** 31)
- for x in numbers:
- current_sum = max(x, current_sum + x)
- best_sum = max(best_sum, current_sum)
- return best_sum
- print(-2 ** 31)
- print("My solution.")
- print(max_subarray1(numbers1))
- print(max_subarray1(numbers2))
- print(max_subarray1(numbers3))
- print(max_subarray1(numbers4))
- print(max_subarray1(numbers5))
- print(max_subarray1(numbers6))
- print(max_subarray1(numbers7))
- print(max_subarray1(numbers8))
- print(max_subarray1(numbers9))
- def max_subarray2(numbers):
- """Find the largest sum of any contiguous subarray."""
- best_sum = 0
- current_sum = 0
- for x in numbers:
- current_sum = max(0, current_sum + x)
- best_sum = max(best_sum, current_sum)
- return best_sum
- print("Their solution.")
- print(max_subarray2(numbers1))
- print(max_subarray2(numbers2))
- print(max_subarray2(numbers3))
- print(max_subarray2(numbers4))
- print(max_subarray2(numbers5))
- print(max_subarray2(numbers6))
- print(max_subarray2(numbers7))
- print(max_subarray2(numbers8))
- print(max_subarray2(numbers9)) Onewikibarnes (talk) 16:21, 4 April 2023 (UTC)