title: Can print statement change logic of the algorithm?
body: <p>I am trying to solve the maximum contagious subarray problem where the goal is to find the maximum sum of any possible subsequence of the list. I devised a collapsing algorithm, where it adds up the same signed neighbors first to get a sequence of alternating sign numbers. Next, I defined a collapse function which sums the sequence if the sum is greater than either of the individual numbers.
  Here is the weird problem I'm facing. If I exclude the print statement as first line of <code>def collapse(list)</code> function, the program throws me an error. The program is as follows</p>
  <pre class="lang-py prettyprint-override"><code>a=[5,4,-1,7,8]
  lena,i= len(a)-1,1
  while i &lt;= lena:
      if a[i]*a[i-1] &gt;= 0:
          a[i-1]=a[i]+a[i-1]
          a.pop(i)
          i -= 1
          lena=len(a)-1
      i+=1
  print(a)
  def collapse(list_):
      #print(list_)
      list_copy = list_
      if list_[0] &gt; 0:
          j = 0
      else:
          j = 1
      if len(list_)&lt;=3:
          list_=max(max((i for i in list_)),sum(list_))
      else:
          for i in range(j + 2, len(list_) - 1, 2):
              if (sum(list_[i - 2:i + 1]) &gt;= max(list_[i - 2], list_[i])):
                  list_[i - 2] = sum(list_[i - 2:i + 1])
                  list_.pop(i)
                  list_.pop(i - 1)
                  i -= 2
      if list_copy == list_:
          return list_
      else:
          collapse(list_)
  
  print(max(collapse(a)))
  </code></pre>
  <p>The error thrown is <code>TypeError: 'int' object is not subscriptable.  However, if I run the same code by uncommenting the print statement in the function definition as </code>def collapse(list):  print(list)`, it works and prints me the correct solution. But parallelly it still shows the same error. I don't understand how can a simple print statement possibly affect my program flow.</p>
  <p>The error is just specific to the list of length 3 because the loop starts and ends at the same value and hence I had to handle a list of length 3 or less separately. Other lists work perfectly fine with this logic.</p>
  <p>Can someone tell me what is happening with this problem?
  Also if someone can tell about its time complexity and memory usage as compared to other algorithms like a brute force of kadane's algorithm etc, that would be helpful.</p>
  <p>Question on Leetcode is : <a href="https://leetcode.com/problems/maximum-subarray/" rel="nofollow noreferrer">Question</a></p>
  <p>Various other solutions are : <a href="https://leetcode.com/problems/maximum-subarray/discuss/1595195/C%2B%2BPython-7-Simple-Solutions-w-Explanation-or-Brute-Force-%2B-DP-%2B-Kadane-%2B-Divide-and-Conquer" rel="nofollow noreferrer">Solutions</a></p>