






import unittest

import numpy as np
import requests

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
   import torch

   from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
   snake_case     					=		False

if is_vision_available():
   from PIL import Image

   from transformers import PixaStructImageProcessor


class 			SCREAMING_SNAKE_CASE      (      unittest.TestCase       ):
     '''simple docstring'''


     def __init__(					self							:						str       ,			UpperCAmelCase_							:						Union[str, Any]       ,			UpperCAmelCase_							:						Dict=7       ,			UpperCAmelCase_							:						List[Any]=3       ,			UpperCAmelCase_							:						str=18       ,			UpperCAmelCase_							:						str=30       ,			UpperCAmelCase_							:						Optional[int]=400       ,			UpperCAmelCase_							:						int=None       ,			UpperCAmelCase_							:						Tuple=True       ,			UpperCAmelCase_							:						Optional[int]=True       ,			UpperCAmelCase_							:						Any=None       ,			):
       SCREAMING_SNAKE_CASE							:   Any						    =     size if size is not None else {"height": 20, "width": 20}
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     parent
       SCREAMING_SNAKE_CASE							:   int						    =     batch_size
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     num_channels
       SCREAMING_SNAKE_CASE							:   List[str]						    =     image_size
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     min_resolution
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     max_resolution
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     size
       SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     do_normalize
       SCREAMING_SNAKE_CASE							:   Any						    =     do_convert_rgb
       SCREAMING_SNAKE_CASE							:   Any						    =     [512, 1024, 2048, 4096]
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     patch_size if patch_size is not None else {"height": 16, "width": 16}


     def 					_A							(					self							:						List[str]   ):
       return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}






     def 					_A							(					self							:						List[Any]   ):
       SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg"
       SCREAMING_SNAKE_CASE							:   Tuple						    =     Image.open(requests.get(UpperCAmelCase_       ,			stream=UpperCAmelCase_   ).raw   ).convert("RGB"   )
       return raw_image





@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11   ,		reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.'''   ,		)
@require_torch
@require_vision
class 			SCREAMING_SNAKE_CASE      (      lowerCAmelCase   ,		unittest.TestCase       ):
     '''simple docstring'''


     UpperCamelCase_				:		List[Any]								=						PixaStructImageProcessor if is_vision_available() else None


     def 					_A							(					self							:						int   ):
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     PixaStructImageProcessingTester(self   )


     @property
     def 					_A							(					self							:						List[Any]   ):
       return self.image_processor_tester.prepare_image_processor_dict()


     def 					_A							(					self							:						List[Any]   ):
       SCREAMING_SNAKE_CASE							:   str						    =     self.image_processing_class(**self.image_processor_dict   )
       self.assertTrue(hasattr(UpperCAmelCase_       ,			"do_normalize"   )   )
       self.assertTrue(hasattr(UpperCAmelCase_       ,			"do_convert_rgb"   )   )


     def 					_A							(					self							:						Tuple   ):
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     self.image_processor_tester.prepare_dummy_image()

       SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     self.image_processing_class(**self.image_processor_dict   )
       SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     2048

       SCREAMING_SNAKE_CASE							:   Tuple						    =     image_processor(UpperCAmelCase_       ,			return_tensors="pt"       ,			max_patches=UpperCAmelCase_   )
       self.assertTrue(torch.allclose(inputs.flattened_patches.mean()       ,			torch.tensor(0.0_606   )       ,			atol=1E-3       ,			rtol=1E-3   )   )


     def 					_A							(					self							:						str   ):
       # Initialize image_processor
       SCREAMING_SNAKE_CASE							:   Dict						    =     self.image_processing_class(**self.image_processor_dict   )
       # create random PIL images
       SCREAMING_SNAKE_CASE							:   List[str]						    =     prepare_image_inputs(self.image_processor_tester       ,			equal_resolution=UpperCAmelCase_   )
       for image in image_inputs:
         self.assertIsInstance(UpperCAmelCase_       ,			Image.Image   )

       # Test not batched input
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     (
           (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
           * self.image_processor_tester.num_channels
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
         # Test not batched input
         SCREAMING_SNAKE_CASE							:   int						    =     image_processor(
             image_inputs[0]       ,			return_tensors="pt"       ,			max_patches=UpperCAmelCase_   ).flattened_patches
         self.assertEqual(
             encoded_images.shape       ,			(1, max_patch, expected_hidden_dim)       ,			)

         # Test batched
         SCREAMING_SNAKE_CASE							:   Optional[int]						    =     image_processor(
             UpperCAmelCase_       ,			return_tensors="pt"       ,			max_patches=UpperCAmelCase_   ).flattened_patches
         self.assertEqual(
             encoded_images.shape       ,			(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)       ,			)


     def 					_A							(					self							:						List[str]   ):
       # Initialize image_processor
       SCREAMING_SNAKE_CASE							:   Dict						    =     self.image_processing_class(**self.image_processor_dict   )
       # create random PIL images
       SCREAMING_SNAKE_CASE							:   int						    =     prepare_image_inputs(self.image_processor_tester       ,			equal_resolution=UpperCAmelCase_   )
       for image in image_inputs:
         self.assertIsInstance(UpperCAmelCase_       ,			Image.Image   )

       # Test not batched input
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     (
           (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
           * self.image_processor_tester.num_channels
       ) + 2

       SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     True

       for max_patch in self.image_processor_tester.max_patches:
         # Test not batched input
         with self.assertRaises(UpperCAmelCase_   ):
           SCREAMING_SNAKE_CASE							:   str						    =     image_processor(
               image_inputs[0]       ,			return_tensors="pt"       ,			max_patches=UpperCAmelCase_   ).flattened_patches

         SCREAMING_SNAKE_CASE							:   Tuple						    =     "Hello"

         SCREAMING_SNAKE_CASE							:   Tuple						    =     image_processor(
             image_inputs[0]       ,			return_tensors="pt"       ,			max_patches=UpperCAmelCase_       ,			header_text=UpperCAmelCase_   ).flattened_patches
         self.assertEqual(
             encoded_images.shape       ,			(1, max_patch, expected_hidden_dim)       ,			)

         # Test batched
         SCREAMING_SNAKE_CASE							:   Tuple						    =     image_processor(
             UpperCAmelCase_       ,			return_tensors="pt"       ,			max_patches=UpperCAmelCase_       ,			header_text=UpperCAmelCase_   ).flattened_patches
         self.assertEqual(
             encoded_images.shape       ,			(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)       ,			)


     def 					_A							(					self							:						Union[str, Any]   ):
       # Initialize image_processor
       SCREAMING_SNAKE_CASE							:   List[str]						    =     self.image_processing_class(**self.image_processor_dict   )
       # create random numpy tensors
       SCREAMING_SNAKE_CASE							:   str						    =     prepare_image_inputs(self.image_processor_tester       ,			equal_resolution=UpperCAmelCase_       ,			numpify=UpperCAmelCase_   )
       for image in image_inputs:
         self.assertIsInstance(UpperCAmelCase_       ,			np.ndarray   )

       SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     (
           (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
           * self.image_processor_tester.num_channels
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
         # Test not batched input
         SCREAMING_SNAKE_CASE							:   Tuple						    =     image_processor(
             image_inputs[0]       ,			return_tensors="pt"       ,			max_patches=UpperCAmelCase_   ).flattened_patches
         self.assertEqual(
             encoded_images.shape       ,			(1, max_patch, expected_hidden_dim)       ,			)

         # Test batched
         SCREAMING_SNAKE_CASE							:   Tuple						    =     image_processor(
             UpperCAmelCase_       ,			return_tensors="pt"       ,			max_patches=UpperCAmelCase_   ).flattened_patches
         self.assertEqual(
             encoded_images.shape       ,			(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)       ,			)






     def 					_A							(					self							:						int   ):
       # Initialize image_processor
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     self.image_processing_class(**self.image_processor_dict   )
       # create random PyTorch tensors
       SCREAMING_SNAKE_CASE							:   Any						    =     prepare_image_inputs(self.image_processor_tester       ,			equal_resolution=UpperCAmelCase_       ,			torchify=UpperCAmelCase_   )
       for image in image_inputs:
         self.assertIsInstance(UpperCAmelCase_       ,			torch.Tensor   )

       # Test not batched input
       SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     (
           (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
           * self.image_processor_tester.num_channels
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
         # Test not batched input
         SCREAMING_SNAKE_CASE							:   str						    =     image_processor(
             image_inputs[0]       ,			return_tensors="pt"       ,			max_patches=UpperCAmelCase_   ).flattened_patches
         self.assertEqual(
             encoded_images.shape       ,			(1, max_patch, expected_hidden_dim)       ,			)

         # Test batched
         SCREAMING_SNAKE_CASE							:   List[Any]						    =     image_processor(
             UpperCAmelCase_       ,			return_tensors="pt"       ,			max_patches=UpperCAmelCase_   ).flattened_patches
         self.assertEqual(
             encoded_images.shape       ,			(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)       ,			)





@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11   ,		reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.'''   ,		)
@require_torch
@require_vision
class 			SCREAMING_SNAKE_CASE      (      lowerCAmelCase   ,		unittest.TestCase       ):
     '''simple docstring'''


     UpperCamelCase_				:		Any								=						PixaStructImageProcessor if is_vision_available() else None


     def 					_A							(					self							:						Any   ):
       SCREAMING_SNAKE_CASE							:   str						    =     PixaStructImageProcessingTester(self       ,			num_channels=4   )
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     3


     @property
     def 					_A							(					self							:						List[str]   ):
       return self.image_processor_tester.prepare_image_processor_dict()


     def 					_A							(					self							:						Any   ):
       SCREAMING_SNAKE_CASE							:   int						    =     self.image_processing_class(**self.image_processor_dict   )
       self.assertTrue(hasattr(UpperCAmelCase_       ,			"do_normalize"   )   )
       self.assertTrue(hasattr(UpperCAmelCase_       ,			"do_convert_rgb"   )   )






     def 					_A							(					self							:						Any   ):
       # Initialize image_processor
       SCREAMING_SNAKE_CASE							:   str						    =     self.image_processing_class(**self.image_processor_dict   )
       # create random PIL images
       SCREAMING_SNAKE_CASE							:   Tuple						    =     prepare_image_inputs(self.image_processor_tester       ,			equal_resolution=UpperCAmelCase_   )
       for image in image_inputs:
         self.assertIsInstance(UpperCAmelCase_       ,			Image.Image   )

       # Test not batched input
       SCREAMING_SNAKE_CASE							:   Tuple						    =     (
           (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
           * (self.image_processor_tester.num_channels - 1)
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
         # Test not batched input
         SCREAMING_SNAKE_CASE							:   Optional[int]						    =     image_processor(
             image_inputs[0]       ,			return_tensors="pt"       ,			max_patches=UpperCAmelCase_   ).flattened_patches
         self.assertEqual(
             encoded_images.shape       ,			(1, max_patch, expected_hidden_dim)       ,			)

         # Test batched
         SCREAMING_SNAKE_CASE							:   List[str]						    =     image_processor(
             UpperCAmelCase_       ,			return_tensors="pt"       ,			max_patches=UpperCAmelCase_   ).flattened_patches
         self.assertEqual(
             encoded_images.shape       ,			(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)       ,			)








'''simple docstring'''

from math import factorial

snake_case									=    {str(digit): factorial(digit) for digit in range(10)}


def        UpperCAmelCase_ (  lowerCamelCase_			):







 """simple docstring"""


 if not isinstance(lowerCamelCase_      ,			lowerCamelCase_			):
  raise TypeError("Parameter number must be int"			)

 if number < 0:
  raise ValueError("Parameter number must be greater than or equal to 0"			)

 # Converts number in string to iterate on its digits and adds its factorial.
 return sum(DIGIT_FACTORIAL[digit] for digit in str(lowerCamelCase_			)			)


def        UpperCAmelCase_ (  lowerCamelCase_ = 6_0      ,			lowerCamelCase_ = 1_0_0_0_0_0_0			):







 """simple docstring"""


 if not isinstance(lowerCamelCase_      ,			lowerCamelCase_			) or not isinstance(lowerCamelCase_      ,			lowerCamelCase_			):
  raise TypeError("Parameters chain_length and number_limit must be int"			)

 if chain_length <= 0 or number_limit <= 0:
  raise ValueError(
      "Parameters chain_length and number_limit must be greater than 0"			)

 # the counter for the chains with the exact desired length
 lowerCAmelCase__     :   str     		=	0
 # the cached sizes of the previous chains
 lowerCAmelCase__     :   dict[int, int]     		=	{}

 for start_chain_element in range(1      ,			lowerCamelCase_			):
  # The temporary set will contain the elements of the chain
  lowerCAmelCase__     :   Any     		=	set()
  lowerCAmelCase__     :   int     		=	0

  # Stop computing the chain when you find a cached size, a repeating item or the
  # length is greater then the desired one.
  lowerCAmelCase__     :   Dict     		=	start_chain_element
  while (
      chain_element not in chain_sets_lengths
      and chain_element not in chain_set
      and chain_set_length <= chain_length
  ):
   chain_set.add(lowerCamelCase_			)
   chain_set_length += 1
   lowerCAmelCase__     :   Dict     		=	digit_factorial_sum(lowerCamelCase_			)

  if chain_element in chain_sets_lengths:
   chain_set_length += chain_sets_lengths[chain_element]

  lowerCAmelCase__     :   Optional[Any]     		=	chain_set_length

  # If chain contains the exact amount of elements increase the counter
  if chain_set_length == chain_length:
   chains_counter += 1

 return chains_counter


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     print(f'{solution()}')





