MCPcopy Create free account
hub / github.com/bslatkin/effectivepython / count_neighbors

Function count_neighbors

example_code/item_075.py:77–91  ·  view source on GitHub ↗
(y, x, get_cell)

Source from the content-addressed store, hash-verified

75
76
77def count_neighbors(y, x, get_cell):
78 n_ = get_cell(y - 1, x + 0) # North
79 ne = get_cell(y - 1, x + 1) # Northeast
80 e_ = get_cell(y + 0, x + 1) # East
81 se = get_cell(y + 1, x + 1) # Southeast
82 s_ = get_cell(y + 1, x + 0) # South
83 sw = get_cell(y + 1, x - 1) # Southwest
84 w_ = get_cell(y + 0, x - 1) # West
85 nw = get_cell(y - 1, x - 1) # Northwest
86 neighbor_states = [n_, ne, e_, se, s_, sw, w_, nw]
87 count = 0
88 for state in neighbor_states:
89 if state == ALIVE:
90 count += 1
91 return count
92
93async def game_logic(state, neighbors):
94 # Do some input/output in here:

Callers 1

step_cellFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected