# ==============================================================================
"""PASCAL : create Pascal's triangle (triangle of binomial terms)"""
# ==============================================================================
__author__  = "Philippe Blasi"
__version__ = "1.0" # use 'grid' function from ezCLI to format table
__date__    = "2026-03-06"
__usage__   = """
User input : <n> (where n:int > 0)
App output: Grid containing Pascal's triangle up to rank n"""
# ==============================================================================
from ezCLI import *
# ------------------------------------------------------------------------------
def facto(n:int) -> int:
  """return n! (iterative implementation)"""
  result = 1
  for i in range(1,n+1):
      result = result * i  # result *= result
  return result
# ------------------------------------------------------------------------------
def bino(n:int, p:int) -> int:
  """return the binomial coefficient C(n,p)"""
  return facto(n)//(facto(p)*facto(n-p))
# ------------------------------------------------------------------------------
def pascal(n:int) -> str:
  """return a grid containing Pascal's triangle up to rank n"""
  table = [[bino(row,col) for col in range(0,row+1)] for row in range(0, n+1)]; #inspect()
  return grid(table) # use the 'grid' function to format 'table' as a grid
# ------------------------------------------------------------------------------
def parser(command:str) ->str:
  """parse 'command' as integer 'n' before calling 'pascal(n)'"""
  n = parse(command); #inspect()
  assert type(n) is int and n >= 0, "<n> must be a  positive integer"
  return pascal(n)
# ==============================================================================
if __name__ == "__main__":
  userloop(parser, "Enter value for <n>")
# ==============================================================================
