Clarification Needed on Active and Inactive House cells after k days Problem using Python



  • Write python code(2.7 version) for the following and also explain the logic because i am not understanding please explain.

    Eight houses ,represented as cells , are arranged in a straight line.Each day every cell competes with its adjacent cells(neighbours). An integer value of 1 represents an active cell and a value of 0 represents an inactive cell , the cell becomes inactive on the next day; otherwise the cell becomes active.The two cells on each end have 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.

    Write an algorithm to output the state of the cells after the given number of days.

    ==================================================

    Input

    The input to the function/method consists of two arguments:

    states , a list of integers representing the current state of cells ;

    days ; an integer representing the number of days.

    Output

    Return a list of integers representing the state of the cells after the given number of days.

    Note

    The elements of the list states contains 0s and 1s only.

    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]

    ==================================================================

    def cellCompete(states,days):

    #write your code logic with explanation

    pass



  • Hi there.

    You are missing some text in the description.

    ...inactive cell. If both the neighbours are either active or inactive, the cell becomes inactive the next day, otherwise it becomes active on the next day. ...

    So everything gets turned to 1 except when both neighbor cells (to the left and right of the cell) are the same. So if both neighbor cells are either 0' or 1's the cell becomes 0. The VERY first and VERY last cell have an invisible neighbor to left and right respectively with a value of 0 that doesn't change.

    ======================================================
    Let's use a 3 cell model instead of an 8 cell model.

    Initial Values:
    0, 1, 0

    After Day 1:
    1, 0, 1

    After Day 2:
    0, 0, 1

    Note: 0, 1, 0 have two invisible cell values that do not change values e.g. (0), 0, 1, 0 , (0)

    ======================================================

    Here are some of the questions to ask when you trying to implement a solution:

    • How many arrays (collections) do you need?

    • How can you keep track of the original previous and next cell values as you change the cell values?

    • How many loops do you need? One? Two?

    • How do you handle the invisible edge cell values?

    I hope this helps a little.



  • Hi there.

    You are missing some text in the description.

    ...inactive cell. If both the neighbours are either active or inactive, the cell becomes inactive the next day, otherwise it becomes active on the next day. ...

    So everything gets turned to 1 except when both neighbor cells (to the left and right of the cell) are the same. So if both neighbor cells are either 0' or 1's the cell becomes 0. The VERY first and VERY last cell have an invisible neighbor to left and right respectively with a value of 0 that doesn't change.

    ======================================================
    Let's use a 3 cell model instead of an 8 cell model.

    Initial Values:
    0, 1, 0

    After Day 1:
    1, 0, 1

    After Day 2:
    0, 0, 1

    Note: 0, 1, 0 have two invisible cell values that do not change values e.g. (0), 0, 1, 0 , (0)

    ======================================================

    Here are some of the questions to ask when you trying to implement a solution:

    • How many arrays (collections) do you need?

    • How can you keep track of the original previous and next cell values as you change the cell values?

    • How many loops do you need? One? Two?

    • How do you handle the invisible edge cell values?

    I hope this helps a little.


Log in to reply