


import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


snake_case							:	Any					      =				random.Random()

if is_torch_available():
						import torch






def 							__lowercase       (			__lowerCAmelCase							:    int ,   __lowerCAmelCase							:    Optional[int]=1.0 ,   __lowerCAmelCase							:    List[str]=None ,   __lowerCAmelCase							:    List[str]=None       ):
							if rng is None:
														a__					=					global_rng

							a__					=					[]
							for batch_idx in range(shape[0]       ):
														values.append([]       )
														for _ in range(shape[1]       ):
																					values[-1].append(rng.random() * scale       )

							return values







class 	snake_case_       (unittest.TestCase				):
					def __init__(				self       :Union[str, Any]      ,__snake_case       :int      ,__snake_case       :str=7      ,__snake_case       :Tuple=4_00      ,__snake_case       :Optional[int]=20_00      ,__snake_case       :Dict=1      ,__snake_case       :Dict=0.0      ,__snake_case       :Optional[int]=1_60_00      ,__snake_case       :str=True      ,__snake_case       :Any=True      ,) ->   Optional[int]:
												a__					=					parent
												a__					=					batch_size
												a__					=					min_seq_length
												a__					=					max_seq_length
												a__					=					(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
												a__					=					feature_size
												a__					=					padding_value
												a__					=					sampling_rate
												a__					=					return_attention_mask
												a__					=					do_normalize
					def 			lowerCamelCase__(				self       :str				) ->   Any:
												return {
												    "feature_size": self.feature_size,
												    "padding_value": self.padding_value,
												    "sampling_rate": self.sampling_rate,
												    "return_attention_mask": self.return_attention_mask,
												    "do_normalize": self.do_normalize,
												}



					def 			lowerCamelCase__(				self       :Union[str, Any]      ,__snake_case       :Any=False      ,__snake_case       :Optional[int]=False				) ->   List[str]:
												def _flatten(__snake_case       :str				):
																			return list(itertools.chain(*__snake_case				)				)

												if equal_length:
																			a__					=					floats_list((self.batch_size, self.max_seq_length)				)
												else:
																			# make sure that inputs increase in size
																			a__					=					[
																			    _flatten(floats_list((x, self.feature_size)				)				)
																			    for x in range(self.min_seq_length      ,self.max_seq_length      ,self.seq_length_diff				)
																			]

												if numpify:
																			a__					=					[np.asarray(__snake_case				) for x in speech_inputs]

												return speech_inputs







@require_torch
@require_torchaudio
class 	snake_case_       (lowerCamelCase_     ,						unittest.TestCase				):
					UpperCAmelCase__ :   Union[str, Any]   				=      ASTFeatureExtractor
					def 			lowerCamelCase__(				self       :Dict				) ->   Tuple:
												a__					=					ASTFeatureExtractionTester(self				)
					def 			lowerCamelCase__(				self       :Optional[int]				) ->   str:
												# Tests that all call wrap to encode_plus and batch_encode_plus
												a__					=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
												# create three inputs of length 800, 1000, and 1200
												a__					=					[floats_list((1, x)				)[0] for x in range(8_00      ,14_00      ,2_00				)]
												a__					=					[np.asarray(__snake_case				) for speech_input in speech_inputs]

												# Test not batched input
												a__					=					feat_extract(speech_inputs[0]      ,return_tensors='np'				).input_values
												a__					=					feat_extract(np_speech_inputs[0]      ,return_tensors='np'				).input_values
												self.assertTrue(np.allclose(__snake_case      ,__snake_case      ,atol=1E-3				)				)

												# Test batched
												a__					=					feat_extract(__snake_case      ,padding=__snake_case      ,return_tensors='np'				).input_values
												a__					=					feat_extract(__snake_case      ,padding=__snake_case      ,return_tensors='np'				).input_values
												for enc_seq_a, enc_seq_a in zip(__snake_case      ,__snake_case				):
																			self.assertTrue(np.allclose(__snake_case      ,__snake_case      ,atol=1E-3				)				)

												# Test 2-D numpy arrays are batched.
												a__					=					[floats_list((1, x)				)[0] for x in (8_00, 8_00, 8_00)]
												a__					=					np.asarray(__snake_case				)
												a__					=					feat_extract(__snake_case      ,return_tensors='np'				).input_values
												a__					=					feat_extract(__snake_case      ,return_tensors='np'				).input_values
												for enc_seq_a, enc_seq_a in zip(__snake_case      ,__snake_case				):
																			self.assertTrue(np.allclose(__snake_case      ,__snake_case      ,atol=1E-3				)				)
					@require_torch
					def 			lowerCamelCase__(				self       :List[str]				) ->   Union[str, Any]:
												import torch

												a__					=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
												a__					=					np.random.rand(1_00				).astype(np.floataa				)
												a__					=					np_speech_inputs.tolist()

												for inputs in [py_speech_inputs, np_speech_inputs]:
																			a__					=					feature_extractor.pad([{'input_values': inputs}]      ,return_tensors='np'				)
																			self.assertTrue(np_processed.input_values.dtype == np.floataa				)
																			a__					=					feature_extractor.pad([{'input_values': inputs}]      ,return_tensors='pt'				)
																			self.assertTrue(pt_processed.input_values.dtype == torch.floataa				)
					def 			lowerCamelCase__(				self       :Any      ,__snake_case       :Dict				) ->   Any:
												from datasets import load_dataset

												a__					=					load_dataset('hf-internal-testing/librispeech_asr_dummy'      ,'clean'      ,split='validation'				)
												# automatic decoding with librispeech
												a__					=					ds.sort('id'				).select(range(__snake_case				)				)[:num_samples]['audio']

												return [x["array"] for x in speech_samples]



					@require_torch
					def 			lowerCamelCase__(				self       :List[str]				) ->   Any:
												# fmt: off
												a__					=					torch.tensor(
												    [-0.98_94, -1.27_76, -0.90_66, -1.27_76, -0.93_49, -1.26_09, -1.03_86, -1.27_76,
												     -1.15_61, -1.27_76, -1.20_52, -1.27_23, -1.21_90, -1.21_32, -1.27_76, -1.11_33,
												     -1.19_53, -1.13_43, -1.15_84, -1.22_03, -1.17_70, -1.24_74, -1.23_81, -1.19_36,
												     -0.92_70, -0.83_17, -0.80_49, -0.77_06, -0.75_65, -0.78_69]				)
												# fmt: on

												a__					=					self._load_datasamples(1				)
												a__					=					ASTFeatureExtractor()
												a__					=					feature_extractor(__snake_case      ,return_tensors='pt'				).input_values
												self.assertEquals(input_values.shape      ,(1, 10_24, 1_28)				)
												self.assertTrue(torch.allclose(input_values[0, 0, :30]      ,__snake_case      ,atol=1E-4				)				)








from collections import defaultdict
from math import ceil, sqrt






def 							__lowercase       (			__lowerCAmelCase							:    int = 1_0_0_0_0_0_0 ,   __lowerCAmelCase							:    int = 1_0       ):
							a__					=					defaultdict(__lowerCAmelCase       )

							for outer_width in range(3 ,   (t_limit // 4) + 2       ):
														if outer_width * outer_width > t_limit:
																					a__					=					max(
																					    ceil(sqrt(outer_width * outer_width - t_limit       )       ) ,   1       )
														else:
																					a__					=					1

														hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2

														for hole_width in range(__lowerCAmelCase ,   outer_width - 1 ,   2       ):
																					count[outer_width * outer_width - hole_width * hole_width] += 1

							return sum(1 for n in count.values() if 1 <= n <= 1_0       )


if __name__ == "__main__":
						print(f"""{solution() = }""")




