



"""simple docstring"""





from __future__ import annotations

from PIL import Image

# Define glider example
lowercase  							=					[
    [0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
]

# Define blinker example
lowercase  							=					[[0, 1, 0], [0, 1, 0], [0, 1, 0]]




def 	UpperCAmelCase     (		A	:						list[list[int]]       ):

		'''simple docstring'''


		_UpperCAmelCase							 =		[]
		for i in range(len(A       )       ):
				_UpperCAmelCase							 =		[]
				for j in range(len(cells[i]       )       ):
						# Get the number of live neighbours
						_UpperCAmelCase							 =		0
						if i > 0 and j > 0:
								neighbour_count += cells[i - 1][j - 1]
						if i > 0:
								neighbour_count += cells[i - 1][j]
						if i > 0 and j < len(cells[i]       ) - 1:
								neighbour_count += cells[i - 1][j + 1]
						if j > 0:
								neighbour_count += cells[i][j - 1]
						if j < len(cells[i]       ) - 1:
								neighbour_count += cells[i][j + 1]
						if i < len(A       ) - 1 and j > 0:
								neighbour_count += cells[i + 1][j - 1]
						if i < len(A       ) - 1:
								neighbour_count += cells[i + 1][j]
						if i < len(A       ) - 1 and j < len(cells[i]       ) - 1:
								neighbour_count += cells[i + 1][j + 1]

						# Rules of the game of life (excerpt from Wikipedia):
						# 1. Any live cell with two or three live neighbours survives.
						# 2. Any dead cell with three live neighbours becomes a live cell.
						# 3. All other live cells die in the next generation.
						#    Similarly, all other dead cells stay dead.
						_UpperCAmelCase							 =		cells[i][j] == 1
						if (
						    (alive and 2 <= neighbour_count <= 3)
						    or not alive
						    and neighbour_count == 3
						):
								next_generation_row.append(1       )
						else:
								next_generation_row.append(0       )

				next_generation.append(A       )
		return next_generation




def 	UpperCAmelCase     (		A	:						list[list[int]]     ,				A	:						int       ):

		'''simple docstring'''


		_UpperCAmelCase							 =		[]
		for _ in range(A       ):
				# Create output image
				_UpperCAmelCase							 =		Image.new('RGB'     ,				(len(cells[0]       ), len(A       ))       )
				_UpperCAmelCase							 =		img.load()

				# Save cells to image
				for x in range(len(A       )       ):
						for y in range(len(cells[0]       )       ):
								_UpperCAmelCase							 =		255 - cells[y][x] * 255
								_UpperCAmelCase							 =		(colour, colour, colour)

        # Save image
				images.append(A       )
				_UpperCAmelCase							 =		new_generation(A       )
		return images


if __name__ == "__main__":
					lowercase  							=					generate_images(GLIDER, 16)
					images[0].save('''out.gif''', save_all=True, append_images=images[1:])







"""simple docstring"""





def 	UpperCAmelCase     (		A	:						int       ):

		'''simple docstring'''


		_UpperCAmelCase							 =		abs(A       )
		_UpperCAmelCase							 =		0
		while n > 0:
				res += n % 10
				n //= 10
		return res




def 	UpperCAmelCase     (		A	:						int       ):

		'''simple docstring'''


		_UpperCAmelCase							 =		abs(A       )
		return n if n < 10 else n % 10 + sum_of_digits(n // 10       )




def 	UpperCAmelCase     (		A	:						int       ):

		'''simple docstring'''


		return sum(int(A       ) for c in str(abs(A       )       )       )




def 	UpperCAmelCase     (		):

		'''simple docstring'''


		from collections.abc import Callable
		from timeit import timeit

		def benchmark_a_function(A	:						Callable     ,				A	:						int       ) -> None:
				_UpperCAmelCase							 =		f'{func.__name__}({value})'
				_UpperCAmelCase							 =		timeit(f'__main__.{call}'     ,				setup='import __main__'       )
				print(f'{call:56} = {func(A       )} -- {timing:.4f} seconds'       )

		for value in (26_2144, 1125_8999_0684_2624, 126_7650_6002_2822_9401_4967_0320_5376):
				for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact):
						benchmark_a_function(A     ,				A       )
				print()


if __name__ == "__main__":
					import doctest

					doctest.testmod()
					benchmark()


