

'''simple docstring'''

import multiprocessing
import os
from typing import BinaryIO, Optional, Union

from .. import Dataset, Features, NamedSplit, config
from ..formatting import query_table
from ..packaged_modules.csv.csv import Csv
from ..utils import logging
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader





class 							_snake_case	(    _a  ):
   def __init__(	self     : Optional[Any]       ,SCREAMING_SNAKE_CASE__     : NestedDataStructureLike[PathLike]       ,SCREAMING_SNAKE_CASE__     : Optional[NamedSplit] = None       ,SCREAMING_SNAKE_CASE__     : Optional[Features] = None       ,SCREAMING_SNAKE_CASE__     : str = None       ,SCREAMING_SNAKE_CASE__     : bool = False       ,SCREAMING_SNAKE_CASE__     : bool = False       ,SCREAMING_SNAKE_CASE__     : Optional[int] = None       ,**SCREAMING_SNAKE_CASE__     : int       ,):
     super().__init__(
         SCREAMING_SNAKE_CASE__       ,split=SCREAMING_SNAKE_CASE__       ,features=SCREAMING_SNAKE_CASE__       ,cache_dir=SCREAMING_SNAKE_CASE__       ,keep_in_memory=SCREAMING_SNAKE_CASE__       ,streaming=SCREAMING_SNAKE_CASE__       ,num_proc=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__       ,)
     SCREAMING_SNAKE_CASE:str   =   path_or_paths if isinstance(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							) else {self.split: path_or_paths}
     SCREAMING_SNAKE_CASE:Optional[int]   =   Csv(
         cache_dir=SCREAMING_SNAKE_CASE__       ,data_files=SCREAMING_SNAKE_CASE__       ,features=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__       ,)







   def       __UpperCamelCase  (	self     : str							):
     # Build iterable dataset
     if self.streaming:
       SCREAMING_SNAKE_CASE:Tuple   =   self.builder.as_streaming_dataset(split=self.split							)
     # Build regular (map-style) dataset
     else:
       SCREAMING_SNAKE_CASE:Optional[int]   =   None
       SCREAMING_SNAKE_CASE:str   =   None
       SCREAMING_SNAKE_CASE:Dict   =   None
       SCREAMING_SNAKE_CASE:Optional[int]   =   None

       self.builder.download_and_prepare(
           download_config=SCREAMING_SNAKE_CASE__       ,download_mode=SCREAMING_SNAKE_CASE__       ,verification_mode=SCREAMING_SNAKE_CASE__       ,base_path=SCREAMING_SNAKE_CASE__       ,num_proc=self.num_proc       ,)
       SCREAMING_SNAKE_CASE:str   =   self.builder.as_dataset(
           split=self.split       ,verification_mode=SCREAMING_SNAKE_CASE__       ,in_memory=self.keep_in_memory							)
     return dataset





class 							_snake_case	:
   def __init__(	self     : Union[str, Any]       ,SCREAMING_SNAKE_CASE__     : Dataset       ,SCREAMING_SNAKE_CASE__     : Union[PathLike, BinaryIO]       ,SCREAMING_SNAKE_CASE__     : Optional[int] = None       ,SCREAMING_SNAKE_CASE__     : Optional[int] = None       ,**SCREAMING_SNAKE_CASE__     : Any       ,):
     if num_proc is not None and num_proc <= 0:
       raise ValueError(F'''num_proc {num_proc} must be an integer > 0.'''							)

     SCREAMING_SNAKE_CASE:List[Any]   =   dataset
     SCREAMING_SNAKE_CASE:str   =   path_or_buf
     SCREAMING_SNAKE_CASE:Any   =   batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
     SCREAMING_SNAKE_CASE:int   =   num_proc
     SCREAMING_SNAKE_CASE:List[Any]   =   "utf-8"
     SCREAMING_SNAKE_CASE:Optional[int]   =   to_csv_kwargs
   def       __UpperCamelCase  (	self     : int							):
     SCREAMING_SNAKE_CASE:int   =   self.to_csv_kwargs.pop("path_or_buf"       ,SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Dict   =   self.to_csv_kwargs.pop("header"       ,SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Optional[Any]   =   self.to_csv_kwargs.pop("index"       ,SCREAMING_SNAKE_CASE__							)

     if isinstance(self.path_or_buf       ,(str, bytes, os.PathLike)							):
       with open(self.path_or_buf       ,"wb+"							) as buffer:
         SCREAMING_SNAKE_CASE:int   =   self._write(file_obj=SCREAMING_SNAKE_CASE__       ,header=SCREAMING_SNAKE_CASE__       ,index=SCREAMING_SNAKE_CASE__       ,**self.to_csv_kwargs							)
     else:
       SCREAMING_SNAKE_CASE:List[str]   =   self._write(file_obj=self.path_or_buf       ,header=SCREAMING_SNAKE_CASE__       ,index=SCREAMING_SNAKE_CASE__       ,**self.to_csv_kwargs							)
     return written
   def       __UpperCamelCase  (	self     : List[str]       ,SCREAMING_SNAKE_CASE__     : Dict							):
     SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE:List[str]   =   args

     SCREAMING_SNAKE_CASE:str   =   query_table(
         table=self.dataset.data       ,key=slice(SCREAMING_SNAKE_CASE__       ,offset + self.batch_size							)       ,indices=self.dataset._indices       ,)
     SCREAMING_SNAKE_CASE:Dict   =   batch.to_pandas().to_csv(
         path_or_buf=SCREAMING_SNAKE_CASE__       ,header=header if (offset == 0) else False       ,index=SCREAMING_SNAKE_CASE__       ,**SCREAMING_SNAKE_CASE__							)
     return csv_str.encode(self.encoding							)







   def       __UpperCamelCase  (	self     : int       ,SCREAMING_SNAKE_CASE__     : BinaryIO       ,SCREAMING_SNAKE_CASE__     : str       ,SCREAMING_SNAKE_CASE__     : Any       ,**SCREAMING_SNAKE_CASE__     : List[str]							):
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   0

     if self.num_proc is None or self.num_proc == 1:
       for offset in logging.tqdm(
           range(0       ,len(self.dataset							)       ,self.batch_size							)       ,unit="ba"       ,disable=not logging.is_progress_bar_enabled()       ,desc="Creating CSV from Arrow format"       ,):
         SCREAMING_SNAKE_CASE:Optional[int]   =   self._batch_csv((offset, header, index, to_csv_kwargs)							)
         written += file_obj.write(SCREAMING_SNAKE_CASE__							)

     else:
       SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE:Optional[Any]   =   len(self.dataset							), self.batch_size
       with multiprocessing.Pool(self.num_proc							) as pool:
         for csv_str in logging.tqdm(
             pool.imap(
                 self._batch_csv       ,[(offset, header, index, to_csv_kwargs) for offset in range(0       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							)]       ,)       ,total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size       ,unit="ba"       ,disable=not logging.is_progress_bar_enabled()       ,desc="Creating CSV from Arrow format"       ,):
           written += file_obj.write(SCREAMING_SNAKE_CASE__							)

     return written









'''simple docstring'''

from __future__ import annotations

from collections import Counter





def 				A_					(	snake_case	):
  SCREAMING_SNAKE_CASE:int   =   [True] * n
  SCREAMING_SNAKE_CASE:List[str]   =   False
  SCREAMING_SNAKE_CASE:Tuple   =   False
  SCREAMING_SNAKE_CASE:int   =   True

  for i in range(3 ,    int(n**0.5 + 1	) ,    2	):
    SCREAMING_SNAKE_CASE:str   =   i * 2
    while index < n:
      SCREAMING_SNAKE_CASE:Any   =   False
      SCREAMING_SNAKE_CASE:List[Any]   =   index + i

  SCREAMING_SNAKE_CASE:Union[str, Any]   =   [2]

  for i in range(3 ,    snake_case ,    2	):
    if is_prime[i]:
      primes.append(snake_case	)

  return primes





def 				A_					(	snake_case	):
  SCREAMING_SNAKE_CASE:List[str]   =   str(snake_case	)
  SCREAMING_SNAKE_CASE:Union[str, Any]   =   []
  SCREAMING_SNAKE_CASE:List[str]   =   ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

  for duplicate in Counter(snake_case	) - Counter(set(snake_case	)	):
    SCREAMING_SNAKE_CASE:List[str]   =   [int(number_str.replace(snake_case ,    snake_case	)	) for digit in digits]
    replacements.append(snake_case	)

  return replacements





def 				A_					(	snake_case = 8	):
  SCREAMING_SNAKE_CASE:str   =   set()

  # Filter primes with less than 3 replaceable digits
  SCREAMING_SNAKE_CASE:Tuple   =   {
      x for x in set(prime_sieve(1000000	)	) if len(str(snake_case	)	) - len(set(str(snake_case	)	)	) >= 3
  }

  for prime in primes:
    if prime in numbers_checked:
      continue

    SCREAMING_SNAKE_CASE:List[Any]   =   digit_replacements(snake_case	)

    for family in replacements:
      numbers_checked.update(snake_case	)
      SCREAMING_SNAKE_CASE:Optional[Any]   =   primes.intersection(snake_case	)

      if len(snake_case	) != family_length:
        continue

      return min(snake_case	)

  return -1


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






