
import json
import os
import tempfile

import datasets
from utils import generate_example_dataset, get_duration


__a         =   5_0_0_0_0
__a         =   5_0_0_0

__a     ,	__a         =   os.path.split(__file__)
__a         =   os.path.join(RESULTS_BASEPATH, 'results', RESULTS_FILENAME.replace('.py', '.json'))




@get_duration
def 							a		(				snake_case__:   datasets.Dataset		,  snake_case__:   Optional[int]   ):


				'''simple docstring'''
				for i in range(snake_case__   ):
								lowercase_	    =   dataset[i]




@get_duration
def 							a		(				snake_case__:   datasets.Dataset		,  snake_case__:   Tuple		,  snake_case__:   Tuple   ):


				'''simple docstring'''
				for i in range(0		,  len(snake_case__   )		,  snake_case__   ):
								lowercase_	    =   dataset[i : i + batch_size]




@get_duration
def 							a		(				snake_case__:   datasets.Dataset		,  snake_case__:   Any		,  snake_case__:   Optional[int]   ):


				'''simple docstring'''
				with dataset.formatted_as(type=snake_case__   ):
								for i in range(snake_case__   ):
												lowercase_	    =   dataset[i]




@get_duration
def 							a		(				snake_case__:   datasets.Dataset		,  snake_case__:   Tuple		,  snake_case__:   Union[str, Any]		,  snake_case__:   int   ):


				'''simple docstring'''
				with dataset.formatted_as(type=snake_case__   ):
								for i in range(0		,  snake_case__		,  snake_case__   ):
												lowercase_	    =   dataset[i : i + batch_size]




def 							a		(				):


				'''simple docstring'''
				lowercase_	    =   {'''num examples''': SPEED_TEST_N_EXAMPLES}
				lowercase_	    =   [
				    (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}),
				]

				lowercase_	    =   [
				    (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'''   )
								lowercase_	    =   datasets.Features(
								    {'''list''': datasets.Sequence(datasets.Value('''float32'''   )   ), '''numbers''': datasets.Value('''float32'''   )}   )
								lowercase_	    =   generate_example_dataset(
								    os.path.join(snake_case__		,  '''dataset.arrow'''   )		,  snake_case__		,  num_examples=snake_case__		,  seq_shapes={'''list''': (100,)}		,  )
								print('''first set of iterations'''   )
								for func, kwargs in functions:
												print(func.__name__		,  str(snake_case__   )   )
												lowercase_	    =   func(snake_case__		,  **snake_case__   )

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

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


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






import os

# Precomputes a list of the 100 first triangular numbers
__a         =   [int(0.5 * n * (n + 1)) for n in range(1, 1_0_1)]




def 							a		(				):


				'''simple docstring'''
				lowercase_	    =   os.path.dirname(os.path.realpath(snake_case__   )   )
				lowercase_	    =   os.path.join(snake_case__		,  '''words.txt'''   )

				lowercase_	    =   ''''''
				with open(snake_case__   ) as f:
								lowercase_	    =   f.readline()

				lowercase_	    =   [word.strip('''"'''   ) for word in words.strip('''\r\n'''   ).split(''','''   )]
				lowercase_	    =   [
				    word
				    for word in [sum(ord(snake_case__   ) - 64 for x in word   ) for word in words]
				    if word in TRIANGULAR_NUMBERS
				]
				return len(snake_case__   )


if __name__ == "__main__":
						print(solution())




