


import pytest

from datasets import Dataset, DatasetDict, Features, NamedSplit, Value
from datasets.io.text import TextDatasetReader

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases




def       a_ (       SCREAMING_SNAKE_CASE__   : str						,     SCREAMING_SNAKE_CASE__   : List[Any]	):
       '''simple docstring'''





       assert isinstance(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	)
       assert dataset.num_rows == 4
       assert dataset.num_columns == 1
       assert dataset.column_names == ["text"]
       for feature, expected_dtype in expected_features.items():
              assert dataset.features[feature].dtype == expected_dtype




@pytest.mark.parametrize('keep_in_memory'						,     [False, True]	)
def       a_ (       SCREAMING_SNAKE_CASE__   : List[Any]						,     SCREAMING_SNAKE_CASE__   : List[str]						,     SCREAMING_SNAKE_CASE__   : List[str]	):
       '''simple docstring'''





       _lowerCamelCase			:						Union[str, Any]            =tmp_path / 'cache'
       _lowerCamelCase			:						str            ={'text': 'string'}
       with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
              _lowerCamelCase			:						Optional[Any]            =TextDatasetReader(SCREAMING_SNAKE_CASE__						,     cache_dir=SCREAMING_SNAKE_CASE__						,     keep_in_memory=SCREAMING_SNAKE_CASE__	).read()
       _check_text_dataset(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	)




@pytest.mark.parametrize(
    'features'						,     [
        None,
        {'text': 'string'},
        {'text': 'int32'},
        {'text': 'float32'},
    ]						,     )
def       a_ (       SCREAMING_SNAKE_CASE__   : Any						,     SCREAMING_SNAKE_CASE__   : List[Any]						,     SCREAMING_SNAKE_CASE__   : List[Any]	):
       '''simple docstring'''





       _lowerCamelCase			:						str            =tmp_path / 'cache'
       _lowerCamelCase			:						Tuple            ={'text': 'string'}
       _lowerCamelCase			:						Dict            =features.copy() if features else default_expected_features
       _lowerCamelCase			:						List[str]            =(
           Features({feature: Value(SCREAMING_SNAKE_CASE__	) for feature, dtype in features.items()}	) if features is not None else None
       )
       _lowerCamelCase			:						List[Any]            =TextDatasetReader(SCREAMING_SNAKE_CASE__						,     features=SCREAMING_SNAKE_CASE__						,     cache_dir=SCREAMING_SNAKE_CASE__	).read()
       _check_text_dataset(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	)




@pytest.mark.parametrize('split'						,     [None, NamedSplit('train'	), 'train', 'test']	)
def       a_ (       SCREAMING_SNAKE_CASE__   : Optional[int]						,     SCREAMING_SNAKE_CASE__   : str						,     SCREAMING_SNAKE_CASE__   : Dict	):
       '''simple docstring'''





       _lowerCamelCase			:						Tuple            =tmp_path / 'cache'
       _lowerCamelCase			:						List[str]            ={'text': 'string'}
       _lowerCamelCase			:						Any            =TextDatasetReader(SCREAMING_SNAKE_CASE__						,     cache_dir=SCREAMING_SNAKE_CASE__						,     split=SCREAMING_SNAKE_CASE__	).read()
       _check_text_dataset(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	)
       assert dataset.split == split if split else "train"




@pytest.mark.parametrize('path_type'						,     [str, list]	)
def       a_ (       SCREAMING_SNAKE_CASE__   : Dict						,     SCREAMING_SNAKE_CASE__   : Any						,     SCREAMING_SNAKE_CASE__   : int	):
       '''simple docstring'''





       if issubclass(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	):
              _lowerCamelCase			:						str            =text_path
       elif issubclass(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	):
              _lowerCamelCase			:						Union[str, Any]            =[text_path]
       _lowerCamelCase			:						Tuple            =tmp_path / 'cache'
       _lowerCamelCase			:						Any            ={'text': 'string'}
       _lowerCamelCase			:						Tuple            =TextDatasetReader(SCREAMING_SNAKE_CASE__						,     cache_dir=SCREAMING_SNAKE_CASE__	).read()
       _check_text_dataset(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	)




def       a_ (       SCREAMING_SNAKE_CASE__   : Dict						,     SCREAMING_SNAKE_CASE__   : Tuple						,     SCREAMING_SNAKE_CASE__   : Dict=("train",)	):
       '''simple docstring'''





       assert isinstance(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	)
       for split in splits:
              _lowerCamelCase			:						Tuple            =dataset_dict[split]
              assert dataset.num_rows == 4
              assert dataset.num_columns == 1
              assert dataset.column_names == ["text"]
              for feature, expected_dtype in expected_features.items():
                     assert dataset.features[feature].dtype == expected_dtype




@pytest.mark.parametrize('keep_in_memory'						,     [False, True]	)
def       a_ (       SCREAMING_SNAKE_CASE__   : str						,     SCREAMING_SNAKE_CASE__   : Optional[int]						,     SCREAMING_SNAKE_CASE__   : Dict	):
       '''simple docstring'''





       _lowerCamelCase			:						str            =tmp_path / 'cache'
       _lowerCamelCase			:						Union[str, Any]            ={'text': 'string'}
       with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
              _lowerCamelCase			:						Any            =TextDatasetReader({'train': text_path}						,     cache_dir=SCREAMING_SNAKE_CASE__						,     keep_in_memory=SCREAMING_SNAKE_CASE__	).read()
       _check_text_datasetdict(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	)




@pytest.mark.parametrize(
    'features'						,     [
        None,
        {'text': 'string'},
        {'text': 'int32'},
        {'text': 'float32'},
    ]						,     )
def       a_ (       SCREAMING_SNAKE_CASE__   : Union[str, Any]						,     SCREAMING_SNAKE_CASE__   : Optional[int]						,     SCREAMING_SNAKE_CASE__   : Any	):
       '''simple docstring'''





       _lowerCamelCase			:						Union[str, Any]            =tmp_path / 'cache'
       # CSV file loses col_1 string dtype information: default now is "int64" instead of "string"
       _lowerCamelCase			:						Any            ={'text': 'string'}
       _lowerCamelCase			:						str            =features.copy() if features else default_expected_features
       _lowerCamelCase			:						Optional[Any]            =(
           Features({feature: Value(SCREAMING_SNAKE_CASE__	) for feature, dtype in features.items()}	) if features is not None else None
       )
       _lowerCamelCase			:						int            =TextDatasetReader({'train': text_path}						,     features=SCREAMING_SNAKE_CASE__						,     cache_dir=SCREAMING_SNAKE_CASE__	).read()
       _check_text_datasetdict(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	)




@pytest.mark.parametrize('split'						,     [None, NamedSplit('train'	), 'train', 'test']	)
def       a_ (       SCREAMING_SNAKE_CASE__   : List[Any]						,     SCREAMING_SNAKE_CASE__   : int						,     SCREAMING_SNAKE_CASE__   : Optional[int]	):
       '''simple docstring'''





       if split:
              _lowerCamelCase			:						Any            ={split: text_path}
       else:
              _lowerCamelCase			:						Union[str, Any]            ='train'
              _lowerCamelCase			:						Tuple            ={'train': text_path, 'test': text_path}
       _lowerCamelCase			:						Any            =tmp_path / 'cache'
       _lowerCamelCase			:						List[str]            ={'text': 'string'}
       _lowerCamelCase			:						Any            =TextDatasetReader(SCREAMING_SNAKE_CASE__						,     cache_dir=SCREAMING_SNAKE_CASE__	).read()
       _check_text_datasetdict(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__						,     splits=list(path.keys()	)	)
       assert all(dataset[split].split == split for split in path.keys()	)






from timeit import timeit

lowerCamelCase		   =			{
    '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_ (       SCREAMING_SNAKE_CASE__   : str	):
       '''simple docstring'''





       _lowerCamelCase			:						Optional[int]            =0
       _lowerCamelCase			:						Union[str, Any]            =len(SCREAMING_SNAKE_CASE__	) - 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_ (       SCREAMING_SNAKE_CASE__   : str	):
       '''simple docstring'''





       _lowerCamelCase			:						List[str]            =len(SCREAMING_SNAKE_CASE__	) // 2
       _lowerCamelCase			:						Optional[Any]            =len(SCREAMING_SNAKE_CASE__	)

       # 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(SCREAMING_SNAKE_CASE__	)	)




def       a_ (       SCREAMING_SNAKE_CASE__   : str	):
       '''simple docstring'''





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




def       a_ (       SCREAMING_SNAKE_CASE__   : str	):
       '''simple docstring'''





       return s == s[::-1]




def       a_ (       SCREAMING_SNAKE_CASE__   : str	):
       '''simple docstring'''





       _lowerCamelCase			:						Optional[int]            =F'''all({name}(key) is value for key, value in test_data.items())'''
       _lowerCamelCase			:						List[Any]            =F'''from __main__ import test_data, {name}'''
       _lowerCamelCase			:						Any            =500_000
       _lowerCamelCase			:						Dict            =timeit(stmt=SCREAMING_SNAKE_CASE__						,     setup=SCREAMING_SNAKE_CASE__						,     number=SCREAMING_SNAKE_CASE__	)
       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')


