import itertools
from dataclasses import dataclass
from typing import Optional

import pyarrow as pa

import datasets
from datasets.table import table_cast


_A      :				str 				=    datasets.utils.logging.get_logger(__name__)







@dataclass
class     _lowercase					(    datasets.BuilderConfig      ):


 '''simple docstring'''






 _SCREAMING_SNAKE_CASE					:	List[str]				     = None







class     _lowercase					(    datasets.ArrowBasedBuilder      ):


 '''simple docstring'''






 _SCREAMING_SNAKE_CASE					:	Optional[Any]				     = ArrowConfig
 def a (				self       :     Dict							)  ->   Optional[Any]:
  return datasets.DatasetInfo(features=self.config.features							)
 def a (				self       :     Tuple				,	SCREAMING_SNAKE_CASE__       :     Union[str, Any]							)  ->   Optional[int]:






  if not self.config.data_files:
   raise ValueError(f"""At least one data file must be specified, but got data_files={self.config.data_files}"""							)
  __lowerCAmelCase    							=						dl_manager.download_and_extract(self.config.data_files							)
  if isinstance(__lowercase				,	(str, list, tuple)							):
   __lowerCAmelCase    							=						data_files
   if isinstance(__lowercase				,	__lowercase							):
    __lowerCAmelCase    							=						[files]
   # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
   __lowerCAmelCase    							=						[dl_manager.iter_files(__lowercase							) for file in files]
   return [datasets.SplitGenerator(name=datasets.Split.TRAIN				,	gen_kwargs={"""files""": files}							)]
  __lowerCAmelCase    							=						[]
  for split_name, files in data_files.items():
   if isinstance(__lowercase				,	__lowercase							):
    __lowerCAmelCase    							=						[files]
   # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
   __lowerCAmelCase    							=						[dl_manager.iter_files(__lowercase							) for file in files]
   # Infer features is they are stoed in the arrow schema
   if self.info.features is None:
    for file in itertools.chain.from_iterable(__lowercase							):
     with open(__lowercase				,	"""rb"""							) as f:
      __lowerCAmelCase    							=						datasets.Features.from_arrow_schema(pa.ipc.open_stream(__lowercase							).schema							)
     break
   splits.append(datasets.SplitGenerator(name=__lowercase				,	gen_kwargs={"""files""": files}							)							)
  return splits
 def a (				self       :     str				,	SCREAMING_SNAKE_CASE__       :     pa.Table							)  ->   Union[str, Any]:






  if self.info.features is not None:
   # more expensive cast to support nested features with keys in a different order
   # allows str <-> int/float or str to Audio for example
   __lowerCAmelCase    							=						table_cast(__lowercase				,	self.info.features.arrow_schema							)
  return pa_table





 def a (				self       :     Tuple				,	SCREAMING_SNAKE_CASE__       :     Optional[Any]							)  ->   int:






  for file_idx, file in enumerate(itertools.chain.from_iterable(__lowercase							)							):
   with open(__lowercase				,	"""rb"""							) as f:
    try:
     for batch_idx, record_batch in enumerate(pa.ipc.open_stream(__lowercase							)							):
      __lowerCAmelCase    							=						pa.Table.from_batches([record_batch]							)
      # Uncomment for debugging (will print the Arrow table size and elements)
      # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
      # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
      yield f"""{file_idx}_{batch_idx}""", self._cast_table(__lowercase							)
    except ValueError as e:
     logger.error(f"""Failed to read file '{file}' with error {type(__lowercase							)}: {e}"""							)
     raise







'''simple docstring'''

import functools
from math import sqrt
from time import time







def 					UpperCamelCase_						( snake_case_	:	Dict						,		*snake_case_	:	Union[str, Any]						,		**snake_case_	:	List[str]				) ->					List[str]:






 '''simple docstring'''

 __lowerCAmelCase    							=						time()
 __lowerCAmelCase    							=						func(*snake_case_						,		**snake_case_				)
 __lowerCAmelCase    							=						time()
 if int(end - start				) > 0:
  print(f"""{func.__name__} runtime: {(end - start):0.4f} s"""				)
 else:
  print(f"""{func.__name__} runtime: {(end - start) * 10_00:0.4f} ms"""				)
 return output







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 if n < 0:
  raise Exception("""n is negative"""				)
 if n == 0:
  return [0]
 __lowerCAmelCase    							=						[0, 1]
 for _ in range(n - 1				):
  fib.append(fib[-1] + fib[-2]				)
 return fib







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 def fib_recursive_term(snake_case_	:	int				) -> int:
  if i < 0:
   raise Exception("""n is negative"""				)
  if i < 2:
   return i
  return fib_recursive_term(i - 1				) + fib_recursive_term(i - 2				)

 if n < 0:
  raise Exception("""n is negative"""				)
 return [fib_recursive_term(snake_case_				) for i in range(n + 1				)]







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 @functools.cache
 def fib_recursive_term(snake_case_	:	int				) -> int:
  if i < 0:
   raise Exception("""n is negative"""				)
  if i < 2:
   return i
  return fib_recursive_term(i - 1				) + fib_recursive_term(i - 2				)

 if n < 0:
  raise Exception("""n is negative"""				)
 return [fib_recursive_term(snake_case_				) for i in range(n + 1				)]







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 if n < 0:
  raise Exception("""n is negative"""				)
 # Cache must be outside recursuive function
 # other it will reset every time it calls itself.
 __lowerCAmelCase    							=						{0: 0, 1: 1, 2: 1}  # Prefilled cache

 def rec_fn_memoized(snake_case_	:	int				) -> int:
  if num in cache:
   return cache[num]

  __lowerCAmelCase    							=						rec_fn_memoized(num - 1				) + rec_fn_memoized(num - 2				)
  __lowerCAmelCase    							=						value
  return value

 return [rec_fn_memoized(snake_case_				) for i in range(n + 1				)]







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 if n < 0:
  raise Exception("""n is negative"""				)
 if n >= 14_75:
  raise Exception("""n is too large"""				)
 __lowerCAmelCase    							=						sqrt(5				)
 __lowerCAmelCase    							=						(1 + sqrt_a) / 2
 return [round(phi**i / sqrt_a				) for i in range(n + 1				)]


if __name__ == "__main__":
 _A      :				Optional[Any] 				=    30
 time_func(fib_iterative, num)
 time_func(fib_recursive, num)  # Around 3s runtime
 time_func(fib_recursive_cached, num)  # Around 0ms runtime
 time_func(fib_memoization, num)
 time_func(fib_binet, num)






