'''simple docstring'''

from abc import ABC, abstractmethod
from typing import List, Optional


class 					_lowercase						(   lowerCAmelCase ):
      '''simple docstring'''


      def __init__(					self				)      ->      Optional[Any]:



         '''simple docstring'''



         self.test()




      def 		lowerCAmelCase__       (					self				)      ->      int:



         '''simple docstring'''



         UpperCAmelCase__				:					Optional[int] 					=	0
         UpperCAmelCase__				:					List[Any] 					=	False
         while not completed:
            if counter == 1:
               self.reset()
            UpperCAmelCase__				:					Optional[int] 					=	self.advance()
            if not self.does_advance(lowerCamelCase_				):
               raise Exception(
                   '''Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.'''				)

            UpperCAmelCase__					,   UpperCAmelCase__					,   UpperCAmelCase__				:					List[str] 					=	self.update(lowerCamelCase_				)
            counter += 1

            if counter > 10000:
               raise Exception('''update() does not fulfill the constraint.'''				)

         if self.remaining() != 0:
            raise Exception('''Custom Constraint is not defined correctly.'''				)




      @abstractmethod
      def 		lowerCAmelCase__       (					self				)      ->      Any:



         '''simple docstring'''



         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)




      @abstractmethod
      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_				)      ->      List[Any]:



         '''simple docstring'''



         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)




      @abstractmethod
      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_				)      ->      Dict:



         '''simple docstring'''



         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)




      @abstractmethod
      def 		lowerCAmelCase__       (					self				)      ->      List[Any]:



         '''simple docstring'''



         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)




      @abstractmethod
      def 		lowerCAmelCase__       (					self				)      ->      Optional[int]:



         '''simple docstring'''



         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)




      @abstractmethod
      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_=False				)      ->      int:



         '''simple docstring'''



         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)




class 					_lowercase						(   lowerCAmelCase ):
      '''simple docstring'''


      def __init__(					self       ,lowerCamelCase_				)      ->      Tuple:



         '''simple docstring'''



         super(lowerCamelCase_       ,self				).__init__()

         if not isinstance(lowerCamelCase_       ,lowerCamelCase_				) or len(lowerCamelCase_				) == 0:
            raise ValueError(f'''`token_ids` has to be a non-empty list, but is {token_ids}.'''				)
         if any((not isinstance(lowerCamelCase_       ,lowerCamelCase_				) or token_id < 0) for token_id in token_ids				):
            raise ValueError(f'''Each list in `token_ids` has to be a list of positive integers, but is {token_ids}.'''				)

         UpperCAmelCase__				:					int 					=	token_ids

         UpperCAmelCase__				:					int 					=	len(self.token_ids				)
         UpperCAmelCase__				:					Dict 					=	-1  # the index of the currently fulfilled step
         UpperCAmelCase__				:					Optional[Any] 					=	False




      def 		lowerCAmelCase__       (					self				)      ->      Optional[Any]:



         '''simple docstring'''



         if self.completed:
            return None
         return self.token_ids[self.fulfilled_idx + 1]




      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_				)      ->      Tuple:



         '''simple docstring'''



         if not isinstance(lowerCamelCase_       ,lowerCamelCase_				):
            raise ValueError(f'''`token_id` has to be an `int`, but is {token_id} of type {type(lowerCamelCase_				)}'''				)

         if self.completed:
            return False

         return token_id == self.token_ids[self.fulfilled_idx + 1]




      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_				)      ->      Optional[int]:



         '''simple docstring'''



         if not isinstance(lowerCamelCase_       ,lowerCamelCase_				):
            raise ValueError(f'''`token_id` has to be an `int`, but is {token_id} of type {type(lowerCamelCase_				)}'''				)

         UpperCAmelCase__				:					Optional[Any] 					=	False
         UpperCAmelCase__				:					Dict 					=	False
         UpperCAmelCase__				:					Optional[Any] 					=	False

         if self.does_advance(lowerCamelCase_				):
            self.fulfilled_idx += 1
            UpperCAmelCase__				:					str 					=	True
            if self.fulfilled_idx == (self.seqlen - 1):
               UpperCAmelCase__				:					Dict 					=	True
            UpperCAmelCase__				:					Any 					=	completed
         else:
            # failed to make progress.
            UpperCAmelCase__				:					Any 					=	True
            self.reset()
         return stepped, completed, reset




      def 		lowerCAmelCase__       (					self				)      ->      str:



         '''simple docstring'''



         UpperCAmelCase__				:					Tuple 					=	False
         UpperCAmelCase__				:					str 					=	0




      def 		lowerCAmelCase__       (					self				)      ->      Tuple:



         '''simple docstring'''



         return self.seqlen - (self.fulfilled_idx + 1)




      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_=False				)      ->      int:



         '''simple docstring'''



         UpperCAmelCase__				:					Tuple 					=	PhrasalConstraint(self.token_ids				)

         if stateful:
            UpperCAmelCase__				:					Tuple 					=	self.seqlen
            UpperCAmelCase__				:					List[Any] 					=	self.fulfilled_idx
            UpperCAmelCase__				:					Tuple 					=	self.completed

         return new_constraint




class 					_lowercase						:
      '''simple docstring'''


      def __init__(					self       ,lowerCamelCase_       ,lowerCamelCase_=True				)      ->      int:



         '''simple docstring'''



         UpperCAmelCase__				:					List[str] 					=	max([len(lowerCamelCase_				) for one in nested_token_ids]				)

         UpperCAmelCase__				:					Dict 					=	{}
         for token_ids in nested_token_ids:
            UpperCAmelCase__				:					List[str] 					=	root
            for tidx, token_id in enumerate(lowerCamelCase_				):
               if token_id not in level:
                  UpperCAmelCase__				:					str 					=	{}

               UpperCAmelCase__				:					str 					=	level[token_id]

         if no_subsets and self.has_subsets(lowerCamelCase_       ,lowerCamelCase_				):
            raise ValueError(
                '''Each list in `nested_token_ids` can\'t be a complete subset of another list, but is'''
                f''' {nested_token_ids}.'''				)

         UpperCAmelCase__				:					Optional[int] 					=	root




      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_				)      ->      List[str]:



         '''simple docstring'''



         UpperCAmelCase__				:					Optional[Any] 					=	self.trie

         for current_token in current_seq:
            UpperCAmelCase__				:					Optional[int] 					=	start[current_token]

         UpperCAmelCase__				:					str 					=	list(start.keys()				)

         return next_tokens




      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_				)      ->      Dict:



         '''simple docstring'''



         UpperCAmelCase__				:					List[Any] 					=	self.next_tokens(lowerCamelCase_				)

         return len(lowerCamelCase_				) == 0




      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_				)      ->      Any:



         '''simple docstring'''



         UpperCAmelCase__				:					Optional[int] 					=	list(root.values()				)
         if len(lowerCamelCase_				) == 0:
            return 1
         else:
            return sum([self.count_leaves(lowerCamelCase_				) for nn in next_nodes]				)




      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_       ,lowerCamelCase_				)      ->      List[Any]:



         '''simple docstring'''



         UpperCAmelCase__				:					Optional[Any] 					=	self.count_leaves(lowerCamelCase_				)
         return len(lowerCamelCase_				) != leaf_count




class 					_lowercase						(   lowerCAmelCase ):
      '''simple docstring'''


      def __init__(					self       ,lowerCamelCase_				)      ->      List[Any]:



         '''simple docstring'''



         super(lowerCamelCase_       ,self				).__init__()

         if not isinstance(lowerCamelCase_       ,lowerCamelCase_				) or len(lowerCamelCase_				) == 0:
            raise ValueError(f'''`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}.'''				)
         if any(not isinstance(lowerCamelCase_       ,lowerCamelCase_				) for token_ids in nested_token_ids				):
            raise ValueError(f'''`nested_token_ids` has to be a list of lists, but is {nested_token_ids}.'''				)
         if any(
             any((not isinstance(lowerCamelCase_       ,lowerCamelCase_				) or token_id < 0) for token_id in token_ids				)
             for token_ids in nested_token_ids				):
            raise ValueError(
                f'''Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}.'''				)

         UpperCAmelCase__				:					Optional[int] 					=	DisjunctiveTrie(lowerCamelCase_				)
         UpperCAmelCase__				:					Union[str, Any] 					=	nested_token_ids

         UpperCAmelCase__				:					Optional[int] 					=	self.trie.max_height
         UpperCAmelCase__				:					int 					=	[]
         UpperCAmelCase__				:					int 					=	False




      def 		lowerCAmelCase__       (					self				)      ->      Any:



         '''simple docstring'''



         UpperCAmelCase__				:					Optional[int] 					=	self.trie.next_tokens(self.current_seq				)

         if len(lowerCamelCase_				) == 0:
            return None
         else:
            return token_list




      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_				)      ->      Dict:



         '''simple docstring'''



         if not isinstance(lowerCamelCase_       ,lowerCamelCase_				):
            raise ValueError(f'''`token_id` is supposed to be type `int`, but is {token_id} of type {type(lowerCamelCase_				)}'''				)

         UpperCAmelCase__				:					Optional[Any] 					=	self.trie.next_tokens(self.current_seq				)

         return token_id in next_tokens




      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_				)      ->      List[str]:



         '''simple docstring'''



         if not isinstance(lowerCamelCase_       ,lowerCamelCase_				):
            raise ValueError(f'''`token_id` is supposed to be type `int`, but is {token_id} of type {type(lowerCamelCase_				)}'''				)

         UpperCAmelCase__				:					Optional[int] 					=	False
         UpperCAmelCase__				:					Union[str, Any] 					=	False
         UpperCAmelCase__				:					Union[str, Any] 					=	False

         if self.does_advance(lowerCamelCase_				):
            self.current_seq.append(lowerCamelCase_				)
            UpperCAmelCase__				:					int 					=	True
         else:
            UpperCAmelCase__				:					List[Any] 					=	True
            self.reset()

         UpperCAmelCase__				:					Tuple 					=	self.trie.reached_leaf(self.current_seq				)
         UpperCAmelCase__				:					int 					=	completed

         return stepped, completed, reset




      def 		lowerCAmelCase__       (					self				)      ->      Tuple:



         '''simple docstring'''



         UpperCAmelCase__				:					str 					=	False
         UpperCAmelCase__				:					List[Any] 					=	[]




      def 		lowerCAmelCase__       (					self				)      ->      Union[str, Any]:



         '''simple docstring'''



         if self.completed:
            # since this can be completed without reaching max height
            return 0
         else:
            return self.seqlen - len(self.current_seq				)




      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_=False				)      ->      int:



         '''simple docstring'''



         UpperCAmelCase__				:					int 					=	DisjunctiveConstraint(self.token_ids				)

         if stateful:
            UpperCAmelCase__				:					List[Any] 					=	self.seqlen
            UpperCAmelCase__				:					Optional[Any] 					=	self.current_seq
            UpperCAmelCase__				:					Dict 					=	self.completed

         return new_constraint




class 					_lowercase						:
      '''simple docstring'''


      def __init__(					self       ,lowerCamelCase_				)      ->      Optional[int]:



         '''simple docstring'''



         UpperCAmelCase__				:					List[str] 					=	constraints

         # max # of steps required to fulfill a given constraint
         UpperCAmelCase__				:					Dict 					=	max([c.seqlen for c in constraints]				)
         UpperCAmelCase__				:					int 					=	len(lowerCamelCase_				)
         UpperCAmelCase__				:					str 					=	False

         self.init_state()




      def 		lowerCAmelCase__       (					self				)      ->      int:



         '''simple docstring'''



         UpperCAmelCase__				:					Optional[Any] 					=	[]
         UpperCAmelCase__				:					int 					=	None
         UpperCAmelCase__				:					List[Any] 					=	[constraint.copy(stateful=lowerCamelCase_				) for constraint in self.constraints]




      def 		lowerCAmelCase__       (					self				)      ->      List[str]:



         '''simple docstring'''



         UpperCAmelCase__				:					Optional[Any] 					=	0
         if self.inprogress_constraint:
            # extra points for having a constraint mid-fulfilled
            add += self.max_seqlen - self.inprogress_constraint.remaining()

         return (len(self.complete_constraints				) * self.max_seqlen) + add




      def 		lowerCAmelCase__       (					self				)      ->      Tuple:



         '''simple docstring'''



         UpperCAmelCase__				:					Union[str, Any] 					=	[]
         if self.inprogress_constraint is None:
            for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
               UpperCAmelCase__				:					Tuple 					=	constraint.advance()
               if isinstance(lowerCamelCase_       ,lowerCamelCase_				):
                  token_list.append(lowerCamelCase_				)
               elif isinstance(lowerCamelCase_       ,lowerCamelCase_				):
                  token_list.extend(lowerCamelCase_				)
         else:
            UpperCAmelCase__				:					Optional[int] 					=	self.inprogress_constraint.advance()
            if isinstance(lowerCamelCase_       ,lowerCamelCase_				):
               token_list.append(lowerCamelCase_				)
            elif isinstance(lowerCamelCase_       ,lowerCamelCase_				):
               token_list.extend(lowerCamelCase_				)

         if len(lowerCamelCase_				) == 0:
            return None
         else:
            return token_list




      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_				)      ->      List[Any]:



         '''simple docstring'''



         self.init_state()

         if token_ids is not None:
            for token in token_ids:
               # completes or steps **one** constraint
               UpperCAmelCase__					,   UpperCAmelCase__				:					Any 					=	self.add(lowerCamelCase_				)

               # the entire list of constraints are fulfilled
               if self.completed:
                  break




      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_				)      ->      Any:



         '''simple docstring'''



         if not isinstance(lowerCamelCase_       ,lowerCamelCase_				):
            raise ValueError(f'''`token_id` should be an `int`, but is `{token_id}`.'''				)

         UpperCAmelCase__					,   UpperCAmelCase__				:					Any 					=	False, False

         if self.completed:
            UpperCAmelCase__				:					str 					=	True
            UpperCAmelCase__				:					Optional[Any] 					=	False
            return complete, stepped

         if self.inprogress_constraint is not None:
            # In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current
            # job, simply update the state

            UpperCAmelCase__					,   UpperCAmelCase__					,   UpperCAmelCase__				:					Dict 					=	self.inprogress_constraint.update(lowerCamelCase_				)
            if reset:
               # 1. If the next token breaks the progress, then we must restart.
               #     e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books".

               #     But that doesn't mean we self.init_state(), since we only reset the state for this particular
               #     constraint, not the full list of constraints.

               self.pending_constraints.append(self.inprogress_constraint.copy(stateful=lowerCamelCase_				)				)
               UpperCAmelCase__				:					List[str] 					=	None

            if complete:
               # 2. If the next token completes the constraint, move it to completed list, set
               #     inprogress to None. If there are no pending constraints either, then this full list of constraints
               #     is complete.

               self.complete_constraints.append(self.inprogress_constraint				)
               UpperCAmelCase__				:					List[Any] 					=	None

               if len(self.pending_constraints				) == 0:
                  # we're done!
                  UpperCAmelCase__				:					List[Any] 					=	True

         else:
            # Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list
            # of constraints?

            for cidx, pending_constraint in enumerate(self.pending_constraints				):
               if pending_constraint.does_advance(lowerCamelCase_				):
                  UpperCAmelCase__					,   UpperCAmelCase__					,   UpperCAmelCase__				:					Optional[int] 					=	pending_constraint.update(lowerCamelCase_				)

                  if not stepped:
                     raise Exception(
                         '''`constraint.update(token_id)` is not yielding incremental progress, '''
                         '''even though `constraint.does_advance(token_id)` is true.'''				)

                  if complete:
                     self.complete_constraints.append(lowerCamelCase_				)
                     UpperCAmelCase__				:					Any 					=	None

                  if not complete and stepped:
                     UpperCAmelCase__				:					Optional[int] 					=	pending_constraint

                  if complete or stepped:
                     # If we made any progress at all, then it's at least not a "pending constraint".

                     UpperCAmelCase__				:					str 					=	(
                         self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :]
                     )

                     if len(self.pending_constraints				) == 0 and self.inprogress_constraint is None:
                        # If there's no longer any pending after this and no inprogress either, then we must be
                        # complete.

                        UpperCAmelCase__				:					Optional[Any] 					=	True

                     break  # prevent accidentally stepping through multiple constraints with just one token.

         return complete, stepped




      def 		lowerCAmelCase__       (					self       ,lowerCamelCase_=True				)      ->      List[Any]:



         '''simple docstring'''



         UpperCAmelCase__				:					Tuple 					=	ConstraintListState(self.constraints				)  # we actually never though self.constraints objects
         # throughout this process. So it's at initialization state.

         if stateful:
            UpperCAmelCase__				:					Tuple 					=	[
                constraint.copy(stateful=lowerCamelCase_				) for constraint in self.complete_constraints
            ]
            if self.inprogress_constraint is not None:
               UpperCAmelCase__				:					List[Any] 					=	self.inprogress_constraint.copy(stateful=lowerCamelCase_				)
            UpperCAmelCase__				:					Tuple 					=	[constraint.copy() for constraint in self.pending_constraints]

         return new_state





'''simple docstring'''

from math import factorial, radians







def __UpperCamelCase( _A       :					float	,      _A       :					int = 18	,      _A       :					int = 10     ):
   '''simple docstring'''
   UpperCAmelCase__				:					int 					=	angle_in_degrees - ((angle_in_degrees // 3_6_0.0) * 3_6_0.0)

   # Converting from degrees to radians
   UpperCAmelCase__				:					Dict 					=	radians(_A     )

   UpperCAmelCase__				:					Union[str, Any] 					=	angle_in_radians
   UpperCAmelCase__				:					List[str] 					=	3
   UpperCAmelCase__				:					int 					=	-1

   for _ in range(_A     ):
      result += (b * (angle_in_radians**a)) / factorial(_A     )

      UpperCAmelCase__				:					List[str] 					=	-b  # One positive term and the next will be negative and so on...
      a += 2  # Increased by 2 for every term.

   return round(_A	,      _A     )


if __name__ == "__main__":
  __import__('doctest').testmod()




