Code For Alls..!

get the code!!

Thursday 31 August 2017

Complete Cell State After D Days

                                                 
There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells(neighbours). Each day, for each cell, if it's neighbours are both active or both inactive, the cell becomes inactive the next day, otherwise it becomes active the next day.
The two cells at the ends have only single adjacent cell, so the other imaginary adjacent cell can be assumed to be always inactive.
On a given day, even after updating the cell state, consider it's previous day's state for updating the state of other cells.
Complete the function cellComplete which takes an 8 element array of integer cells representing the current state of 8 cells and an integer D days representing the number of days to simulate.
An integer value of 1 represents an active cell and value of 0 represents an inactive cell.
Input Format:
The first line contains 8 integer values representing the initial state of the cells.
The second line contains D - which represents the number of days.
Output Format:
The first line contains the state of the cells after D days.
Boundary Conditions:
1 <= D <= 1000
Example Input/Output 1:
Input:
1 0 0 0 0 1 0 0
3
Output:
0 0 1 1 1 0 1 0
Example Input/Output 2:
Input:
1 0 0 0 0 1 0 0
2
Output:
1 0 1 1 0 0 0 1
python:

def cellCompete(c,d):
     for _ in range(d):
          for i in range(8):
                n=c[i]
                if i>0 and i<7:
                    c[i]=0 if c[i+1]==a else 1
                elif i==0:
                                    c[i]=0 if c[i+1]==0 else 1
                            else:
                                      c[i]=0 if a==0 else 1
                    a=n
          return c;

cells = [int(x) for x in input().split()]
days = int(input())
print(*cellCompete(cells,days))