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

Function count_neighbors

example_code/item_071.py:90–104  ·  view source on GitHub ↗
(y, x, get_cell)

Source from the content-addressed store, hash-verified

88
89print("Example 4")
90def count_neighbors(y, x, get_cell):
91 n_ = get_cell(y - 1, x + 0) # North
92 ne = get_cell(y - 1, x + 1) # Northeast
93 e_ = get_cell(y + 0, x + 1) # East
94 se = get_cell(y + 1, x + 1) # Southeast
95 s_ = get_cell(y + 1, x + 0) # South
96 sw = get_cell(y + 1, x - 1) # Southwest
97 w_ = get_cell(y + 0, x - 1) # West
98 nw = get_cell(y - 1, x - 1) # Northwest
99 neighbor_states = [n_, ne, e_, se, s_, sw, w_, nw]
100 count = 0
101 for state in neighbor_states:
102 if state == ALIVE:
103 count += 1
104 return count
105
106
107alive = {(9, 5), (9, 6)}

Callers 2

item_071.pyFile · 0.70
step_cellFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected