'''simple docstring'''

import unittest

import numpy as np

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


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import ChineseCLIPImageProcessor



class 			UpperCAmelCase  (						unittest.TestCase					):





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase	,   __lowerCAmelCase=7	,   __lowerCAmelCase=3	,   __lowerCAmelCase=18	,   __lowerCAmelCase=30	,   __lowerCAmelCase=400	,   __lowerCAmelCase=True	,   __lowerCAmelCase=None	,   __lowerCAmelCase=True	,   __lowerCAmelCase=None	,   __lowerCAmelCase=True	,   __lowerCAmelCase=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]	,   __lowerCAmelCase=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]	,   __lowerCAmelCase=True	,   )  ->  Dict:
        lowercase__				:    Any  =					size if size is not None else {'''height''': 224, '''width''': 224}
        lowercase__				:    Optional[Any]  =					crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
        lowercase__				:    str  =					parent
        lowercase__				:    Any  =					batch_size
        lowercase__				:    List[Any]  =					num_channels
        lowercase__				:    Union[str, Any]  =					image_size
        lowercase__				:    Optional[int]  =					min_resolution
        lowercase__				:    Optional[Any]  =					max_resolution
        lowercase__				:    Union[str, Any]  =					do_resize
        lowercase__				:    Any  =					size
        lowercase__				:    Any  =					do_center_crop
        lowercase__				:    Any  =					crop_size
        lowercase__				:    List[str]  =					do_normalize
        lowercase__				:    Union[str, Any]  =					image_mean
        lowercase__				:    Any  =					image_std
        lowercase__				:    List[Any]  =					do_convert_rgb

       def _lowerCAmelCase(						self )  ->  Tuple:
        return {
            "do_resize": self.do_resize,
            "size": self.size,
            "do_center_crop": self.do_center_crop,
            "crop_size": self.crop_size,
            "do_normalize": self.do_normalize,
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_convert_rgb": self.do_convert_rgb,
        }


       def _lowerCAmelCase(						self	,   __lowerCAmelCase=False	,   __lowerCAmelCase=False	,   __lowerCAmelCase=False )  ->  Union[str, Any]:
        assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

        if equal_resolution:
         lowercase__				:    Any  =					[]
         for i in range(self.batch_size ):
          image_inputs.append(
              np.random.randint(
                  255	,   size=(self.num_channels, self.max_resolution, self.max_resolution)	,   dtype=np.uinta ) )
        else:
         lowercase__				:    Dict  =					[]
         for i in range(self.batch_size ):
          lowercase__ ,							lowercase__				:    Tuple  =					np.random.choice(np.arange(self.min_resolution	,   self.max_resolution )	,   2 )
          image_inputs.append(np.random.randint(255	,   size=(self.num_channels, width, height)	,   dtype=np.uinta ) )

        if not numpify and not torchify:
         # PIL expects the channel dimension as last dimension
         lowercase__				:    Optional[int]  =					[Image.fromarray(np.moveaxis(__lowerCAmelCase	,   0	,   -1 ) ) for x in image_inputs]

        if torchify:
         lowercase__				:    Dict  =					[torch.from_numpy(__lowerCAmelCase ) for x in image_inputs]

        return image_inputs



@require_torch
@require_vision
class 			UpperCAmelCase  (						a__    ,						unittest.TestCase					):





       '''simple docstring'''



       SCREAMING_SNAKE_CASE							=    ChineseCLIPImageProcessor if is_vision_available() else None

       def _lowerCAmelCase(						self )  ->  int:
        lowercase__				:    Tuple  =					ChineseCLIPImageProcessingTester(self	,   do_center_crop=__lowerCAmelCase )

       @property
       def _lowerCAmelCase(						self )  ->  Dict:
        return self.image_processor_tester.prepare_image_processor_dict()

       def _lowerCAmelCase(						self )  ->  Dict:
        lowercase__				:    Union[str, Any]  =					self.image_processing_class(**self.image_processor_dict )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''do_resize''' ) )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''size''' ) )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''do_center_crop''' ) )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''center_crop''' ) )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''do_normalize''' ) )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''image_mean''' ) )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''image_std''' ) )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''do_convert_rgb''' ) )

       def _lowerCAmelCase(						self )  ->  Any:
        lowercase__				:    int  =					self.image_processing_class.from_dict(self.image_processor_dict )
        self.assertEqual(image_processor.size	,   {'''height''': 224, '''width''': 224} )
        self.assertEqual(image_processor.crop_size	,   {'''height''': 18, '''width''': 18} )

        lowercase__				:    Dict  =					self.image_processing_class.from_dict(self.image_processor_dict	,   size=42	,   crop_size=84 )
        self.assertEqual(image_processor.size	,   {'''shortest_edge''': 42} )
        self.assertEqual(image_processor.crop_size	,   {'''height''': 84, '''width''': 84} )

       def _lowerCAmelCase(						self )  ->  Tuple:
        pass

       def _lowerCAmelCase(						self )  ->  Tuple:
        # Initialize image_processing
        lowercase__				:    Optional[Any]  =					self.image_processing_class(**self.image_processor_dict )
        # create random PIL images
        lowercase__				:    str  =					self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCAmelCase )
        for image in image_inputs:
         self.assertIsInstance(__lowerCAmelCase	,   Image.Image )

        # Test not batched input
        lowercase__				:    Optional[int]  =					image_processing(image_inputs[0]	,   return_tensors='''pt''' ).pixel_values
        self.assertEqual(
            encoded_images.shape	,   (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )	,   )

        # Test batched
        lowercase__				:    Union[str, Any]  =					image_processing(__lowerCAmelCase	,   return_tensors='''pt''' ).pixel_values
        self.assertEqual(
            encoded_images.shape	,   (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )	,   )

       def _lowerCAmelCase(						self )  ->  str:
        # Initialize image_processing
        lowercase__				:    Tuple  =					self.image_processing_class(**self.image_processor_dict )
        # create random numpy tensors
        lowercase__				:    int  =					self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCAmelCase	,   numpify=__lowerCAmelCase )
        for image in image_inputs:
         self.assertIsInstance(__lowerCAmelCase	,   np.ndarray )

        # Test not batched input
        lowercase__				:    Union[str, Any]  =					image_processing(image_inputs[0]	,   return_tensors='''pt''' ).pixel_values
        self.assertEqual(
            encoded_images.shape	,   (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )	,   )

        # Test batched
        lowercase__				:    str  =					image_processing(__lowerCAmelCase	,   return_tensors='''pt''' ).pixel_values
        self.assertEqual(
            encoded_images.shape	,   (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )	,   )


       def _lowerCAmelCase(						self )  ->  str:
        # Initialize image_processing
        lowercase__				:    Dict  =					self.image_processing_class(**self.image_processor_dict )
        # create random PyTorch tensors
        lowercase__				:    Dict  =					self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCAmelCase	,   torchify=__lowerCAmelCase )
        for image in image_inputs:
         self.assertIsInstance(__lowerCAmelCase	,   torch.Tensor )

        # Test not batched input
        lowercase__				:    Any  =					image_processing(image_inputs[0]	,   return_tensors='''pt''' ).pixel_values
        self.assertEqual(
            encoded_images.shape	,   (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )	,   )

        # Test batched
        lowercase__				:    str  =					image_processing(__lowerCAmelCase	,   return_tensors='''pt''' ).pixel_values
        self.assertEqual(
            encoded_images.shape	,   (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )	,   )



@require_torch
@require_vision
class 			UpperCAmelCase  (						a__    ,						unittest.TestCase					):





       '''simple docstring'''



       SCREAMING_SNAKE_CASE							=    ChineseCLIPImageProcessor if is_vision_available() else None

       def _lowerCAmelCase(						self )  ->  str:
        lowercase__				:    List[Any]  =					ChineseCLIPImageProcessingTester(self	,   num_channels=4	,   do_center_crop=__lowerCAmelCase )
        lowercase__				:    List[Any]  =					3

       @property
       def _lowerCAmelCase(						self )  ->  List[Any]:
        return self.image_processor_tester.prepare_image_processor_dict()

       def _lowerCAmelCase(						self )  ->  Optional[int]:
        lowercase__				:    int  =					self.image_processing_class(**self.image_processor_dict )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''do_resize''' ) )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''size''' ) )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''do_center_crop''' ) )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''center_crop''' ) )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''do_normalize''' ) )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''image_mean''' ) )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''image_std''' ) )
        self.assertTrue(hasattr(__lowerCAmelCase	,   '''do_convert_rgb''' ) )

       def _lowerCAmelCase(						self )  ->  List[str]:
        pass


       def _lowerCAmelCase(						self )  ->  Union[str, Any]:
        # Initialize image_processing
        lowercase__				:    Any  =					self.image_processing_class(**self.image_processor_dict )
        # create random PIL images
        lowercase__				:    List[Any]  =					self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCAmelCase )
        for image in image_inputs:
         self.assertIsInstance(__lowerCAmelCase	,   Image.Image )

        # Test not batched input
        lowercase__				:    Dict  =					image_processing(image_inputs[0]	,   return_tensors='''pt''' ).pixel_values
        self.assertEqual(
            encoded_images.shape	,   (
                1,
                self.expected_encoded_image_num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )	,   )

        # Test batched
        lowercase__				:    Optional[Any]  =					image_processing(__lowerCAmelCase	,   return_tensors='''pt''' ).pixel_values
        self.assertEqual(
            encoded_images.shape	,   (
                self.image_processor_tester.batch_size,
                self.expected_encoded_image_num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )	,   )


'''simple docstring'''

from math import pow
def 			__UpperCamelCase       (   UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase		,     ):
 if current_sum == needed_sum:
  # If the sum of the powers is equal to needed_sum, then we have a solution.
  solutions_count += 1
  return current_sum, solutions_count

 lowercase__				:    Optional[Any]  =					int(pow(UpperCAmelCase		,     UpperCAmelCase     )     )
 if current_sum + i_to_n <= needed_sum:
  # If the sum of the powers is less than needed_sum, then continue adding powers.
  current_sum += i_to_n
  lowercase__ ,							lowercase__				:    Dict  =					backtrack(
      UpperCAmelCase		,     UpperCAmelCase		,     current_number + 1		,     UpperCAmelCase		,     UpperCAmelCase     )
  current_sum -= i_to_n
 if i_to_n < needed_sum:
  # If the power of i is less than needed_sum, then try with the next power.
  lowercase__ ,							lowercase__				:    str  =					backtrack(
      UpperCAmelCase		,     UpperCAmelCase		,     current_number + 1		,     UpperCAmelCase		,     UpperCAmelCase     )
 return current_sum, solutions_count
def 			__UpperCamelCase       (   UpperCAmelCase		,     UpperCAmelCase     ):
 if not (1 <= needed_sum <= 1000 and 2 <= power <= 10):
  raise ValueError(
      '''Invalid input\n'''
      '''needed_sum must be between 1 and 1000, power between 2 and 10.'''     )

 return backtrack(UpperCAmelCase		,     UpperCAmelCase		,     1		,     0		,     0     )[1]  # Return the solutions_count


if __name__ == "__main__":
  import doctest

  doctest.testmod()

