Navigation

    ask avan logo
    • Register
    • Login
    • Search
    • Categories
    • Unsolved
    • Solved

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

    Python
    2
    2
    49
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      takeme last edited by avan

      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

      Reply Quote 0
        1 Reply Last reply

      • avan
        avan last edited by

        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.

        Reply Quote 0
          1 Reply Last reply

        • avan
          avan last edited by

          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.

          Reply Quote 0
            1 Reply Last reply

          • First post
            Last post