



import time
from dataclasses import dataclass
from multiprocessing import Pool
from unittest import TestCase
from unittest.mock import patch

import multiprocess
import numpy as np
import pytest

from datasets.utils.py_utils import (
    NestedDataStructure,
    asdict,
    iflatmap_unordered,
    map_nested,
    temp_seed,
    temporary_assignment,
    zip_dict,
)

from .utils import require_tf, require_torch



def       lowercase       (   SCREAMING_SNAKE_CASE__       :	Optional[int]				)  ->     Any:  # picklable for multiprocessing
	return x.sum()



def       lowercase       (   SCREAMING_SNAKE_CASE__       :	Optional[Any]				)  ->     str:  # picklable for multiprocessing
	return i + 1







@dataclass
class 		snake_case						:



			'''simple docstring'''






			snake_case_					: int
			snake_case_					: str







class 		snake_case						(	SCREAMING_SNAKE_CASE_  ):



			'''simple docstring'''






			def      UpperCamelCase_  (					self							:			int)				->				Optional[Any]:

				"""simple docstring"""







				_snake_case    :  str					=					{}
				_snake_case    :  Optional[int]					=					[]
				_snake_case    :  List[str]					=					1
				_snake_case    :  Tuple					=					[1, 2]
				_snake_case    :  str					=					{"""a""": 1, """b""": 2}
				_snake_case    :  Any					=					{"""a""": [1, 2], """b""": [3, 4]}
				_snake_case    :  str					=					{"""a""": {"""1""": 1}, """b""": 2}
				_snake_case    :  Optional[Any]					=					{"""a""": 1, """b""": 2, """c""": 3, """d""": 4}
				_snake_case    :  Union[str, Any]					=					{}
				_snake_case    :  List[str]					=					[]
				_snake_case    :  str					=					2
				_snake_case    :  Optional[Any]					=					[2, 3]
				_snake_case    :  str					=					{"""a""": 2, """b""": 3}
				_snake_case    :  Optional[int]					=					{"""a""": [2, 3], """b""": [4, 5]}
				_snake_case    :  Union[str, Any]					=					{"""a""": {"""1""": 2}, """b""": 3}
				_snake_case    :  Any					=					{"""a""": 2, """b""": 3, """c""": 4, """d""": 5}
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase)				,		lowerCAmelCase)

				_snake_case    :  List[Any]					=					2
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase				,		num_proc=lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase				,		num_proc=lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase				,		num_proc=lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase				,		num_proc=lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase				,		num_proc=lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase				,		num_proc=lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase				,		num_proc=lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase				,		num_proc=lowerCAmelCase)				,		lowerCAmelCase)

				_snake_case    :  Optional[int]					=					{"""a""": np.eye(2), """b""": np.zeros(3), """c""": np.ones(2)}
				_snake_case    :  Dict					=					{"""a""": 2, """b""": 0, """c""": 2}
				_snake_case    :  Any					=					{
				    """a""": np.eye(2).astype(lowerCAmelCase),
				    """b""": np.zeros(3).astype(lowerCAmelCase),
				    """c""": np.ones(2).astype(lowerCAmelCase),
				}
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase				,		map_numpy=lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(
				    {k: v.tolist() for k, v in map_nested(lowerCAmelCase				,		lowerCAmelCase				,		map_numpy=lowerCAmelCase).items()}				,		{k: v.tolist() for k, v in expected_map_nested_sna_int.items()}				,		)
				self.assertEqual(map_nested(lowerCAmelCase				,		lowerCAmelCase				,		map_numpy=lowerCAmelCase				,		num_proc=lowerCAmelCase)				,		lowerCAmelCase)
				self.assertEqual(
				    {k: v.tolist() for k, v in map_nested(lowerCAmelCase				,		lowerCAmelCase				,		map_numpy=lowerCAmelCase				,		num_proc=lowerCAmelCase).items()}				,		{k: v.tolist() for k, v in expected_map_nested_sna_int.items()}				,		)
				with self.assertRaises(lowerCAmelCase):  # can't pickle a local lambda
					map_nested(lambda lowerCAmelCase: x + 1				,		lowerCAmelCase				,		num_proc=lowerCAmelCase)

			def      UpperCamelCase_  (					self							:			List[Any])				->				Any:

				"""simple docstring"""







				_snake_case    :  Union[str, Any]					=					{"""a""": 1, """b""": 2}
				_snake_case    :  Tuple					=					{"""a""": 3, """b""": 4}
				_snake_case    :  Dict					=					{"""a""": 5, """b""": 6}
				_snake_case    :  Optional[Any]					=					sorted([("""a""", (1, 3, 5)), ("""b""", (2, 4, 6))])
				self.assertEqual(sorted(zip_dict(lowerCAmelCase				,		lowerCAmelCase				,		lowerCAmelCase))				,		lowerCAmelCase)


			def      UpperCamelCase_  (					self							:			Optional[Any])				->				int:

				"""simple docstring"""







				class 		snake_case						:



							'''simple docstring'''






							snake_case_					: Any          =		'bar'
				_snake_case    :  Dict					=					Foo()
				self.assertEqual(foo.my_attr				,		"""bar""")
				with temporary_assignment(lowerCAmelCase				,		"""my_attr"""				,		"""BAR"""):
					self.assertEqual(foo.my_attr				,		"""BAR""")
				self.assertEqual(foo.my_attr				,		"""bar""")



@pytest.mark.parametrize(
    """iterable_length, num_proc, expected_num_proc"""				,       [
        (1, None, 1),
        (1, 1, 1),
        (2, None, 1),
        (2, 1, 1),
        (2, 2, 1),
        (2, 3, 1),
        (3, 2, 1),
        (16, 16, 16),
        (16, 17, 16),
        (17, 16, 16),
    ]				,       )
def       lowercase       (   SCREAMING_SNAKE_CASE__       :	List[str]				,       SCREAMING_SNAKE_CASE__       :	Tuple				,       SCREAMING_SNAKE_CASE__       :	List[str]				)  ->     Any:
	with patch("""datasets.utils.py_utils._single_map_nested"""				) as mock_single_map_nested, patch(
	    """datasets.parallel.parallel.Pool"""				) as mock_multiprocessing_pool:
		_snake_case    :  Dict					=					{F'''{i}''': i for i in range(_SCREAMING_SNAKE_CASE				)}
		_snake_case    :  List[Any]					=					map_nested(lambda SCREAMING_SNAKE_CASE__				: x + 10				,       _SCREAMING_SNAKE_CASE				,       num_proc=_SCREAMING_SNAKE_CASE				,       parallel_min_length=16				)
		if expected_num_proc == 1:
			assert mock_single_map_nested.called
			assert not mock_multiprocessing_pool.called
		else:
			assert not mock_single_map_nested.called
			assert mock_multiprocessing_pool.called
			assert mock_multiprocessing_pool.call_args[0][0] == expected_num_proc







class 		snake_case						(	SCREAMING_SNAKE_CASE_  ):



			'''simple docstring'''






			@require_tf
			def      UpperCamelCase_  (					self							:			Tuple)				->				str:

				"""simple docstring"""







				import tensorflow as tf
				from tensorflow.keras import layers

				_snake_case    :  Tuple					=					layers.Dense(2)

				def gen_random_output():
					_snake_case    :  Optional[int]					=					tf.random.uniform((1, 3))
					return model(lowerCAmelCase).numpy()

				with temp_seed(42				,		set_tensorflow=lowerCAmelCase):
					_snake_case    :  Optional[int]					=					gen_random_output()
				with temp_seed(42				,		set_tensorflow=lowerCAmelCase):
					_snake_case    :  Tuple					=					gen_random_output()
				_snake_case    :  Union[str, Any]					=					gen_random_output()

				np.testing.assert_equal(lowerCAmelCase				,		lowerCAmelCase)
				self.assertGreater(np.abs(outa - outa).sum()				,		0)

			@require_torch
			def      UpperCamelCase_  (					self							:			Tuple)				->				Any:

				"""simple docstring"""







				import torch

				def gen_random_output():
					_snake_case    :  Optional[Any]					=					torch.nn.Linear(3				,		2)
					_snake_case    :  Dict					=					torch.rand(1				,		3)
					return model(lowerCAmelCase).detach().numpy()

				with temp_seed(42				,		set_pytorch=lowerCAmelCase):
					_snake_case    :  int					=					gen_random_output()
				with temp_seed(42				,		set_pytorch=lowerCAmelCase):
					_snake_case    :  str					=					gen_random_output()
				_snake_case    :  Any					=					gen_random_output()

				np.testing.assert_equal(lowerCAmelCase				,		lowerCAmelCase)
				self.assertGreater(np.abs(outa - outa).sum()				,		0)


			def      UpperCamelCase_  (					self							:			List[str])				->				Tuple:

				"""simple docstring"""







				def gen_random_output():
					return np.random.rand(1				,		3)

				with temp_seed(42):
					_snake_case    :  Optional[int]					=					gen_random_output()
				with temp_seed(42):
					_snake_case    :  Dict					=					gen_random_output()
				_snake_case    :  Optional[int]					=					gen_random_output()

				np.testing.assert_equal(lowerCAmelCase				,		lowerCAmelCase)
				self.assertGreater(np.abs(outa - outa).sum()				,		0)



@pytest.mark.parametrize("""input_data"""				,       [{}]				)
def       lowercase       (   SCREAMING_SNAKE_CASE__       :	List[Any]				)  ->     Any:
	_snake_case    :  str					=					NestedDataStructure(_SCREAMING_SNAKE_CASE				).data
	assert output_data == input_data



@pytest.mark.parametrize(
    """data, expected_output"""				,       [
        ({}, []),
        ([], []),
        ("""foo""", ["""foo"""]),
        (["""foo""", """bar"""], ["""foo""", """bar"""]),
        ([["""foo""", """bar"""]], ["""foo""", """bar"""]),
        ([[["""foo"""], ["""bar"""]]], ["""foo""", """bar"""]),
        ([[["""foo"""], """bar"""]], ["""foo""", """bar"""]),
        ({"""a""": 1, """b""": 2}, [1, 2]),
        ({"""a""": [1, 2], """b""": [3, 4]}, [1, 2, 3, 4]),
        ({"""a""": [[1, 2]], """b""": [[3, 4]]}, [1, 2, 3, 4]),
        ({"""a""": [[1, 2]], """b""": [3, 4]}, [1, 2, 3, 4]),
        ({"""a""": [[[1], [2]]], """b""": [[[3], [4]]]}, [1, 2, 3, 4]),
        ({"""a""": [[[1], [2]]], """b""": [[3, 4]]}, [1, 2, 3, 4]),
        ({"""a""": [[[1], [2]]], """b""": [3, 4]}, [1, 2, 3, 4]),
        ({"""a""": [[[1], [2]]], """b""": [3, [4]]}, [1, 2, 3, 4]),
        ({"""a""": {"""1""": 1}, """b""": 2}, [1, 2]),
        ({"""a""": {"""1""": [1]}, """b""": 2}, [1, 2]),
        ({"""a""": {"""1""": [1]}, """b""": [2]}, [1, 2]),
    ]				,       )
def       lowercase       (   SCREAMING_SNAKE_CASE__       :	List[str]				,       SCREAMING_SNAKE_CASE__       :	Any				)  ->     Optional[int]:
	_snake_case    :  str					=					NestedDataStructure(_SCREAMING_SNAKE_CASE				).flatten()
	assert output == expected_output



def       lowercase       (   )  ->     int:
	_snake_case    :  Dict					=					A(x=1				,       y="""foobar"""				)
	_snake_case    :  Any					=					{"""x""": 1, """y""": """foobar"""}
	assert asdict(_SCREAMING_SNAKE_CASE				) == expected_output

	_snake_case    :  Union[str, Any]					=					{"""a""": {"""b""": A(x=10				,       y="""foo"""				)}, """c""": [A(x=20				,       y="""bar"""				)]}
	_snake_case    :  Optional[int]					=					{"""a""": {"""b""": {"""x""": 10, """y""": """foo"""}}, """c""": [{"""x""": 20, """y""": """bar"""}]}
	assert asdict(_SCREAMING_SNAKE_CASE				) == expected_output

	with pytest.raises(_SCREAMING_SNAKE_CASE				):
		asdict([1, A(x=10				,       y="""foo"""				)]				)



def       lowercase       (   SCREAMING_SNAKE_CASE__       :	Union[str, Any]				)  ->     Any:
	return text.split()



def       lowercase       (   SCREAMING_SNAKE_CASE__       :	List[str]				)  ->     Union[str, Any]:
	yield (time.time(), content)
	time.sleep(2				)
	yield (time.time(), content)



def       lowercase       (   )  ->     Any:
	with Pool(2				) as pool:
		_snake_case    :  str					=					list(iflatmap_unordered(_SCREAMING_SNAKE_CASE				,       _split_text				,       kwargs_iterable=[{"""text""": """hello there"""}] * 10				)				)
		assert out.count("""hello"""				) == 10
		assert out.count("""there"""				) == 10
		assert len(_SCREAMING_SNAKE_CASE				) == 20

	# check multiprocess from pathos (uses dill for pickling)
	with multiprocess.Pool(2				) as pool:
		_snake_case    :  Any					=					list(iflatmap_unordered(_SCREAMING_SNAKE_CASE				,       _split_text				,       kwargs_iterable=[{"""text""": """hello there"""}] * 10				)				)
		assert out.count("""hello"""				) == 10
		assert out.count("""there"""				) == 10
		assert len(_SCREAMING_SNAKE_CASE				) == 20

	# check that we get items as fast as possible
	with Pool(2				) as pool:
		_snake_case    :  str					=					[]
		for yield_time, content in iflatmap_unordered(
		    _SCREAMING_SNAKE_CASE				,       _aseconds_generator_of_aitems_with_timing				,       kwargs_iterable=[{"""content""": """a"""}, {"""content""": """b"""}]				):
			assert yield_time < time.time() + 0.1, "we should each item directly after it was yielded"
			out.append(_SCREAMING_SNAKE_CASE				)
		assert out.count("""a"""				) == 2
		assert out.count("""b"""				) == 2
		assert len(_SCREAMING_SNAKE_CASE				) == 4





def       lowercase       (   SCREAMING_SNAKE_CASE__       :	int = 1_000				)  ->     int:
 _snake_case	,    _snake_case    :  str					=					1, 1
 _snake_case    :  List[Any]					=					2
 while True:
  _snake_case    :  Union[str, Any]					=					0
  _snake_case    :  int					=					fa + fa
  _snake_case	,    _snake_case    :  Union[str, Any]					=					fa, f
  index += 1
  for _ in str(SCREAMING_SNAKE_CASE__				):
   i += 1
  if i == n:
   break
 return index


if __name__ == "__main__":
  print(solution(int(str(input()).strip())))
