title: Remove triplets of adjacent numbers from the list
body: <p>Here is the problem. The input is a list of integers. If three adjacent numbers appear next to each other they should be dropped and the operation goes again. Iphone app with the balls of the same colors. The output should be the count of the balls that will be destroyed.</p>
  <p>Example:</p>
  <pre><code>input = [3,3,4,4,4,3,4]
  </code></pre>
  <p>1st iteration</p>
  <pre><code>output: [3,3,3,4]
  </code></pre>
  <p>Final output:</p>
  <pre><code>6   
  </code></pre>
  <p>4,4,4 on the first iteration, so three balls. and 3,3,3 on the second. Overall six.</p>
  <p>My code is below. It will remove the 4,4,4 but will fail after, because list index will quickly go out of range.</p>
  <pre><code>    def balls(a):
      
          curr_index = 0
          removed_count = 0
          while len(a) &gt; 2:
  
              if (a[curr_index] == a[curr_index+1]) and (a[curr_index] == a[curr_index+2]):
  
                  a.remove(a[curr_index])
                  a.remove(a[curr_index+1])
                  a.remove(a[curr_index+2])
  
              curr_index += 1
              removed_count += 3
  
          return removed_count
  
  a = [3, 3, 4, 4, 4, 3, 4]
      
  print(balls(a)) # should print 6
  </code></pre>
  <p>Any ideas?</p>