Conway's game of life neighbor count










3















def neighbors(matrix, r, c):

live_neighbors = 0

if matrix[r][c-1] != 0:
live_neighbors += 1
if matrix[r-1][c] != 0:
live_neighbors += 1
if matrix[r-1][c+1] != 0:
live_neighbors += 1
if matrix[r][c-1] != 0:
live_neighbors += 1
if matrix[r][c+1] != 0:
live_neighbors += 1
if matrix[r+1][c-1] != 0:
live_neighbors += 1
if matrix[r+1][c] != 0:
live_neighbors += 1
if matrix[r+1][c+1] != 0:
live_neighbors += 1

return live_neighbors


This is the code I have written so far. How do I count the neighbors of the border cells? If I use this code I would get an index out of range error.










share|improve this question
























  • One major consideration is borders: does the value in position [0, 0] have 3 neighbors? Or does it "roll over" and still have 8?

    – Brad Solomon
    Nov 14 '18 at 3:16











  • Either way, you may also find scipy.signal.convolve useful

    – Brad Solomon
    Nov 14 '18 at 3:17















3















def neighbors(matrix, r, c):

live_neighbors = 0

if matrix[r][c-1] != 0:
live_neighbors += 1
if matrix[r-1][c] != 0:
live_neighbors += 1
if matrix[r-1][c+1] != 0:
live_neighbors += 1
if matrix[r][c-1] != 0:
live_neighbors += 1
if matrix[r][c+1] != 0:
live_neighbors += 1
if matrix[r+1][c-1] != 0:
live_neighbors += 1
if matrix[r+1][c] != 0:
live_neighbors += 1
if matrix[r+1][c+1] != 0:
live_neighbors += 1

return live_neighbors


This is the code I have written so far. How do I count the neighbors of the border cells? If I use this code I would get an index out of range error.










share|improve this question
























  • One major consideration is borders: does the value in position [0, 0] have 3 neighbors? Or does it "roll over" and still have 8?

    – Brad Solomon
    Nov 14 '18 at 3:16











  • Either way, you may also find scipy.signal.convolve useful

    – Brad Solomon
    Nov 14 '18 at 3:17













3












3








3








def neighbors(matrix, r, c):

live_neighbors = 0

if matrix[r][c-1] != 0:
live_neighbors += 1
if matrix[r-1][c] != 0:
live_neighbors += 1
if matrix[r-1][c+1] != 0:
live_neighbors += 1
if matrix[r][c-1] != 0:
live_neighbors += 1
if matrix[r][c+1] != 0:
live_neighbors += 1
if matrix[r+1][c-1] != 0:
live_neighbors += 1
if matrix[r+1][c] != 0:
live_neighbors += 1
if matrix[r+1][c+1] != 0:
live_neighbors += 1

return live_neighbors


This is the code I have written so far. How do I count the neighbors of the border cells? If I use this code I would get an index out of range error.










share|improve this question
















def neighbors(matrix, r, c):

live_neighbors = 0

if matrix[r][c-1] != 0:
live_neighbors += 1
if matrix[r-1][c] != 0:
live_neighbors += 1
if matrix[r-1][c+1] != 0:
live_neighbors += 1
if matrix[r][c-1] != 0:
live_neighbors += 1
if matrix[r][c+1] != 0:
live_neighbors += 1
if matrix[r+1][c-1] != 0:
live_neighbors += 1
if matrix[r+1][c] != 0:
live_neighbors += 1
if matrix[r+1][c+1] != 0:
live_neighbors += 1

return live_neighbors


This is the code I have written so far. How do I count the neighbors of the border cells? If I use this code I would get an index out of range error.







python python-3.x matrix conways-game-of-life cellular-automata






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 23:17









blhsing

29.5k41336




29.5k41336










asked Nov 13 '18 at 22:58









BrowserMBrowserM

214




214












  • One major consideration is borders: does the value in position [0, 0] have 3 neighbors? Or does it "roll over" and still have 8?

    – Brad Solomon
    Nov 14 '18 at 3:16











  • Either way, you may also find scipy.signal.convolve useful

    – Brad Solomon
    Nov 14 '18 at 3:17

















  • One major consideration is borders: does the value in position [0, 0] have 3 neighbors? Or does it "roll over" and still have 8?

    – Brad Solomon
    Nov 14 '18 at 3:16











  • Either way, you may also find scipy.signal.convolve useful

    – Brad Solomon
    Nov 14 '18 at 3:17
















One major consideration is borders: does the value in position [0, 0] have 3 neighbors? Or does it "roll over" and still have 8?

– Brad Solomon
Nov 14 '18 at 3:16





One major consideration is borders: does the value in position [0, 0] have 3 neighbors? Or does it "roll over" and still have 8?

– Brad Solomon
Nov 14 '18 at 3:16













Either way, you may also find scipy.signal.convolve useful

– Brad Solomon
Nov 14 '18 at 3:17





Either way, you may also find scipy.signal.convolve useful

– Brad Solomon
Nov 14 '18 at 3:17












3 Answers
3






active

oldest

votes


















1














A possible solution without all those if statements:



def neighbors(matrix, r, c):
def get(r, c):
if 0 <= r < len(matrix) and 0 <= c < len(matrix[r]):
return matrix[r][c]
else:
return 0

neighbors_list = [get(r-1, c-1), get(r-1, c), get(r-1, c+1),
get(r , c-1), get(r , c+1),
get(r+1, c-1), get(r+1, c), get(r+1, c+1)]

return sum(map(bool, neighbors_list))

matrix = [ [0, 0, 0, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 1, 1],
[0, 0, 1, 1, 1] ]

print(neighbors(matrix, 0, 0)) # 0
print(neighbors(matrix, 1, 2)) # 1
print(neighbors(matrix, 3, 2)) # 4
print(neighbors(matrix, 4, 4)) # 3


In case the cells would only have values either 0 or 1, the neighbors function would simply return sum(neighbors_list).






share|improve this answer






























    1














    You can use a helper function to check for boundaries:



    def neighbors(matrix, r, c):
    def get(r, c):
    return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c]

    live_neighbors = 0

    if get(r, c-1) != 0:
    live_neighbors += 1
    if get(r-1, c) != 0:
    live_neighbors += 1
    if get(r-1, c+1) != 0:
    live_neighbors += 1
    if get(r, c-1) != 0:
    live_neighbors += 1
    if get(r, c+1) != 0:
    live_neighbors += 1
    if get(r+1, c-1) != 0:
    live_neighbors += 1
    if get(r+1, c) != 0:
    live_neighbors += 1
    if get(r+1, c+1) != 0:
    live_neighbors += 1

    return live_neighbors


    You can also use itertools.product in a generator expression for sum instead of if statements to count all the live neighbors:



    from itertools import product
    def neighbors(matrix, r, c):
    def get(r, c):
    return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c]
    return sum(get(r + i, c + j) for i, j in product(range(-1, 2), 2) if i or j)





    share|improve this answer

























    • why did you write len(matrix(r))?

      – BrowserM
      Nov 14 '18 at 9:57











    • Oops that was a typo indeed. Fixed. Thanks.

      – blhsing
      Nov 14 '18 at 13:15












    • Maybe inverse the if/else so you can use comparison chaining? if 0 <= r < len(matrix) and 0 <= c < len(matrix[r]): return matrix[r][c] else: return 0. In fact, you could just do return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c] and then drop the != 0 in the if clauses.

      – tobias_k
      Nov 15 '18 at 10:39












    • @tobias_k True thanks. I also took the opportunity to further optimize the rest of the code.

      – blhsing
      Nov 15 '18 at 14:08


















    0














    Closest to what you have would be:



    def neighbors(matrix, r, c):

    live_neighbors = 0

    if c and matrix[r][c-1] != 0:
    live_neighbors += 1
    if r and matrix[r-1][c] != 0:
    live_neighbors += 1
    if r and matrix[r-1][c+1] != 0:
    live_neighbors += 1
    if c and matrix[r][c-1] != 0:
    live_neighbors += 1
    if c < len(matrix[r])-1 and matrix[r][c+1] != 0:
    live_neighbors += 1
    if r < len(matrix)-1 and matrix[r+1][c-1] != 0:
    live_neighbors += 1
    if r < len(matrix)-1 and matrix[r+1][c] != 0:
    live_neighbors += 1
    if r < len(matrix)-1 and matrix[r+1][c+1] != 0:
    live_neighbors += 1

    return live_neighbors


    and so forth.






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290763%2fconways-game-of-life-neighbor-count%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      A possible solution without all those if statements:



      def neighbors(matrix, r, c):
      def get(r, c):
      if 0 <= r < len(matrix) and 0 <= c < len(matrix[r]):
      return matrix[r][c]
      else:
      return 0

      neighbors_list = [get(r-1, c-1), get(r-1, c), get(r-1, c+1),
      get(r , c-1), get(r , c+1),
      get(r+1, c-1), get(r+1, c), get(r+1, c+1)]

      return sum(map(bool, neighbors_list))

      matrix = [ [0, 0, 0, 0, 0],
      [0, 0, 0, 0, 1],
      [0, 0, 0, 1, 1],
      [0, 0, 0, 1, 1],
      [0, 0, 1, 1, 1] ]

      print(neighbors(matrix, 0, 0)) # 0
      print(neighbors(matrix, 1, 2)) # 1
      print(neighbors(matrix, 3, 2)) # 4
      print(neighbors(matrix, 4, 4)) # 3


      In case the cells would only have values either 0 or 1, the neighbors function would simply return sum(neighbors_list).






      share|improve this answer



























        1














        A possible solution without all those if statements:



        def neighbors(matrix, r, c):
        def get(r, c):
        if 0 <= r < len(matrix) and 0 <= c < len(matrix[r]):
        return matrix[r][c]
        else:
        return 0

        neighbors_list = [get(r-1, c-1), get(r-1, c), get(r-1, c+1),
        get(r , c-1), get(r , c+1),
        get(r+1, c-1), get(r+1, c), get(r+1, c+1)]

        return sum(map(bool, neighbors_list))

        matrix = [ [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 1],
        [0, 0, 0, 1, 1],
        [0, 0, 0, 1, 1],
        [0, 0, 1, 1, 1] ]

        print(neighbors(matrix, 0, 0)) # 0
        print(neighbors(matrix, 1, 2)) # 1
        print(neighbors(matrix, 3, 2)) # 4
        print(neighbors(matrix, 4, 4)) # 3


        In case the cells would only have values either 0 or 1, the neighbors function would simply return sum(neighbors_list).






        share|improve this answer

























          1












          1








          1







          A possible solution without all those if statements:



          def neighbors(matrix, r, c):
          def get(r, c):
          if 0 <= r < len(matrix) and 0 <= c < len(matrix[r]):
          return matrix[r][c]
          else:
          return 0

          neighbors_list = [get(r-1, c-1), get(r-1, c), get(r-1, c+1),
          get(r , c-1), get(r , c+1),
          get(r+1, c-1), get(r+1, c), get(r+1, c+1)]

          return sum(map(bool, neighbors_list))

          matrix = [ [0, 0, 0, 0, 0],
          [0, 0, 0, 0, 1],
          [0, 0, 0, 1, 1],
          [0, 0, 0, 1, 1],
          [0, 0, 1, 1, 1] ]

          print(neighbors(matrix, 0, 0)) # 0
          print(neighbors(matrix, 1, 2)) # 1
          print(neighbors(matrix, 3, 2)) # 4
          print(neighbors(matrix, 4, 4)) # 3


          In case the cells would only have values either 0 or 1, the neighbors function would simply return sum(neighbors_list).






          share|improve this answer













          A possible solution without all those if statements:



          def neighbors(matrix, r, c):
          def get(r, c):
          if 0 <= r < len(matrix) and 0 <= c < len(matrix[r]):
          return matrix[r][c]
          else:
          return 0

          neighbors_list = [get(r-1, c-1), get(r-1, c), get(r-1, c+1),
          get(r , c-1), get(r , c+1),
          get(r+1, c-1), get(r+1, c), get(r+1, c+1)]

          return sum(map(bool, neighbors_list))

          matrix = [ [0, 0, 0, 0, 0],
          [0, 0, 0, 0, 1],
          [0, 0, 0, 1, 1],
          [0, 0, 0, 1, 1],
          [0, 0, 1, 1, 1] ]

          print(neighbors(matrix, 0, 0)) # 0
          print(neighbors(matrix, 1, 2)) # 1
          print(neighbors(matrix, 3, 2)) # 4
          print(neighbors(matrix, 4, 4)) # 3


          In case the cells would only have values either 0 or 1, the neighbors function would simply return sum(neighbors_list).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 10:33









          myrmicamyrmica

          41418




          41418























              1














              You can use a helper function to check for boundaries:



              def neighbors(matrix, r, c):
              def get(r, c):
              return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c]

              live_neighbors = 0

              if get(r, c-1) != 0:
              live_neighbors += 1
              if get(r-1, c) != 0:
              live_neighbors += 1
              if get(r-1, c+1) != 0:
              live_neighbors += 1
              if get(r, c-1) != 0:
              live_neighbors += 1
              if get(r, c+1) != 0:
              live_neighbors += 1
              if get(r+1, c-1) != 0:
              live_neighbors += 1
              if get(r+1, c) != 0:
              live_neighbors += 1
              if get(r+1, c+1) != 0:
              live_neighbors += 1

              return live_neighbors


              You can also use itertools.product in a generator expression for sum instead of if statements to count all the live neighbors:



              from itertools import product
              def neighbors(matrix, r, c):
              def get(r, c):
              return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c]
              return sum(get(r + i, c + j) for i, j in product(range(-1, 2), 2) if i or j)





              share|improve this answer

























              • why did you write len(matrix(r))?

                – BrowserM
                Nov 14 '18 at 9:57











              • Oops that was a typo indeed. Fixed. Thanks.

                – blhsing
                Nov 14 '18 at 13:15












              • Maybe inverse the if/else so you can use comparison chaining? if 0 <= r < len(matrix) and 0 <= c < len(matrix[r]): return matrix[r][c] else: return 0. In fact, you could just do return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c] and then drop the != 0 in the if clauses.

                – tobias_k
                Nov 15 '18 at 10:39












              • @tobias_k True thanks. I also took the opportunity to further optimize the rest of the code.

                – blhsing
                Nov 15 '18 at 14:08















              1














              You can use a helper function to check for boundaries:



              def neighbors(matrix, r, c):
              def get(r, c):
              return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c]

              live_neighbors = 0

              if get(r, c-1) != 0:
              live_neighbors += 1
              if get(r-1, c) != 0:
              live_neighbors += 1
              if get(r-1, c+1) != 0:
              live_neighbors += 1
              if get(r, c-1) != 0:
              live_neighbors += 1
              if get(r, c+1) != 0:
              live_neighbors += 1
              if get(r+1, c-1) != 0:
              live_neighbors += 1
              if get(r+1, c) != 0:
              live_neighbors += 1
              if get(r+1, c+1) != 0:
              live_neighbors += 1

              return live_neighbors


              You can also use itertools.product in a generator expression for sum instead of if statements to count all the live neighbors:



              from itertools import product
              def neighbors(matrix, r, c):
              def get(r, c):
              return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c]
              return sum(get(r + i, c + j) for i, j in product(range(-1, 2), 2) if i or j)





              share|improve this answer

























              • why did you write len(matrix(r))?

                – BrowserM
                Nov 14 '18 at 9:57











              • Oops that was a typo indeed. Fixed. Thanks.

                – blhsing
                Nov 14 '18 at 13:15












              • Maybe inverse the if/else so you can use comparison chaining? if 0 <= r < len(matrix) and 0 <= c < len(matrix[r]): return matrix[r][c] else: return 0. In fact, you could just do return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c] and then drop the != 0 in the if clauses.

                – tobias_k
                Nov 15 '18 at 10:39












              • @tobias_k True thanks. I also took the opportunity to further optimize the rest of the code.

                – blhsing
                Nov 15 '18 at 14:08













              1












              1








              1







              You can use a helper function to check for boundaries:



              def neighbors(matrix, r, c):
              def get(r, c):
              return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c]

              live_neighbors = 0

              if get(r, c-1) != 0:
              live_neighbors += 1
              if get(r-1, c) != 0:
              live_neighbors += 1
              if get(r-1, c+1) != 0:
              live_neighbors += 1
              if get(r, c-1) != 0:
              live_neighbors += 1
              if get(r, c+1) != 0:
              live_neighbors += 1
              if get(r+1, c-1) != 0:
              live_neighbors += 1
              if get(r+1, c) != 0:
              live_neighbors += 1
              if get(r+1, c+1) != 0:
              live_neighbors += 1

              return live_neighbors


              You can also use itertools.product in a generator expression for sum instead of if statements to count all the live neighbors:



              from itertools import product
              def neighbors(matrix, r, c):
              def get(r, c):
              return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c]
              return sum(get(r + i, c + j) for i, j in product(range(-1, 2), 2) if i or j)





              share|improve this answer















              You can use a helper function to check for boundaries:



              def neighbors(matrix, r, c):
              def get(r, c):
              return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c]

              live_neighbors = 0

              if get(r, c-1) != 0:
              live_neighbors += 1
              if get(r-1, c) != 0:
              live_neighbors += 1
              if get(r-1, c+1) != 0:
              live_neighbors += 1
              if get(r, c-1) != 0:
              live_neighbors += 1
              if get(r, c+1) != 0:
              live_neighbors += 1
              if get(r+1, c-1) != 0:
              live_neighbors += 1
              if get(r+1, c) != 0:
              live_neighbors += 1
              if get(r+1, c+1) != 0:
              live_neighbors += 1

              return live_neighbors


              You can also use itertools.product in a generator expression for sum instead of if statements to count all the live neighbors:



              from itertools import product
              def neighbors(matrix, r, c):
              def get(r, c):
              return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c]
              return sum(get(r + i, c + j) for i, j in product(range(-1, 2), 2) if i or j)






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 15 '18 at 14:07

























              answered Nov 13 '18 at 23:17









              blhsingblhsing

              29.5k41336




              29.5k41336












              • why did you write len(matrix(r))?

                – BrowserM
                Nov 14 '18 at 9:57











              • Oops that was a typo indeed. Fixed. Thanks.

                – blhsing
                Nov 14 '18 at 13:15












              • Maybe inverse the if/else so you can use comparison chaining? if 0 <= r < len(matrix) and 0 <= c < len(matrix[r]): return matrix[r][c] else: return 0. In fact, you could just do return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c] and then drop the != 0 in the if clauses.

                – tobias_k
                Nov 15 '18 at 10:39












              • @tobias_k True thanks. I also took the opportunity to further optimize the rest of the code.

                – blhsing
                Nov 15 '18 at 14:08

















              • why did you write len(matrix(r))?

                – BrowserM
                Nov 14 '18 at 9:57











              • Oops that was a typo indeed. Fixed. Thanks.

                – blhsing
                Nov 14 '18 at 13:15












              • Maybe inverse the if/else so you can use comparison chaining? if 0 <= r < len(matrix) and 0 <= c < len(matrix[r]): return matrix[r][c] else: return 0. In fact, you could just do return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c] and then drop the != 0 in the if clauses.

                – tobias_k
                Nov 15 '18 at 10:39












              • @tobias_k True thanks. I also took the opportunity to further optimize the rest of the code.

                – blhsing
                Nov 15 '18 at 14:08
















              why did you write len(matrix(r))?

              – BrowserM
              Nov 14 '18 at 9:57





              why did you write len(matrix(r))?

              – BrowserM
              Nov 14 '18 at 9:57













              Oops that was a typo indeed. Fixed. Thanks.

              – blhsing
              Nov 14 '18 at 13:15






              Oops that was a typo indeed. Fixed. Thanks.

              – blhsing
              Nov 14 '18 at 13:15














              Maybe inverse the if/else so you can use comparison chaining? if 0 <= r < len(matrix) and 0 <= c < len(matrix[r]): return matrix[r][c] else: return 0. In fact, you could just do return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c] and then drop the != 0 in the if clauses.

              – tobias_k
              Nov 15 '18 at 10:39






              Maybe inverse the if/else so you can use comparison chaining? if 0 <= r < len(matrix) and 0 <= c < len(matrix[r]): return matrix[r][c] else: return 0. In fact, you could just do return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c] and then drop the != 0 in the if clauses.

              – tobias_k
              Nov 15 '18 at 10:39














              @tobias_k True thanks. I also took the opportunity to further optimize the rest of the code.

              – blhsing
              Nov 15 '18 at 14:08





              @tobias_k True thanks. I also took the opportunity to further optimize the rest of the code.

              – blhsing
              Nov 15 '18 at 14:08











              0














              Closest to what you have would be:



              def neighbors(matrix, r, c):

              live_neighbors = 0

              if c and matrix[r][c-1] != 0:
              live_neighbors += 1
              if r and matrix[r-1][c] != 0:
              live_neighbors += 1
              if r and matrix[r-1][c+1] != 0:
              live_neighbors += 1
              if c and matrix[r][c-1] != 0:
              live_neighbors += 1
              if c < len(matrix[r])-1 and matrix[r][c+1] != 0:
              live_neighbors += 1
              if r < len(matrix)-1 and matrix[r+1][c-1] != 0:
              live_neighbors += 1
              if r < len(matrix)-1 and matrix[r+1][c] != 0:
              live_neighbors += 1
              if r < len(matrix)-1 and matrix[r+1][c+1] != 0:
              live_neighbors += 1

              return live_neighbors


              and so forth.






              share|improve this answer



























                0














                Closest to what you have would be:



                def neighbors(matrix, r, c):

                live_neighbors = 0

                if c and matrix[r][c-1] != 0:
                live_neighbors += 1
                if r and matrix[r-1][c] != 0:
                live_neighbors += 1
                if r and matrix[r-1][c+1] != 0:
                live_neighbors += 1
                if c and matrix[r][c-1] != 0:
                live_neighbors += 1
                if c < len(matrix[r])-1 and matrix[r][c+1] != 0:
                live_neighbors += 1
                if r < len(matrix)-1 and matrix[r+1][c-1] != 0:
                live_neighbors += 1
                if r < len(matrix)-1 and matrix[r+1][c] != 0:
                live_neighbors += 1
                if r < len(matrix)-1 and matrix[r+1][c+1] != 0:
                live_neighbors += 1

                return live_neighbors


                and so forth.






                share|improve this answer

























                  0












                  0








                  0







                  Closest to what you have would be:



                  def neighbors(matrix, r, c):

                  live_neighbors = 0

                  if c and matrix[r][c-1] != 0:
                  live_neighbors += 1
                  if r and matrix[r-1][c] != 0:
                  live_neighbors += 1
                  if r and matrix[r-1][c+1] != 0:
                  live_neighbors += 1
                  if c and matrix[r][c-1] != 0:
                  live_neighbors += 1
                  if c < len(matrix[r])-1 and matrix[r][c+1] != 0:
                  live_neighbors += 1
                  if r < len(matrix)-1 and matrix[r+1][c-1] != 0:
                  live_neighbors += 1
                  if r < len(matrix)-1 and matrix[r+1][c] != 0:
                  live_neighbors += 1
                  if r < len(matrix)-1 and matrix[r+1][c+1] != 0:
                  live_neighbors += 1

                  return live_neighbors


                  and so forth.






                  share|improve this answer













                  Closest to what you have would be:



                  def neighbors(matrix, r, c):

                  live_neighbors = 0

                  if c and matrix[r][c-1] != 0:
                  live_neighbors += 1
                  if r and matrix[r-1][c] != 0:
                  live_neighbors += 1
                  if r and matrix[r-1][c+1] != 0:
                  live_neighbors += 1
                  if c and matrix[r][c-1] != 0:
                  live_neighbors += 1
                  if c < len(matrix[r])-1 and matrix[r][c+1] != 0:
                  live_neighbors += 1
                  if r < len(matrix)-1 and matrix[r+1][c-1] != 0:
                  live_neighbors += 1
                  if r < len(matrix)-1 and matrix[r+1][c] != 0:
                  live_neighbors += 1
                  if r < len(matrix)-1 and matrix[r+1][c+1] != 0:
                  live_neighbors += 1

                  return live_neighbors


                  and so forth.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 10:41









                  kabanuskabanus

                  11.4k31339




                  11.4k31339



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290763%2fconways-game-of-life-neighbor-count%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線