From 4684a8a4069e0ed40e89c0c2bf1d54ddcc72938d Mon Sep 17 00:00:00 2001 From: Erwin Junge Date: Wed, 20 Oct 2021 23:29:18 +0200 Subject: [PATCH] Add missing type annotation --- cellular_automata/game_of_life.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/cellular_automata/game_of_life.py b/cellular_automata/game_of_life.py index 09863993dc3a..c5324da73dbf 100644 --- a/cellular_automata/game_of_life.py +++ b/cellular_automata/game_of_life.py @@ -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: @@ -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.