

'''simple docstring'''

import os
import tempfile
from pathlib import Path
from unittest import TestCase

import pyarrow as pa
import pytest

from datasets.arrow_dataset import Dataset
from datasets.arrow_reader import ArrowReader, BaseReader, FileInstructions, ReadInstruction, make_file_instructions
from datasets.info import DatasetInfo
from datasets.splits import NamedSplit, Split, SplitDict, SplitInfo

from .utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases





class 							_snake_case	(    UpperCamelCase_  ):
   def       __UpperCamelCase  (	self     : Dict       ,SCREAMING_SNAKE_CASE__     : List[str]       ,SCREAMING_SNAKE_CASE__     : Optional[int]=False							):
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   (
         filename_skip_take['filename'],
         filename_skip_take['skip'] if 'skip' in filename_skip_take else None,
         filename_skip_take['take'] if 'take' in filename_skip_take else None,
     )
     open(os.path.join(lowercase_							)       ,"wb"							).close()
     SCREAMING_SNAKE_CASE:Optional[Any]   =   pa.Table.from_pydict({"filename": [Path(lowercase_							).name] * 100}							)
     if take == -1:
       SCREAMING_SNAKE_CASE:List[str]   =   len(lowercase_							) - skip
     if skip is not None and take is not None:
       SCREAMING_SNAKE_CASE:Union[str, Any]   =   pa_table.slice(lowercase_       ,lowercase_							)
     return pa_table





class 							_snake_case	(    UpperCamelCase_  ):
   def       __UpperCamelCase  (	self     : Union[str, Any]							):
     SCREAMING_SNAKE_CASE:int   =   'my_name'
     SCREAMING_SNAKE_CASE:Tuple   =   SplitInfo(name="train"       ,num_examples=100							)
     SCREAMING_SNAKE_CASE:Optional[Any]   =   SplitInfo(name="test"       ,num_examples=100							)
     SCREAMING_SNAKE_CASE:str   =   [train_info, test_info]
     SCREAMING_SNAKE_CASE:Tuple   =   SplitDict()
     split_dict.add(lowercase_							)
     split_dict.add(lowercase_							)
     SCREAMING_SNAKE_CASE:int   =   DatasetInfo(splits=lowercase_							)

     with tempfile.TemporaryDirectory() as tmp_dir:
       SCREAMING_SNAKE_CASE:Optional[Any]   =   ReaderTest(lowercase_       ,lowercase_							)

       SCREAMING_SNAKE_CASE:Optional[Any]   =   'test[:33%]'
       SCREAMING_SNAKE_CASE:int   =   Dataset(**reader.read(lowercase_       ,lowercase_       ,lowercase_							)							)
       self.assertEqual(dset["filename"][0]       ,F'''{name}-test'''							)
       self.assertEqual(dset.num_rows       ,33							)
       self.assertEqual(dset.num_columns       ,1							)

       SCREAMING_SNAKE_CASE:int   =   ['train', 'test[:33%]']
       SCREAMING_SNAKE_CASE:Any   =   [Split.TRAIN, ReadInstruction.from_spec("test[:33%]"							)]
       for instructions in [instructionsa, instructionsa]:
         SCREAMING_SNAKE_CASE:List[Any]   =   [reader.read(lowercase_       ,lowercase_       ,lowercase_							) for instr in instructions]
         SCREAMING_SNAKE_CASE:Union[str, Any]   =   (Dataset(**lowercase_							) for dataset_kwargs in datasets_kwargs)
         self.assertEqual(train_dset["filename"][0]       ,F'''{name}-train'''							)
         self.assertEqual(train_dset.num_rows       ,100							)
         self.assertEqual(train_dset.num_columns       ,1							)
         self.assertIsInstance(train_dset.split       ,lowercase_							)
         self.assertEqual(str(train_dset.split							)       ,"train"							)
         self.assertEqual(test_dset["filename"][0]       ,F'''{name}-test'''							)
         self.assertEqual(test_dset.num_rows       ,33							)
         self.assertEqual(test_dset.num_columns       ,1							)
         self.assertIsInstance(test_dset.split       ,lowercase_							)
         self.assertEqual(str(test_dset.split							)       ,"test[:33%]"							)
         del train_dset, test_dset
   def       __UpperCamelCase  (	self     : Optional[Any]							):
     SCREAMING_SNAKE_CASE:Tuple   =   'my_name'
     SCREAMING_SNAKE_CASE:Optional[int]   =   SplitInfo(name="train"       ,num_examples=1_000       ,shard_lengths=[100] * 10							)
     SCREAMING_SNAKE_CASE:Any   =   [train_info]
     SCREAMING_SNAKE_CASE:Dict   =   SplitDict()
     split_dict.add(lowercase_							)
     SCREAMING_SNAKE_CASE:List[str]   =   DatasetInfo(splits=lowercase_							)

     with tempfile.TemporaryDirectory() as tmp_dir:
       SCREAMING_SNAKE_CASE:str   =   ReaderTest(lowercase_       ,lowercase_							)

       SCREAMING_SNAKE_CASE:Dict   =   'train[:33%]'
       SCREAMING_SNAKE_CASE:Union[str, Any]   =   Dataset(**reader.read(lowercase_       ,lowercase_       ,lowercase_							)							)
       self.assertEqual(dset["filename"][0]       ,F'''{name}-train-00000-of-00010'''							)
       self.assertEqual(dset["filename"][-1]       ,F'''{name}-train-00003-of-00010'''							)
       self.assertEqual(dset.num_rows       ,330							)
       self.assertEqual(dset.num_columns       ,1							)







   def       __UpperCamelCase  (	self     : Dict							):
     SCREAMING_SNAKE_CASE:Optional[int]   =   SplitInfo(name="train"       ,num_examples=100							)
     SCREAMING_SNAKE_CASE:Dict   =   SplitInfo(name="test"       ,num_examples=100							)
     SCREAMING_SNAKE_CASE:str   =   SplitDict()
     split_dict.add(lowercase_							)
     split_dict.add(lowercase_							)
     SCREAMING_SNAKE_CASE:Optional[int]   =   DatasetInfo(splits=lowercase_							)

     with tempfile.TemporaryDirectory() as tmp_dir:
       SCREAMING_SNAKE_CASE:Tuple   =   ReaderTest(lowercase_       ,lowercase_							)

       SCREAMING_SNAKE_CASE:Union[str, Any]   =   [
           {'filename': os.path.join(lowercase_       ,"train"							)},
           {'filename': os.path.join(lowercase_       ,"test"							), 'skip': 10, 'take': 10},
       ]
       SCREAMING_SNAKE_CASE:int   =   Dataset(**reader.read_files(lowercase_       ,original_instructions="train+test[10:20]"							)							)
       self.assertEqual(dset.num_rows       ,110							)
       self.assertEqual(dset.num_columns       ,1							)
       del dset





@pytest.mark.parametrize("in_memory" ,    [False, True]	)
def 				A_					(	snake_case ,    snake_case ,    snake_case	):
  SCREAMING_SNAKE_CASE:Optional[int]   =   arrow_file
  with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
    SCREAMING_SNAKE_CASE:Optional[int]   =   ArrowReader.read_table(SCREAMING_SNAKE_CASE__ ,    in_memory=SCREAMING_SNAKE_CASE__	)
  assert table.shape == dataset.data.shape
  assert set(table.column_names	) == set(dataset.data.column_names	)
  assert dict(table.to_pydict()	) == dict(dataset.data.to_pydict()	)  # to_pydict returns OrderedDict





@pytest.mark.parametrize("in_memory" ,    [False, True]	)
def 				A_					(	snake_case ,    snake_case ,    snake_case	):
  SCREAMING_SNAKE_CASE:Union[str, Any]   =   arrow_file
  SCREAMING_SNAKE_CASE:List[str]   =   ArrowReader("" ,    SCREAMING_SNAKE_CASE__	)
  with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
    SCREAMING_SNAKE_CASE:str   =   reader.read_files([{"filename": filename}] ,    in_memory=SCREAMING_SNAKE_CASE__	)
  assert dataset_kwargs.keys() == {"arrow_table", "info", "split"}
  SCREAMING_SNAKE_CASE:List[str]   =   dataset_kwargs['arrow_table']
  assert table.shape == dataset.data.shape
  assert set(table.column_names	) == set(dataset.data.column_names	)
  assert dict(table.to_pydict()	) == dict(dataset.data.to_pydict()	)  # to_pydict returns OrderedDict





def 				A_					(	):
  assert ReadInstruction("train" ,    to=10 ,    unit="abs"	).to_spec() == "train[:10]"
  assert ReadInstruction("train" ,    from_=-80 ,    to=10 ,    unit="%"	).to_spec() == "train[-80%:10%]"

  SCREAMING_SNAKE_CASE:Any   =   'train+test'
  assert ReadInstruction.from_spec(SCREAMING_SNAKE_CASE__	).to_spec() == spec_train_test

  SCREAMING_SNAKE_CASE:Optional[Any]   =   'train[2:10]'
  assert ReadInstruction.from_spec(SCREAMING_SNAKE_CASE__	).to_spec() == spec_train_abs

  SCREAMING_SNAKE_CASE:Dict   =   'train[15%:-20%]'
  assert ReadInstruction.from_spec(SCREAMING_SNAKE_CASE__	).to_spec() == spec_train_pct

  SCREAMING_SNAKE_CASE:Tuple   =   'train[:10%](closest)'
  assert ReadInstruction.from_spec(SCREAMING_SNAKE_CASE__	).to_spec() == "train[:10%]"

  SCREAMING_SNAKE_CASE:str   =   'train[:10%](pct1_dropremainder)'
  assert ReadInstruction.from_spec(SCREAMING_SNAKE_CASE__	).to_spec() == spec_train_pct_rounding

  SCREAMING_SNAKE_CASE:Union[str, Any]   =   'train[:10%](pct1_dropremainder)+test[-10%:](pct1_dropremainder)'
  assert ReadInstruction.from_spec(SCREAMING_SNAKE_CASE__	).to_spec() == spec_train_test_pct_rounding





def 				A_					(	):
  SCREAMING_SNAKE_CASE:Any   =   'dummy'
  SCREAMING_SNAKE_CASE:Dict   =   [SplitInfo(name="train" ,    num_examples=100	)]
  SCREAMING_SNAKE_CASE:Any   =   'train[:33%]'
  SCREAMING_SNAKE_CASE:List[Any]   =   'arrow'
  SCREAMING_SNAKE_CASE:Dict   =   'prefix'

  SCREAMING_SNAKE_CASE:Any   =   make_file_instructions(SCREAMING_SNAKE_CASE__ ,    SCREAMING_SNAKE_CASE__ ,    SCREAMING_SNAKE_CASE__ ,    SCREAMING_SNAKE_CASE__ ,    SCREAMING_SNAKE_CASE__	)
  assert isinstance(SCREAMING_SNAKE_CASE__ ,    SCREAMING_SNAKE_CASE__	)
  assert file_instructions.num_examples == 33
  assert file_instructions.file_instructions == [
      {"filename": os.path.join(SCREAMING_SNAKE_CASE__ ,    F'''{name}-train.arrow'''	), "skip": 0, "take": 33}
  ]

  SCREAMING_SNAKE_CASE:int   =   [SplitInfo(name="train" ,    num_examples=100 ,    shard_lengths=[10] * 10	)]
  SCREAMING_SNAKE_CASE:Any   =   make_file_instructions(SCREAMING_SNAKE_CASE__ ,    SCREAMING_SNAKE_CASE__ ,    SCREAMING_SNAKE_CASE__ ,    SCREAMING_SNAKE_CASE__ ,    SCREAMING_SNAKE_CASE__	)
  assert isinstance(SCREAMING_SNAKE_CASE__ ,    SCREAMING_SNAKE_CASE__	)
  assert file_instructions.num_examples == 33
  assert file_instructions.file_instructions == [
      {"filename": os.path.join(SCREAMING_SNAKE_CASE__ ,    F'''{name}-train-00000-of-00010.arrow'''	), "skip": 0, "take": -1},
      {"filename": os.path.join(SCREAMING_SNAKE_CASE__ ,    F'''{name}-train-00001-of-00010.arrow'''	), "skip": 0, "take": -1},
      {"filename": os.path.join(SCREAMING_SNAKE_CASE__ ,    F'''{name}-train-00002-of-00010.arrow'''	), "skip": 0, "take": -1},
      {"filename": os.path.join(SCREAMING_SNAKE_CASE__ ,    F'''{name}-train-00003-of-00010.arrow'''	), "skip": 0, "take": 3},
  ]





@pytest.mark.parametrize("name, expected_exception" ,    [(None, TypeError), ("", ValueError)]	)
def 				A_					(	snake_case ,    snake_case	):
  SCREAMING_SNAKE_CASE:str   =   [SplitInfo(name="train" ,    num_examples=100	)]
  SCREAMING_SNAKE_CASE:str   =   'train'
  SCREAMING_SNAKE_CASE:Tuple   =   'arrow'
  SCREAMING_SNAKE_CASE:List[str]   =   'prefix_path'
  with pytest.raises(SCREAMING_SNAKE_CASE__	):
    SCREAMING_SNAKE_CASE:Dict   =   make_file_instructions(SCREAMING_SNAKE_CASE__ ,    SCREAMING_SNAKE_CASE__ ,    SCREAMING_SNAKE_CASE__ ,    SCREAMING_SNAKE_CASE__ ,    SCREAMING_SNAKE_CASE__	)









'''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())






