






'''simple docstring'''







from queue import Queue
from typing import TYPE_CHECKING, Optional


if TYPE_CHECKING:
							from ..models.auto import AutoTokenizer




class 		UpperCamelCase__    :




		'''simple docstring'''





		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase       ):
								raise NotImplementedError()






		def 			UpperCamelCase_					(      self       ):
								raise NotImplementedError()




class 		UpperCamelCase__    (a			):




		'''simple docstring'''





		def __init__(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = False   ,**_lowerCAmelCase       ):
								lowerCamelCase__         =  tokenizer
								lowerCamelCase__         =  skip_prompt
								lowerCamelCase__         =  decode_kwargs

								# variables used in the streaming process
								lowerCamelCase__         =  []
								lowerCamelCase__         =  0
								lowerCamelCase__         =  True






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase       ):
								if len(value.shape       ) > 1 and value.shape[0] > 1:
														raise ValueError("""TextStreamer only supports batch size 1"""       )
								elif len(value.shape       ) > 1:
														lowerCamelCase__         =  value[0]

								if self.skip_prompt and self.next_tokens_are_prompt:
														lowerCamelCase__         =  False
														return

								# Add the new token to the cache and decodes the entire thing.
								self.token_cache.extend(value.tolist()       )
								lowerCamelCase__         =  self.tokenizer.decode(self.token_cache   ,**self.decode_kwargs       )

								# After the symbol for a new line, we flush the cache.
								if text.endswith("""\n"""       ):
														lowerCamelCase__         =  text[self.print_len :]
														lowerCamelCase__         =  []
														lowerCamelCase__         =  0
								# If the last token is a CJK character, we print the characters.
								elif len(_lowerCAmelCase       ) > 0 and self._is_chinese_char(ord(text[-1]       )       ):
														lowerCamelCase__         =  text[self.print_len :]
														self.print_len += len(_lowerCAmelCase       )
								# Otherwise, prints until the last space char (simple heuristic to avoid printing incomplete words,
								# which may change with the subsequent token -- there are probably smarter ways to do this!)
								else:
														lowerCamelCase__         =  text[self.print_len : text.rfind(""" """       ) + 1]
														self.print_len += len(_lowerCAmelCase       )

								self.on_finalized_text(_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								# Flush the cache, if it exists
								if len(self.token_cache       ) > 0:
														lowerCamelCase__         =  self.tokenizer.decode(self.token_cache   ,**self.decode_kwargs       )
														lowerCamelCase__         =  text[self.print_len :]
														lowerCamelCase__         =  []
														lowerCamelCase__         =  0
								else:
														lowerCamelCase__         =  """"""

								lowerCamelCase__         =  True
								self.on_finalized_text(_lowerCAmelCase   ,stream_end=_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = False       ):
								print(_lowerCAmelCase   ,flush=_lowerCAmelCase   ,end="""""" if not stream_end else None       )






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase       ):
								# This defines a "chinese character" as anything in the CJK Unicode block:
								#   https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block)
								#
								# Note that the CJK Unicode block is NOT all Japanese and Korean characters,
								# despite its name. The modern Korean Hangul alphabet is a different block,
								# as is Japanese Hiragana and Katakana. Those alphabets are used to write
								# space-separated words, so they are not treated specially and handled
								# like the all of the other languages.
								if (
								    (cp >= 0x4e_00 and cp <= 0x9f_ff)
								    or (cp >= 0x34_00 and cp <= 0x4d_bf)  #
								    or (cp >= 0x2_00_00 and cp <= 0x2_a6_df)  #
								    or (cp >= 0x2_a7_00 and cp <= 0x2_b7_3f)  #
								    or (cp >= 0x2_b7_40 and cp <= 0x2_b8_1f)  #
								    or (cp >= 0x2_b8_20 and cp <= 0x2_ce_af)  #
								    or (cp >= 0xf9_00 and cp <= 0xfa_ff)
								    or (cp >= 0x2_f8_00 and cp <= 0x2_fa_1f)  #
								):  #
														return True

								return False




class 		UpperCamelCase__    (a			):




		'''simple docstring'''





		def __init__(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = False   ,_lowerCAmelCase = None   ,**_lowerCAmelCase       ):
								super().__init__(_lowerCAmelCase   ,_lowerCAmelCase   ,**_lowerCAmelCase       )
								lowerCamelCase__         =  Queue()
								lowerCamelCase__         =  None
								lowerCamelCase__         =  timeout






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = False       ):
								self.text_queue.put(_lowerCAmelCase   ,timeout=self.timeout       )
								if stream_end:
														self.text_queue.put(self.stop_signal   ,timeout=self.timeout       )






		def __iter__(      self       ):
								return self






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.text_queue.get(timeout=self.timeout       )
								if value == self.stop_signal:
														raise StopIteration()
								else:
														return value









'''simple docstring'''







UpperCamelCase    :      Tuple  			=		[sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(10_00_00)]
def 						A__			(				__lowerCAmelCase		:			int						):
						lowerCamelCase__         =  0
						while number:
												# Increased Speed Slightly by checking every 5 digits together.
												sum_of_digits_squared += DIGITS_SQUARED[number % 10_0000]
												number //= 10_0000

						return sum_of_digits_squared


# There are 2 Chains made,
# One ends with 89 with the chain member 58 being the one which when declared first,
# there will be the least number of iterations for all the members to be checked.

# The other one ends with 1 and has only one element 1.

# So 58 and 1 are chosen to be declared at the starting.

# Changed dictionary to an array to quicken the solution
UpperCamelCase    :      list[bool | None]  			=		[None] * 10_00_00_00
UpperCamelCase    :      Tuple  			=		True
UpperCamelCase    :      Optional[int]  			=		False
def 						A__			(				__lowerCAmelCase		:			int						):

						if CHAINS[number - 1] is not None:
												return CHAINS[number - 1]  # type: ignore

						lowerCamelCase__         =  chain(next_number(__lowerCAmelCase						)						)
						lowerCamelCase__         =  number_chain

						while number < 1000_0000:
												lowerCamelCase__         =  number_chain
												number *= 10

						return number_chain
def 						A__			(				__lowerCAmelCase		:			int = 1000_0000						):
						for i in range(1   ,    __lowerCAmelCase						):
												if CHAINS[i] is None:
																		chain(i + 1						)

						return CHAINS[:number].count(__lowerCAmelCase						)


if __name__ == "__main__":
							import doctest

							doctest.testmod()
							print(F'{solution() = }')

