'''simple docstring'''




import builtins
import sys

from ...utils.imports import _is_package_available
from . import cursor, input
from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor
from .keymap import KEYMAP


__UpperCAmelCase     							=False
try:
  __UpperCAmelCase     							=_is_package_available("google.colab")
except ModuleNotFoundError:
  pass




@input.register
class     a__      :






     def __init__( self		:		Dict  ,				a		:		str = None  ,				a		:		list = []    ):





         """simple docstring"""



         __lowerCamelCase		=  0
         __lowerCamelCase		=  choices
         __lowerCamelCase		=  prompt
         if sys.platform == "win32":
             __lowerCamelCase		=  '''*'''
         else:
             __lowerCamelCase		=  '''➔ '''






     def    SCREAMING_SNAKE_CASE__   ( self		:		str  ,				a		:		int  ,				a		:		str = ""    ):





         """simple docstring"""



         if sys.platform != "win32":
             writeColor(self.choices[index]  ,				32  ,				a    )
         else:
             forceWrite(self.choices[index]  ,				a    )






     def    SCREAMING_SNAKE_CASE__   ( self		:		List[Any]  ,				a		:		int    ):





         """simple docstring"""



         if index == self.position:
             forceWrite(f""" {self.arrow_char} """    )
             self.write_choice(a    )
         else:
             forceWrite(f"""    {self.choices[index]}"""    )
         reset_cursor()






     def    SCREAMING_SNAKE_CASE__   ( self		:		Any  ,				a		:		Direction  ,				a		:		int = 1    ):





         """simple docstring"""



         __lowerCamelCase		=  self.position
         if direction == Direction.DOWN:
             if self.position + 1 >= len(self.choices    ):
                 return
             self.position += num_spaces
         else:
             if self.position - 1 < 0:
                 return
             self.position -= num_spaces
         clear_line()
         self.print_choice(a    )
         move_cursor(a  ,				direction.name    )
         self.print_choice(self.position    )






     @input.mark(KEYMAP['''up''']    )
     def    SCREAMING_SNAKE_CASE__   ( self		:		Union[str, Any]    ):





         """simple docstring"""



         self.move_direction(Direction.UP    )






     @input.mark(KEYMAP['''down''']    )
     def    SCREAMING_SNAKE_CASE__   ( self		:		Union[str, Any]    ):





         """simple docstring"""



         self.move_direction(Direction.DOWN    )






     @input.mark(KEYMAP['''newline''']    )
     def    SCREAMING_SNAKE_CASE__   ( self		:		List[str]    ):





         """simple docstring"""



         move_cursor(len(self.choices    ) - self.position  ,				'''DOWN'''    )
         return self.position






     @input.mark(KEYMAP['''interrupt''']    )
     def    SCREAMING_SNAKE_CASE__   ( self		:		Tuple    ):





         """simple docstring"""



         move_cursor(len(self.choices    ) - self.position  ,				'''DOWN'''    )
         raise KeyboardInterrupt






     @input.mark_multiple(*[KEYMAP[str(a    )] for number in range(10    )]    )
     def    SCREAMING_SNAKE_CASE__   ( self		:		Dict    ):





         """simple docstring"""



         __lowerCamelCase		=  int(chr(self.current_selection    )    )
         __lowerCamelCase		=  index - self.position
         if index == self.position:
             return
         if index < len(self.choices    ):
             if self.position > index:
                 self.move_direction(Direction.UP  ,				-movement    )
             elif self.position < index:
                 self.move_direction(Direction.DOWN  ,				a    )
             else:
                 return
         else:
             return






     def    SCREAMING_SNAKE_CASE__   ( self		:		List[str]  ,				a		:		int = 0    ):





         """simple docstring"""



         if self.prompt:
             linebreak()
             forceWrite(self.prompt  ,				'''\n'''    )
             if in_colab:
                 forceWrite('''Please input a choice index (starting from 0), and press enter'''  ,				'''\n'''    )
             else:
                 forceWrite('''Please select a choice using the arrow or number keys, and selecting with enter'''  ,				'''\n'''    )
         __lowerCamelCase		=  default_choice
         for i in range(len(self.choices    )    ):
             self.print_choice(a    )
             forceWrite('''\n'''    )
         move_cursor(len(self.choices    ) - self.position  ,				'''UP'''    )
         with cursor.hide():
             while True:
                 if in_colab:
                     try:
                         __lowerCamelCase		=  int(builtins.input()    )
                     except ValueError:
                         __lowerCamelCase		=  default_choice
                 else:
                     __lowerCamelCase		=  self.handle_input()
                 if choice is not None:
                     reset_cursor()
                     for _ in range(len(self.choices    ) + 1    ):
                         move_cursor(1  ,				'''UP'''    )
                         clear_line()
                     self.write_choice(a  ,				'''\n'''    )
                     return choice




"""simple docstring"""







def    UpperCAmelCase__       (lowerCAmelCase_   ):




					'''simple docstring'''


					if upper_limit < 0:
										raise ValueError("Limit for the Catalan sequence must be ≥ 0"   )

					__SCREAMING_SNAKE_CASE						   =				[0] * (upper_limit + 1)

					# Base case: C(0) = C(1) = 1
					__SCREAMING_SNAKE_CASE						   =				1
					if upper_limit > 0:
										__SCREAMING_SNAKE_CASE						   =				1

					# Recurrence relation: C(i) = sum(C(j).C(i-j-1)), from j = 0 to i
					for i in range(2  ,						upper_limit + 1   ):
										for j in range(lowerCAmelCase_   ):
															catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1]

					return catalan_list


if __name__ == "__main__":
		print('''\n********* Catalan Numbers Using Dynamic Programming ************\n''')
		print('''\n*** Enter -1 at any time to quit ***''')
		print('''\nEnter the upper limit (≥ 0) for the Catalan number sequence: ''', end='''''')
		try:
				while True:
						a__				:							List[str]						=     int(input().strip())
						if N < 0:
								print('''\n********* Goodbye!! ************''')
								break
						else:
								print(F"The Catalan numbers from 0 through {N} are:")
								print(catalan_numbers(N))
								print('''Try another upper limit for the sequence: ''', end='''''')
		except (NameError, ValueError):
				print('''\n********* Invalid input, goodbye! ************\n''')

		import doctest

		doctest.testmod()
