title: Creating an algorithm to solve a problem in python
body: <p><strong>Algorithm Objective:</strong></p>
  
  <p>link to the pictures i took while giving the amazon interview: </p>
  
  <p>[<a href="https://boards.wetransfer.com/board/shl7w5z1e62os7nwv20190618224258/latest][pictures]" rel="nofollow noreferrer">https://boards.wetransfer.com/board/shl7w5z1e62os7nwv20190618224258/latest][pictures]</a></p>
  
  <p>Eight houses, represented as cells, are arranged in a straight line. Each day every cell competes with its adjacent cells(neighbors). An integer value of 1 represents an active cell and a value of 0 represents an inactive cell. If the neighbors on both sides of a cell are either active or inactive, the cell becomes inactive on the next day, otherwise the cell becomes active. The two cell on each end have a single a single adjacent cell, so assume that the unoccupied space on the opposite side is an inactive cell. Even after updating the cell state, consider its previous state when updating the state of other cells. The state information of all cells should be updated simultaneously.</p>
  
  <p>Create an algorithm to output the state of the cells after the given number of days.</p>
  
  <p><strong>Input:</strong></p>
  
  <p>The input to the function/method consists of two arguments:
  <code>states</code>, a list of integers representing the current state of cells,
  <code>days</code>,an integer representing the number of days.</p>
  
  <p><strong>Output:</strong></p>
  
  <p>Return a list of integers representing the state of the cells after the given number of days</p>
  
  <p><strong>Note:</strong></p>
  
  <p>The elements of the list states contains 0s and 1s only</p>
  
  <p><strong>TestCase 1:</strong></p>
  
  <p>Input: <code>[1,0,0,0,0,1,0,0] , 1</code></p>
  
  <p>Expected Return Value: <code>0 1 0 0 1 0 1 0</code></p>
  
  <p>TestCase 2:</p>
  
  <p>Input: <code>[1,1,1,0,1,1,1,1] , 2</code></p>
  
  <p>Expected Return Value: <code>0 0 0 0 0 1 1 0</code></p>
  
  <p><strong>What I Tried:</strong></p>
  
  <pre><code>def cellCompete(states, days):
      # WRITE YOUR CODE HERE
      il = 0; 
      tl = len(states);
      intialvalue = states
      results = []
      states = []
      for i in range(days):
        #first range
        if(intialvalue[il] != intialvalue[il+1]):
          print('value of index 0 is : ',reverse(intialvalue[il]))
          results.append(reverse(intialvalue[il]))
        else:
          print('value of index 0 is :', intialvalue[il])
          results.append(intialvalue[il])
        print("-------------------")  
  
        #range middle
        while il &lt; tl-2:
          if(intialvalue[il] != intialvalue[il+1] or intialvalue[il+1] != intialvalue[il+2]):
            print('value of index',il+1,'is : ',reverse(intialvalue[il+1]))
            results.append(reverse(intialvalue[il+1]))
          else:
            print('value of index', il+1,'is :', intialvalue[il+1])
            results.append(intialvalue[il+1])
          print("-------------------") 
          il += 1
  
        #range last
        if(intialvalue[tl-2] != intialvalue[tl-1]):
          print('value of index',tl-1,'is : ',reverse(intialvalue[tl-1]))
          results.append(reverse(intialvalue[tl-1]))
        else:
          print('value of index',tl-1,'is :', intialvalue[tl-1])
          results.append(intialvalue[tl-1])
        print("-------------------")  
  
        print('Input: ',intialvalue)
        print('Results: ',results)
        initialvalue = results
  
  def reverse(val):
      if(val == 0):
        return 1
      elif(val == 1):
        return 0
  print("-------------------------Test case 1--------------------")
  cellCompete([1,0,0,0,0,1,0,0],1)
  print("-------------------------Test case 2--------------------")
  cellCompete([1,1,1,0,1,1,1,1],2)
  </code></pre>
  
  <p>I am relatively new to python and i could not complete this algorithm for the second case on this python</p>