Skip to content

Commit d924a80

Browse files
authored
[mypy] Add missing type annotation (TheAlgorithms#5491)
1 parent 08254eb commit d924a80

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

Diff for: cellular_automata/game_of_life.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@
4040
random.shuffle(choice)
4141

4242

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

4747

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

5353

54-
def run(canvas):
54+
def run(canvas: list[list[bool]]) -> list[list[bool]]:
5555
"""This function runs the rules of game through all points, and changes their
5656
status accordingly.(in the same canvas)
5757
@Args:
@@ -62,21 +62,22 @@ def run(canvas):
6262
--
6363
None
6464
"""
65-
canvas = np.array(canvas)
66-
next_gen_canvas = np.array(create_canvas(canvas.shape[0]))
67-
for r, row in enumerate(canvas):
65+
current_canvas = np.array(canvas)
66+
next_gen_canvas = np.array(create_canvas(current_canvas.shape[0]))
67+
for r, row in enumerate(current_canvas):
6868
for c, pt in enumerate(row):
6969
# print(r-1,r+2,c-1,c+2)
7070
next_gen_canvas[r][c] = __judge_point(
71-
pt, canvas[r - 1 : r + 2, c - 1 : c + 2]
71+
pt, current_canvas[r - 1 : r + 2, c - 1 : c + 2]
7272
)
7373

74-
canvas = next_gen_canvas
74+
current_canvas = next_gen_canvas
7575
del next_gen_canvas # cleaning memory as we move on.
76-
return canvas.tolist()
76+
return_canvas: list[list[bool]] = current_canvas.tolist()
77+
return return_canvas
7778

7879

79-
def __judge_point(pt, neighbours):
80+
def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool:
8081
dead = 0
8182
alive = 0
8283
# finding dead or alive neighbours count.

0 commit comments

Comments
 (0)