# ==============================================================================
"""BINO : compute the binomial coefficient C(n,p)"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "2.0" # implement truncated factorial function for optimization
__date__    = "2022-11-12"
__usage__   = """
User input : <n>,<p> (where n:int >= 0, p:int >= 0)
App output : binomial coefficient C(n,p)"""
# ==============================================================================
from ezCLI import *
# ------------------------------------------------------------------------------
def facto(n:int, m:int=0) -> int:
  """compute a truncated factorial term = m * (m+1) * (m+2) * ... * n"""
  value = 1
  for p in range(m+1, n+1):
    value *= p
  return value
# ------------------------------------------------------------------------------
def bino(n:int, p:int) -> int:
  """compute the binomial coefficient C(n,p) using truncated factorial"""
  m = max(p, n-p); return facto(n, m) // facto(n-m)
# ------------------------------------------------------------------------------
def parser(command:str) -> str:
  """parse 'command' as 'n,p' before calling 'bino(n,p)'"""
  command = convert(command); #inspect()
  assert type(command) is tuple and len(command) == 2, "invalid syntax"
  n, p = command; #inspect()
  assert type(n) is int and n >= 0, "<n> must be a positive integer"
  assert type(p) is int and p >= 0, "<p> must be a positive integer"
  if p > n: n, p = p, n # swap 'n' and 'p'
  return f"bino({n},{p}) = {bino(n,p)}"
# ==============================================================================
if __name__ == '__main__':
  userloop(parser, "Enter <n>,<p>")
# ==============================================================================
