






'''simple docstring'''







import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import datasets
import datasets.config

from .utils import require_beam





class 	SCREAMING_SNAKE_CASE					(					datasets.BeamBasedBuilder       ):






			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			Dict:
							return datasets.DatasetInfo(
							    features=datasets.Features({'''content''': datasets.Value('''string'''					)}					)  ,						supervised_keys=__SCREAMING_SNAKE_CASE  ,						)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			int					)			->			Any:
							return [datasets.SplitGenerator(name=datasets.Split.TRAIN  ,						gen_kwargs={'''examples''': get_test_dummy_examples()}					)]







			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			Any					)			->			Optional[Any]:
							import apache_beam as beam

							return pipeline | "Load Examples" >> beam.Create(__SCREAMING_SNAKE_CASE					)





class 	SCREAMING_SNAKE_CASE					(					datasets.BeamBasedBuilder       ):






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			int:
							return datasets.DatasetInfo(
							    features=datasets.Features({'''a''': datasets.Sequence({'''b''': datasets.Value('''string'''					)}					)}					)  ,						supervised_keys=__SCREAMING_SNAKE_CASE  ,						)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			Tuple					)			->			int:
							return [
							    datasets.SplitGenerator(name=datasets.Split.TRAIN  ,						gen_kwargs={'''examples''': get_test_nested_examples()}					)
							]







			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			int  ,						__SCREAMING_SNAKE_CASE      :			int					)			->			Dict:
							import apache_beam as beam

							return pipeline | "Load Examples" >> beam.Create(__SCREAMING_SNAKE_CASE					)





def 		_UpperCAmelCase   (							):
				return [(i, {"content": content}) for i, content in enumerate(['''foo''', '''bar''', '''foobar''']      )]





def 		_UpperCAmelCase   (							):
				return [(i, {"a": {"b": [content]}}) for i, content in enumerate(['''foo''', '''bar''', '''foobar''']      )]





class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_       ):






			@require_beam
			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			Dict:
							a_		:  Union[str, Any]											=      len(get_test_dummy_examples()					)
							with tempfile.TemporaryDirectory() as tmp_cache_dir:
											a_		:  Any											=      DummyBeamDataset(cache_dir=__SCREAMING_SNAKE_CASE  ,						beam_runner='''DirectRunner'''					)
											builder.download_and_prepare()
											self.assertTrue(
											    os.path.exists(
											        os.path.join(__SCREAMING_SNAKE_CASE  ,						builder.name  ,						'''default'''  ,						'''0.0.0'''  ,						f'{builder.name}-train.arrow'					)					)					)
											self.assertDictEqual(builder.info.features  ,						datasets.Features({'''content''': datasets.Value('''string'''					)}					)					)
											a_		:  List[Any]											=      builder.as_dataset()
											self.assertEqual(dset['''train'''].num_rows  ,						__SCREAMING_SNAKE_CASE					)
											self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples  ,						__SCREAMING_SNAKE_CASE					)
											self.assertDictEqual(dset['''train'''][0]  ,						get_test_dummy_examples()[0][1]					)
											self.assertDictEqual(
											    dset['''train'''][expected_num_examples - 1]  ,						get_test_dummy_examples()[expected_num_examples - 1][1]					)
											self.assertTrue(
											    os.path.exists(os.path.join(__SCREAMING_SNAKE_CASE  ,						builder.name  ,						'''default'''  ,						'''0.0.0'''  ,						'''dataset_info.json'''					)					)					)
											del dset






			@require_beam
			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			Any:
							import apache_beam as beam

							a_		:  str											=      beam.io.parquetio.WriteToParquet

							a_		:  Dict											=      len(get_test_dummy_examples()					)
							with tempfile.TemporaryDirectory() as tmp_cache_dir:
											a_		:  Union[str, Any]											=      DummyBeamDataset(cache_dir=__SCREAMING_SNAKE_CASE  ,						beam_runner='''DirectRunner'''					)
											with patch('''apache_beam.io.parquetio.WriteToParquet'''					) as write_parquet_mock:
															a_		:  Optional[Any]											=      partial(__SCREAMING_SNAKE_CASE  ,						num_shards=2					)
															builder.download_and_prepare()
											self.assertTrue(
											    os.path.exists(
											        os.path.join(
											            __SCREAMING_SNAKE_CASE  ,						builder.name  ,						'''default'''  ,						'''0.0.0'''  ,						f'{builder.name}-train-00000-of-00002.arrow'					)					)					)
											self.assertTrue(
											    os.path.exists(
											        os.path.join(
											            __SCREAMING_SNAKE_CASE  ,						builder.name  ,						'''default'''  ,						'''0.0.0'''  ,						f'{builder.name}-train-00000-of-00002.arrow'					)					)					)
											self.assertDictEqual(builder.info.features  ,						datasets.Features({'''content''': datasets.Value('''string'''					)}					)					)
											a_		:  Union[str, Any]											=      builder.as_dataset()
											self.assertEqual(dset['''train'''].num_rows  ,						__SCREAMING_SNAKE_CASE					)
											self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples  ,						__SCREAMING_SNAKE_CASE					)
											# Order is not preserved when sharding, so we just check that all the elements are there
											self.assertListEqual(sorted(dset['''train''']['''content''']					)  ,						sorted(['''foo''', '''bar''', '''foobar''']					)					)
											self.assertTrue(
											    os.path.exists(os.path.join(__SCREAMING_SNAKE_CASE  ,						builder.name  ,						'''default'''  ,						'''0.0.0'''  ,						'''dataset_info.json'''					)					)					)
											del dset






			@require_beam
			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			Dict:
							with tempfile.TemporaryDirectory() as tmp_cache_dir:
											a_		:  int											=      DummyBeamDataset(cache_dir=__SCREAMING_SNAKE_CASE					)
											self.assertRaises(datasets.builder.MissingBeamOptions  ,						builder.download_and_prepare					)







			@require_beam
			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			Optional[int]:
							a_		:  Union[str, Any]											=      len(get_test_nested_examples()					)
							with tempfile.TemporaryDirectory() as tmp_cache_dir:
											a_		:  int											=      NestedBeamDataset(cache_dir=__SCREAMING_SNAKE_CASE  ,						beam_runner='''DirectRunner'''					)
											builder.download_and_prepare()
											self.assertTrue(
											    os.path.exists(
											        os.path.join(__SCREAMING_SNAKE_CASE  ,						builder.name  ,						'''default'''  ,						'''0.0.0'''  ,						f'{builder.name}-train.arrow'					)					)					)
											self.assertDictEqual(
											    builder.info.features  ,						datasets.Features({'''a''': datasets.Sequence({'''b''': datasets.Value('''string'''					)}					)}					)					)
											a_		:  List[str]											=      builder.as_dataset()
											self.assertEqual(dset['''train'''].num_rows  ,						__SCREAMING_SNAKE_CASE					)
											self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples  ,						__SCREAMING_SNAKE_CASE					)
											self.assertDictEqual(dset['''train'''][0]  ,						get_test_nested_examples()[0][1]					)
											self.assertDictEqual(
											    dset['''train'''][expected_num_examples - 1]  ,						get_test_nested_examples()[expected_num_examples - 1][1]					)
											self.assertTrue(
											    os.path.exists(os.path.join(__SCREAMING_SNAKE_CASE  ,						builder.name  ,						'''default'''  ,						'''0.0.0'''  ,						'''dataset_info.json'''					)					)					)
											del dset












'''simple docstring'''







from __future__ import annotations





def 		_UpperCAmelCase   (							__A							:   tuple[int, int]						,		__A							:   int      ):
				a_				,			a_		:  List[str]											=      position
				a_		:  Optional[int]											=      [
				    (y + 1, x + 2),
				    (y - 1, x + 2),
				    (y + 1, x - 2),
				    (y - 1, x - 2),
				    (y + 2, x + 1),
				    (y + 2, x - 1),
				    (y - 2, x + 1),
				    (y - 2, x - 1),
				]
				a_		:  Any											=      []

				for position in positions:
								a_				,			a_		:  Dict											=      position
								if 0 <= y_test < n and 0 <= x_test < n:
												permissible_positions.append(__A      )

				return permissible_positions





def 		_UpperCAmelCase   (							__A							:   list[list[int]]      ):
				return not any(elem == 0 for row in board for elem in row      )





def 		_UpperCAmelCase   (							__A							:   list[list[int]]						,		__A							:   tuple[int, int]						,		__A							:   int      ):

				if is_complete(__A      ):
								return True

				for position in get_valid_pos(__A						,		len(__A      )      ):
								a_				,			a_		:  Dict											=      position

								if board[y][x] == 0:
												a_		:  Optional[Any]											=      curr + 1
												if open_knight_tour_helper(__A						,		__A						,		curr + 1      ):
																return True
												a_		:  Tuple											=      0

				return False





def 		_UpperCAmelCase   (							__A							:   int      ):
				a_		:  List[str]											=      [[0 for i in range(__A      )] for j in range(__A      )]

				for i in range(__A      ):
								for j in range(__A      ):
												a_		:  Optional[Any]											=      1
												if open_knight_tour_helper(__A						,		(i, j)						,		1      ):
																return board
												a_		:  Union[str, Any]											=      0

				a_		:  Dict											=      f'Open Kight Tour cannot be performed on a board of size {n}'
				raise ValueError(__A      )


if __name__ == "__main__":
		import doctest

		doctest.testmod()




