[mypy] Add missing type annotation (#5491)

This commit is contained in:
Erwin Junge 2021-10-22 11:45:30 +02:00 committed by GitHub
parent 08254eb2e4
commit d924a8051b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,18 +40,18 @@ choice = [0] * 100 + [1] * 10
random.shuffle(choice) 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)] canvas = [[False for i in range(size)] for j in range(size)]
return canvas return canvas
def seed(canvas): def seed(canvas: list[list[bool]]) -> None:
for i, row in enumerate(canvas): for i, row in enumerate(canvas):
for j, _ in enumerate(row): for j, _ in enumerate(row):
canvas[i][j] = bool(random.getrandbits(1)) 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 """This function runs the rules of game through all points, and changes their
status accordingly.(in the same canvas) status accordingly.(in the same canvas)
@Args: @Args:
@ -62,21 +62,22 @@ def run(canvas):
-- --
None None
""" """
canvas = np.array(canvas) current_canvas = np.array(canvas)
next_gen_canvas = np.array(create_canvas(canvas.shape[0])) next_gen_canvas = np.array(create_canvas(current_canvas.shape[0]))
for r, row in enumerate(canvas): for r, row in enumerate(current_canvas):
for c, pt in enumerate(row): for c, pt in enumerate(row):
# print(r-1,r+2,c-1,c+2) # print(r-1,r+2,c-1,c+2)
next_gen_canvas[r][c] = __judge_point( 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. 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 dead = 0
alive = 0 alive = 0
# finding dead or alive neighbours count. # finding dead or alive neighbours count.