

from __future__ import annotations

from PIL import Image

# Define glider example
_SCREAMING_SNAKE_CASE					:  Tuple             =      [
    [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
_SCREAMING_SNAKE_CASE					:  Optional[Any]             =      [[0, 1, 0], [0, 1, 0], [0, 1, 0]]
def     UpperCAmelCase_       (						_A    ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__   =   []
     for i in range(len(_A    )    ):
          SCREAMING_SNAKE_CASE__   =   []
          for j in range(len(cells[i]    )    ):
               # Get the number of live neighbours
               SCREAMING_SNAKE_CASE__   =   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.
               SCREAMING_SNAKE_CASE__   =   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				,		_A    ):





     '''simple docstring'''




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

          # Save cells to image
          for x in range(len(_A    )    ):
               for y in range(len(cells[0]    )    ):
                    SCREAMING_SNAKE_CASE__   =   2_55 - cells[y][x] * 2_55
                    SCREAMING_SNAKE_CASE__   =   (colour, colour, colour)

        # Save image
          images.append(_A    )
          SCREAMING_SNAKE_CASE__   =   new_generation(_A    )
     return images


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





import doctest
from collections import deque

import numpy as np






class 	UpperCAmelCase__		:






  """simple docstring"""


  def __init__(							self						:		List[Any] )		->							None:
       SCREAMING_SNAKE_CASE__   =   [2, 1, 2, -1]
       SCREAMING_SNAKE_CASE__   =   [1, 2, 3, 4]







  def 				lowercase_		(							self						:		Optional[int] )		->							list[float]:
       SCREAMING_SNAKE_CASE__   =   len(self.first_signal )
       SCREAMING_SNAKE_CASE__   =   len(self.second_signal )

       SCREAMING_SNAKE_CASE__   =   max(__lowerCamelCase     ,       __lowerCamelCase )

       # create a zero matrix of max_length x max_length
       SCREAMING_SNAKE_CASE__   =   [[0] * max_length for i in range(__lowerCamelCase )]

       # fills the smaller signal with zeros to make both signals of same length
       if length_first_signal < length_second_signal:
            self.first_signal += [0] * (max_length - length_first_signal)
       elif length_first_signal > length_second_signal:
            self.second_signal += [0] * (max_length - length_second_signal)
       for i in range(__lowerCamelCase ):
            SCREAMING_SNAKE_CASE__   =   deque(self.second_signal )
            rotated_signal.rotate(__lowerCamelCase )
            for j, item in enumerate(__lowerCamelCase ):
                 matrix[i][j] += item

        # multiply the matrix with the first signal
       SCREAMING_SNAKE_CASE__   =   np.matmul(np.transpose(__lowerCamelCase )     ,       np.transpose(self.first_signal ) )

       # rounding-off to two decimal places
       return [round(__lowerCamelCase     ,       2 ) for i in final_signal]


if __name__ == "__main__":
       doctest.testmod()


