



'''simple docstring'''
from timeit import timeit

snake_case_ :	List[str]         =  {
    "MALAYALAM": True,
    "String": False,
    "rotor": True,
    "level": True,
    "A": True,
    "BB": True,
    "ABC": False,
    "amanaplanacanalpanama": True,  # "a man a plan a canal panama"
}
# Ensure our test data is valid
assert all((key == key[::-1]) is value for key, value in test_data.items())






def A__    (     UpperCAmelCase_							):
						_UpperCamelCase					:		int 				=   0
						_UpperCamelCase					:		int 				=   len(A__							) - 1
						while start_i < end_i:
												if s[start_i] == s[end_i]:
																		start_i += 1
																		end_i -= 1
												else:
																		return False
						return True






def A__    (     UpperCAmelCase_							):
						_UpperCamelCase					:		Dict 				=   len(A__							) // 2
						_UpperCamelCase					:		List[Any] 				=   len(A__							)

						# We need to traverse till half of the length of string
						# as we can get access of the i'th last element from
						# i'th index.
						# eg: [0,1,2,3,4,5] => 4th index can be accessed
						# with the help of 1st index (i==n-i-1)
						# where n is length of string
						return all(s[i] == s[n - i - 1] for i in range(A__							)							)






def A__    (     UpperCAmelCase_							):
						if len(A__							) <= 2:
												return True
						if s[0] == s[len(A__							) - 1]:
												return is_palindrome_recursive(s[1:-1]							)
						else:
												return False






def A__    (     UpperCAmelCase_							):
						return s == s[::-1]






def A__    (     UpperCAmelCase_							):
						_UpperCamelCase					:		List[Any] 				=   f'all({name}(key) is value for key, value in test_data.items())'
						_UpperCamelCase					:		int 				=   f'from __main__ import test_data, {name}'
						_UpperCamelCase					:		Union[str, Any] 				=   5_0_0_0_0_0
						_UpperCamelCase					:		Union[str, Any] 				=   timeit(stmt=A__  ,  setup=A__  ,  number=A__							)
						print(f'{name:<35} finished {number:,} runs in {result:.5f} seconds'							)


if __name__ == "__main__":
					for key, value in test_data.items():
										assert is_palindrome(key) is is_palindrome_recursive(key)
										assert is_palindrome(key) is is_palindrome_slice(key)
										print(F"""{key:21} {value}""")
					print('a man a plan a canal panama')

					# finished 500,000 runs in 0.46793 seconds
					benchmark_function('is_palindrome_slice')
					# finished 500,000 runs in 0.85234 seconds
					benchmark_function('is_palindrome')
					# finished 500,000 runs in 1.32028 seconds
					benchmark_function('is_palindrome_recursive')
					# finished 500,000 runs in 2.08679 seconds
					benchmark_function('is_palindrome_traversal')







'''simple docstring'''


import json
import os
import tempfile

import datasets
from utils import generate_example_dataset, get_duration


_lowerCamelCase	:       List[str]      	=	5_0000
_lowerCamelCase	:       Optional[int]      	=	5000

_lowerCamelCase	,_lowerCamelCase	:       int      	=	os.path.split(__file__)
_lowerCamelCase	:       str      	=	os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json"))







@get_duration
def 				__lowerCamelCase				(   A__    ,						A__    )       ->	Any:






   """simple docstring"""
   for i in range(A__    ):
      UpperCamelCase					=  dataset[i]







@get_duration
def 				__lowerCamelCase				(   A__    ,						A__    ,						A__    )       ->	int:






   """simple docstring"""
   for i in range(0    ,						len(A__    )    ,						A__    ):
      UpperCamelCase					=  dataset[i : i + batch_size]







@get_duration
def 				__lowerCamelCase				(   A__    ,						A__    ,						A__    )       ->	List[Any]:






   """simple docstring"""
   with dataset.formatted_as(type=A__    ):
      for i in range(A__    ):
         UpperCamelCase					=  dataset[i]







@get_duration
def 				__lowerCamelCase				(   A__    ,						A__    ,						A__    ,						A__    )       ->	int:






   """simple docstring"""
   with dataset.formatted_as(type=A__    ):
      for i in range(0    ,						A__    ,						A__    ):
         UpperCamelCase					=  dataset[i : i + batch_size]







def 				__lowerCamelCase				(   )       ->	List[str]:






   """simple docstring"""
   UpperCamelCase					=  {'num examples': SPEED_TEST_N_EXAMPLES}
   UpperCamelCase					=  [
       (read, {'length': SMALL_TEST}),
       (read, {'length': SPEED_TEST_N_EXAMPLES}),
       (read_batch, {'length': SPEED_TEST_N_EXAMPLES, 'batch_size': 10}),
       (read_batch, {'length': SPEED_TEST_N_EXAMPLES, 'batch_size': 100}),
       (read_batch, {'length': SPEED_TEST_N_EXAMPLES, 'batch_size': 1_000}),
       (read_formatted, {'type': 'numpy', 'length': SMALL_TEST}),
       (read_formatted, {'type': 'pandas', 'length': SMALL_TEST}),
       (read_formatted, {'type': 'torch', 'length': SMALL_TEST}),
       (read_formatted, {'type': 'tensorflow', 'length': SMALL_TEST}),
       (read_formatted_batch, {'type': 'numpy', 'length': SMALL_TEST, 'batch_size': 10}),
       (read_formatted_batch, {'type': 'numpy', 'length': SMALL_TEST, 'batch_size': 1_000}),
   ]

   UpperCamelCase					=  [
       (read, {'length': SMALL_TEST}),
       (read, {'length': SPEED_TEST_N_EXAMPLES}),
       (read_batch, {'length': SPEED_TEST_N_EXAMPLES, 'batch_size': 10}),
       (read_batch, {'length': SPEED_TEST_N_EXAMPLES, 'batch_size': 100}),
       (read_batch, {'length': SPEED_TEST_N_EXAMPLES, 'batch_size': 1_000}),
       (read_formatted, {'type': 'numpy', 'length': SMALL_TEST}),
       (read_formatted_batch, {'type': 'numpy', 'length': SMALL_TEST, 'batch_size': 10}),
       (read_formatted_batch, {'type': 'numpy', 'length': SMALL_TEST, 'batch_size': 1_000}),
   ]
   with tempfile.TemporaryDirectory() as tmp_dir:
      print('generating dataset'    )
      UpperCamelCase					=  datasets.Features(
          {'list': datasets.Sequence(datasets.Value('float32'    )    ), 'numbers': datasets.Value('float32'    )}    )
      UpperCamelCase					=  generate_example_dataset(
          os.path.join(A__    ,						'dataset.arrow'    )    ,						A__    ,						num_examples=A__    ,						seq_shapes={'list': (100,)}    ,						)
      print('first set of iterations'    )
      for func, kwargs in functions:
         print(func.__name__    ,						str(A__    )    )
         UpperCamelCase					=  func(A__    ,						**A__    )

      print('shuffling dataset'    )
      UpperCamelCase					=  dataset.shuffle()
      print('Second set of iterations (after shuffling'    )
      for func, kwargs in functions_shuffled:
         print('shuffled '    ,						func.__name__    ,						str(A__    )    )
         UpperCamelCase					=  func(
             A__    ,						**A__    )

   with open(A__    ,						'wb'    ) as f:
      f.write(json.dumps(A__    ).encode('utf-8'    )    )


if __name__ == "__main__":  # useful to run the profiler
   benchmark_iterating()

