# ==============================================================================
"""FIBO : compute the n first terms of the Fibonacci sequence"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "1.0" # use iterative implementation of the Fibonacci terms
__date__    = "2022-11-12"
__usage__   = """
User input: <n> (where n:int >= 0)
App output: sequence of the n first Fibonacci terms"""
# ==============================================================================
from ezCLI import *
# ------------------------------------------------------------------------------
def fibo(n:int) -> int:
  """return the nth term of the Fibonacci sequence (iterative version)"""
  u_k_moins_2 = 0  # u_0
  u_k_moins_1 = 1  # u_1

  for i in range(n):
    #print(f"u_k_moins_2={u_k_moins_2} u_k_moins_1={u_k_moins_1}")
    u_k_moins_2, u_k_moins_1 = u_k_moins_1, u_k_moins_1 + u_k_moins_2

  return u_k_moins_2
    
# ------------------------------------------------------------------------------
def fibos(n:int) -> str:
  """return a string containing the 'n' first terms of the Fibonacci sequence"""
  lines = [f"fibo({p}) = {fibo(p)}" for p in range(0,n+1)]; #inspect()
  return '\n'.join(lines) # join all lines into a single multi-line string
# ------------------------------------------------------------------------------
def parser(command:str) -> str:
  """parse 'command' as integer 'n' before calling 'fibos(n)'"""
  n = parse(command); #inspect()
  assert type(n) is int and n >= 0, "<n> must be a positive integer"
  return fibos(n)
# ==============================================================================
if __name__ == "__main__":
  userloop(parser, "Enter value for <n>")
# ==============================================================================
