MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / new_generation

Function new_generation

cellular_automata/one_dimensional.py:30–41  ·  view source on GitHub ↗
(cells: list[list[int]], rule: list[int], time: int)

Source from the content-addressed store, hash-verified

28
29
30def new_generation(cells: list[list[int]], rule: list[int], time: int) -> list[int]:
31 population = len(cells[0]) # 31
32 next_generation = []
33 for i in range(population):
34 # Get the neighbors of each cell
35 # Handle neighbours outside bounds by using 0 as their value
36 left_neighbor = 0 if i == 0 else cells[time][i - 1]
37 right_neighbor = 0 if i == population - 1 else cells[time][i + 1]
38 # Define a new cell and add it to the new generation
39 situation = 7 - int(f"{left_neighbor}{cells[time][i]}{right_neighbor}", 2)
40 next_generation.append(rule[situation])
41 return next_generation
42
43
44def generate_image(cells: list[list[int]]) -> Image.Image:

Callers 1

one_dimensional.pyFile · 0.70

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected