Skip to content

[mypy] Fix type annotations for cellular_automata/game_of_life.py #5491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions cellular_automata/game_of_life.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@
random.shuffle(choice)


def create_canvas(size):
def create_canvas(size: int) -> list[list[bool]]:
canvas = [[False for i in range(size)] for j in range(size)]
return canvas


def seed(canvas):
def seed(canvas: list[list[bool]]) -> None:
for i, row in enumerate(canvas):
for j, _ in enumerate(row):
canvas[i][j] = bool(random.getrandbits(1))


def run(canvas):
def run(canvas: list[list[bool]]) -> list[list[bool]]:
"""This function runs the rules of game through all points, and changes their
status accordingly.(in the same canvas)
@Args:
Expand All @@ -62,21 +62,22 @@ def run(canvas):
--
None
"""
canvas = np.array(canvas)
next_gen_canvas = np.array(create_canvas(canvas.shape[0]))
for r, row in enumerate(canvas):
current_canvas = np.array(canvas)
next_gen_canvas = np.array(create_canvas(current_canvas.shape[0]))
for r, row in enumerate(current_canvas):
for c, pt in enumerate(row):
# print(r-1,r+2,c-1,c+2)
next_gen_canvas[r][c] = __judge_point(
pt, canvas[r - 1 : r + 2, c - 1 : c + 2]
pt, current_canvas[r - 1 : r + 2, c - 1 : c + 2]
)

canvas = next_gen_canvas
current_canvas = next_gen_canvas
del next_gen_canvas # cleaning memory as we move on.
return canvas.tolist()
return_canvas: list[list[bool]] = current_canvas.tolist()
return return_canvas


def __judge_point(pt, neighbours):
def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool:
dead = 0
alive = 0
# finding dead or alive neighbours count.
Expand Down