Problem statement:
There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells(neighbour). Each day, for each cell, if its neighbours are both active or both inactive, the cell becomes inactive the next day,. otherwise it becomes active the next day.
Assumptions: The two cells on the ends have single adjacent cell, so the other adjacent cell can be assumsed to be always inactive. Even after updating the cell state. consider its pervious state for updating the state of other cells. Update the cell informationof allcells simultaneously.
Write a fuction cellCompete which takes takes one 8 element array of integers cells representing the current state of 8 cells and one integer days representing te number of days to simulate. An integer value of 1 represents an active cell and value of 0 represents an inactive cell.
TESTCASES 1:
INPUT:
[1,0,0,0,0,1,0,0],1
EXPECTED RETURN VALUE:
[0,1,0,0,1,0,1,0]
TESTCASE 2:
INPUT:
[1,1,1,0,1,1,1,1,],2
EXPECTED RETURN VALUE:
[0,0,0,0,0,1,1,0]
There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells(neighbour). Each day, for each cell, if its neighbours are both active or both inactive, the cell becomes inactive the next day,. otherwise it becomes active the next day.
Assumptions: The two cells on the ends have single adjacent cell, so the other adjacent cell can be assumsed to be always inactive. Even after updating the cell state. consider its pervious state for updating the state of other cells. Update the cell informationof allcells simultaneously.
Write a fuction cellCompete which takes takes one 8 element array of integers cells representing the current state of 8 cells and one integer days representing te number of days to simulate. An integer value of 1 represents an active cell and value of 0 represents an inactive cell.
TESTCASES 1:
INPUT:
[1,0,0,0,0,1,0,0],1
EXPECTED RETURN VALUE:
[0,1,0,0,1,0,1,0]
TESTCASE 2:
INPUT:
[1,1,1,0,1,1,1,1,],2
EXPECTED RETURN VALUE:
[0,0,0,0,0,1,1,0]
public class Colony {
public static int[] cellCompete(int[] cells, int days) {
int len = cells.length;
int[] newCells = new int[cells.length];
for (int k = 0; k < days; k++) {
for (int i = 0; i < cells.length; i++) {
int cell = cells[i];
int nextCell;
int prevCell;
int activenumber;
if (i == 0) {
// edge cases
nextCell = cells[1];
prevCell = 0;
} else if (i == cells.length - 1) {
// edge case
prevCell = cells[cells.length - 2];
nextCell = 0;
} else {
nextCell = cells[i + 1];
prevCell = cells[i - 1];
}
if (nextCell == prevCell) {
// set it to inactive
activenumber = 0;
} else {
//set it to active
activenumber = 1;
}
newCells[i] = activenumber;
}
for (int i = 0; i < 8; i++) {
cells[i] = newCells[i];
}
}
return newCells;
}
public static void main(String[] args) {
int[] array = {1, 1, 1, 0, 1, 1, 1, 1};
int days = 2;
array = cellCompete(array, days);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
}
}
Here's some sweet little Python code:
ReplyDeletedef cell(arr, days):
new = arr[:]
n = len(arr)
if n == 1: print [0] #only 1 node
for _ in range(days):
new[0] = arr[1]
new[n - 1] = arr[n - 2]
for i in range(1, n-1):
new[i] = 1 - (arr[i-1] == arr[i+1])
arr = new[:]
return new
arr = [1, 1, 1, 0, 1, 1, 1, 1]
days = 2
print cell(arr, days)
Wrong..not working
Delete