# ==============================================================================
"""FACTO : compute the sequence of factorial terms from 0! to n!"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "1.0" # use iterative implementation for the factorial function
__date__    = "2022-11-12"
__usage__   = """
User input: <n> (where n:int >= 0)
App output: sequence of factorial terms from 0! to n!"""
# ==============================================================================
from ezCLI import *
# ------------------------------------------------------------------------------
def facto(n:int) -> int:
  """return n! (iterative implementation)"""
  value = 1
  for p in range(1, n+1):
    value *= p
  return value
# ------------------------------------------------------------------------------
def factos(n:int) -> str:
  """return a string containing the 'n' first factorial numbers"""
  lines = ''
  for p in range(0, n+1):
    lines += f"{p}! = {facto(p)}\n"
  return lines.strip() # remove trailing newline character
  # Alternative implementation using list comprehension
  # return '\n'.join(f"{p}! = {facto(p)}" for p in range(n+1))
# ------------------------------------------------------------------------------
def parser(command:str) -> str:
  """parse 'command' as integer 'n' before calling 'factos(n)'"""
  n = parse(command)
  assert type(n) is int and n >= 0, "<n> must be a positive integer"
  return factos(n)
# ==============================================================================
if __name__ == "__main__":
  userloop(parser, "Enter value for <n>")
# ==============================================================================
