




'''simple docstring'''





def      __a     (       _UpperCamelCase:   int   )						->			None:

					"""simple docstring"""




					_snake_case							=				generate_pascal_triangle(_UpperCamelCase   )
					for row_idx in range(_UpperCamelCase   ):
										# Print left spaces
										for _ in range(num_rows - row_idx - 1   ):
															print(end=" "   )
										# Print row values
										for col_idx in range(row_idx + 1   ):
															if col_idx != row_idx:
																				print(triangle[row_idx][col_idx] ,						end=" "   )
															else:
																				print(triangle[row_idx][col_idx] ,						end=""   )
										print()





def      __a     (       _UpperCamelCase:   int   )						->			list[list[int]]:

					"""simple docstring"""




					if not isinstance(_UpperCamelCase ,						_UpperCamelCase   ):
										raise TypeError("The input value of 'num_rows' should be 'int'"   )

					if num_rows == 0:
										return []
					elif num_rows < 0:
										raise ValueError(
										    "The input value of 'num_rows' should be greater than or equal to 0"   )

					_snake_case							=				[]
					for current_row_idx in range(_UpperCamelCase   ):
										_snake_case							=				populate_current_row(_UpperCamelCase ,						_UpperCamelCase   )
										triangle.append(_UpperCamelCase   )
					return triangle





def      __a     (       _UpperCamelCase:   list[list[int]] ,						_UpperCamelCase:   int   )						->			list[int]:

					"""simple docstring"""




					_snake_case							=				[-1] * (current_row_idx + 1)
					# first and last elements of current row are equal to 1
					_snake_case							, _snake_case							=				1, 1
					for current_col_idx in range(1 ,						_UpperCamelCase   ):
										calculate_current_element(
										    _UpperCamelCase ,						_UpperCamelCase ,						_UpperCamelCase ,						_UpperCamelCase   )
					return current_row





def      __a     (       _UpperCamelCase:   list[list[int]] ,						_UpperCamelCase:   list[int] ,						_UpperCamelCase:   int ,						_UpperCamelCase:   int ,						)						->			None:

					"""simple docstring"""




					_snake_case							=				triangle[current_row_idx - 1][current_col_idx - 1]
					_snake_case							=				triangle[current_row_idx - 1][current_col_idx]
					_snake_case							=				above_to_left_elt + above_to_right_elt





def      __a     (       _UpperCamelCase:   int   )						->			list[list[int]]:

					"""simple docstring"""




					if not isinstance(_UpperCamelCase ,						_UpperCamelCase   ):
										raise TypeError("The input value of 'num_rows' should be 'int'"   )

					if num_rows == 0:
										return []
					elif num_rows < 0:
										raise ValueError(
										    "The input value of 'num_rows' should be greater than or equal to 0"   )

					_snake_case							=				[[1]]

					for row_index in range(1 ,						_UpperCamelCase   ):
										_snake_case							=				[0] + result[-1] + [0]
										_snake_case							=				row_index + 1
										# Calculate the number of distinct elements in a row
										_snake_case							=				sum(divmod(_UpperCamelCase ,						2   )   )
										_snake_case							=				[
										    temp_row[i - 1] + temp_row[i] for i in range(1 ,						distinct_elements + 1   )
										]
										_snake_case							=				row_first_half[: (row_index + 1) // 2]
										row_second_half.reverse()
										_snake_case							=				row_first_half + row_second_half
										result.append(_UpperCamelCase   )

					return result





def      __a     (       )						->			None:

					"""simple docstring"""




					from collections.abc import Callable
					from timeit import timeit

					def benchmark_a_function(_UpperCamelCase:   Callable ,						_UpperCamelCase:   int   ) -> None:
										_snake_case							=				F"""{func.__name__}({value})"""
										_snake_case							=				timeit(F"""__main__.{call}""" ,						setup="import __main__"   )
										# print(f"{call:38} = {func(value)} -- {timing:.4f} seconds")
										print(F"""{call:38} -- {timing:.4f} seconds"""   )

					for value in range(15   ):  # (1, 7, 14):
										for func in (generate_pascal_triangle, generate_pascal_triangle_optimized):
															benchmark_a_function(_UpperCamelCase ,						_UpperCamelCase   )
										print()


if __name__ == "__main__":
			import doctest

			doctest.testmod()
			benchmark()








'''simple docstring'''





from __future__ import annotations

from collections import namedtuple
from dataclasses import dataclass

@dataclass
class        _a    :
     SCREAMING_SNAKE_CASE_							:   int
     SCREAMING_SNAKE_CASE_							:   TreeNode | None       =						None
     SCREAMING_SNAKE_CASE_							:   TreeNode | None       =						None







UpperCamelCase_						:      Any       =						namedtuple('''CoinsDistribResult''', '''moves excess''')





def      __a     (       _UpperCamelCase:   TreeNode | None   )						->			int:

     """simple docstring"""




     if root is None:
          return 0

     # Validation
     def count_nodes(_UpperCamelCase:   TreeNode | None   ) -> int:
          if node is None:
               return 0

          return count_nodes(node.left   ) + count_nodes(node.right   ) + 1

     def count_coins(_UpperCamelCase:   TreeNode | None   ) -> int:
          if node is None:
               return 0

          return count_coins(node.left   ) + count_coins(node.right   ) + node.data

     if count_nodes(_UpperCamelCase   ) != count_coins(_UpperCamelCase   ):
          raise ValueError("The nodes number should be same as the number of coins"   )

     # Main calculation
     def get_distrib(_UpperCamelCase:   TreeNode | None   ) -> CoinsDistribResult:

          if node is None:
               return CoinsDistribResult(0 ,						1   )

          _snake_case							, _snake_case							=				get_distrib(node.left   )
          _snake_case							, _snake_case							=				get_distrib(node.right   )

          _snake_case							=				1 - left_distrib_excess
          _snake_case							=				1 - right_distrib_excess

          _snake_case							=				(
              left_distrib_moves
              + right_distrib_moves
              + abs(_UpperCamelCase   )
              + abs(_UpperCamelCase   )
          )
          _snake_case							=				node.data - coins_to_left - coins_to_right

          return CoinsDistribResult(_UpperCamelCase ,						_UpperCamelCase   )

     return get_distrib(_UpperCamelCase   )[0]


if __name__ == "__main__":
   import doctest

   doctest.testmod()


