





from collections import Counter
from pathlib import Path
from typing import Optional, Tuple

import yaml


class 						lowerCamelCase       (yaml.SafeLoader      ):







							"""simple docstring"""




							def  A_     ( self     :					Dict,			_UpperCAmelCase     :					Dict						)				->			str:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      str     =						[self.constructed_objects[key_node] for key_node, _ in node.value]
														SCREAMING_SNAKE_CASE__       :      str     =						[tuple(_UpperCAmelCase						) if isinstance(_UpperCAmelCase,			_UpperCAmelCase						) else key for key in keys]
														SCREAMING_SNAKE_CASE__       :      List[str]     =						Counter(_UpperCAmelCase						)
														SCREAMING_SNAKE_CASE__       :      List[str]     =						[key for key in counter if counter[key] > 1]
														if duplicate_keys:
																					raise TypeError(F'''Got duplicate yaml keys: {duplicate_keys}'''						)






							def  A_     ( self     :					Tuple,			_UpperCAmelCase     :					Any,			_UpperCAmelCase     :					List[str]=False						)				->			List[Any]:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						super().construct_mapping(_UpperCAmelCase,			deep=_UpperCAmelCase						)
														self._check_no_duplicates_on_constructed_node(_UpperCAmelCase						)
														return mapping




def _a (  SCREAMING_SNAKE_CASE__      :  str       )	->    Tuple[Optional[str], str]:

							'''simple docstring'''






							SCREAMING_SNAKE_CASE__       :      str     =						list(readme_content.splitlines()       )
							if full_content and full_content[0] == "---" and "---" in full_content[1:]:
														SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						full_content[1:].index("---"       ) + 1
														SCREAMING_SNAKE_CASE__       :      Any     =						"\n".join(full_content[1:sep_idx]       )
														return yamlblock, "\n".join(full_content[sep_idx + 1 :]       )

							return None, "\n".join(SCREAMING_SNAKE_CASE__       )


class 						lowerCamelCase       (__lowerCamelCase      ):







							"""simple docstring"""




							UpperCAmelCase_      						=   {"train_eval_index"}  # train-eval-index in the YAML metadata






							@classmethod
							def  A_     ( cls     :					int,			_UpperCAmelCase     :					Path						)				->			"DatasetMetadata":





														"""simple docstring"""
														with open(_UpperCAmelCase,			encoding="utf-8"						) as readme_file:
																					SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      int     =						_split_yaml_from_readme(readme_file.read()						)
														if yaml_string is not None:
																					return cls.from_yaml_string(_UpperCAmelCase						)
														else:
																					return cls()






							def  A_     ( self     :					Optional[Any],			_UpperCAmelCase     :					Path						)				->			List[str]:





														"""simple docstring"""
														if path.exists():
																					with open(_UpperCAmelCase,			encoding="utf-8"						) as readme_file:
																												SCREAMING_SNAKE_CASE__       :      Any     =						readme_file.read()
														else:
																					SCREAMING_SNAKE_CASE__       :      str     =						None
														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						self._to_readme(_UpperCAmelCase						)
														with open(_UpperCAmelCase,			"w",			encoding="utf-8"						) as readme_file:
																					readme_file.write(_UpperCAmelCase						)






							def  A_     ( self     :					Dict,			_UpperCAmelCase     :					Optional[str] = None						)				->			str:





														"""simple docstring"""
														if readme_content is not None:
																					SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      Tuple     =						_split_yaml_from_readme(_UpperCAmelCase						)
																					SCREAMING_SNAKE_CASE__       :      List[Any]     =						"---\n" + self.to_yaml_string() + "---\n" + content
														else:
																					SCREAMING_SNAKE_CASE__       :      Optional[int]     =						"---\n" + self.to_yaml_string() + "---\n"
														return full_content






							@classmethod
							def  A_     ( cls     :					str,			_UpperCAmelCase     :					str						)				->			"DatasetMetadata":





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      Any     =						yaml.load(_UpperCAmelCase,			Loader=_NoDuplicateSafeLoader						) or {}

														# Convert the YAML keys to DatasetMetadata fields
														SCREAMING_SNAKE_CASE__       :      List[Any]     =						{
														    (key.replace("-",			"_"						) if key.replace("-",			"_"						) in cls._FIELDS_WITH_DASHES else key): value
														    for key, value in metadata_dict.items()
														}
														return cls(**_UpperCAmelCase						)






							def  A_     ( self     :					Optional[Any]						)				->			str:





														"""simple docstring"""
														return yaml.safe_dump(
														    {
														        (key.replace("_",			"-"						) if key in self._FIELDS_WITH_DASHES else key): value
														        for key, value in self.items()
														    },			sort_keys=_UpperCAmelCase,			allow_unicode=_UpperCAmelCase,			encoding="utf-8",			).decode("utf-8"						)


_lowerCamelCase :    Tuple  							= {
    '''image-classification''': [],
    '''translation''': [],
    '''image-segmentation''': [],
    '''fill-mask''': [],
    '''automatic-speech-recognition''': [],
    '''token-classification''': [],
    '''sentence-similarity''': [],
    '''audio-classification''': [],
    '''question-answering''': [],
    '''summarization''': [],
    '''zero-shot-classification''': [],
    '''table-to-text''': [],
    '''feature-extraction''': [],
    '''other''': [],
    '''multiple-choice''': [],
    '''text-classification''': [],
    '''text-to-image''': [],
    '''text2text-generation''': [],
    '''zero-shot-image-classification''': [],
    '''tabular-classification''': [],
    '''tabular-regression''': [],
    '''image-to-image''': [],
    '''tabular-to-text''': [],
    '''unconditional-image-generation''': [],
    '''text-retrieval''': [],
    '''text-to-speech''': [],
    '''object-detection''': [],
    '''audio-to-audio''': [],
    '''text-generation''': [],
    '''conversational''': [],
    '''table-question-answering''': [],
    '''visual-question-answering''': [],
    '''image-to-text''': [],
    '''reinforcement-learning''': [],
    '''voice-activity-detection''': [],
    '''time-series-forecasting''': [],
    '''document-question-answering''': [],
}


if __name__ == "__main__":
		from argparse import ArgumentParser

		_lowerCamelCase :    Tuple  							= ArgumentParser(usage='''Validate the yaml metadata block of a README.md file.''')
		ap.add_argument('''readme_filepath''')
		_lowerCamelCase :    Optional[int]  							= ap.parse_args()

		_lowerCamelCase :    Tuple  							= Path(args.readme_filepath)
		_lowerCamelCase :    Optional[int]  							= DatasetMetadata.from_readme(readme_filepath)
		print(dataset_metadata)
		dataset_metadata.to_readme(readme_filepath)













class 						lowerCamelCase       :







							"""simple docstring"""




							def __init__( self     :					int,			_UpperCAmelCase     :					Dict,			_UpperCAmelCase     :					str						)				->			Optional[Any]:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      str     =						name
														SCREAMING_SNAKE_CASE__       :      Tuple     =						val






							def __str__( self     :					str						)				->			List[Any]:





														"""simple docstring"""
														return F'''{self.__class__.__name__}({self.name}, {self.val})'''






							def __lt__( self     :					str,			_UpperCAmelCase     :					Dict						)				->			Union[str, Any]:





														"""simple docstring"""
														return self.val < other.val


class 						lowerCamelCase       :







							"""simple docstring"""




							def __init__( self     :					Optional[Any],			_UpperCAmelCase     :					str						)				->			Tuple:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						{}
														SCREAMING_SNAKE_CASE__       :      str     =						{}
														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						self.build_heap(_UpperCAmelCase						)






							def __getitem__( self     :					Union[str, Any],			_UpperCAmelCase     :					Any						)				->			Dict:





														"""simple docstring"""
														return self.get_value(_UpperCAmelCase						)






							def  A_     ( self     :					int,			_UpperCAmelCase     :					Optional[int]						)				->			Any:





														"""simple docstring"""
														return (idx - 1) // 2






							def  A_     ( self     :					Optional[Any],			_UpperCAmelCase     :					Any						)				->			Dict:





														"""simple docstring"""
														return idx * 2 + 1






							def  A_     ( self     :					str,			_UpperCAmelCase     :					List[str]						)				->			Union[str, Any]:





														"""simple docstring"""
														return idx * 2 + 2






							def  A_     ( self     :					Tuple,			_UpperCAmelCase     :					int						)				->			Optional[int]:





														"""simple docstring"""
														return self.heap_dict[key]






							def  A_     ( self     :					Optional[int],			_UpperCAmelCase     :					Dict						)				->			Optional[Any]:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      List[str]     =						len(_UpperCAmelCase						) - 1
														SCREAMING_SNAKE_CASE__       :      Optional[int]     =						self.get_parent_idx(_UpperCAmelCase						)

														for idx, i in enumerate(_UpperCAmelCase						):
																					SCREAMING_SNAKE_CASE__       :      Optional[int]     =						idx
																					SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						i.val

														for i in range(_UpperCAmelCase,			-1,			-1						):
																					self.sift_down(_UpperCAmelCase,			_UpperCAmelCase						)
														return array






							def  A_     ( self     :					List[Any],			_UpperCAmelCase     :					Dict,			_UpperCAmelCase     :					Tuple						)				->			Tuple:





														"""simple docstring"""
														while True:
																					SCREAMING_SNAKE_CASE__       :      str     =						self.get_left_child_idx(_UpperCAmelCase						)  # noqa: E741
																					SCREAMING_SNAKE_CASE__       :      Tuple     =						self.get_right_child_idx(_UpperCAmelCase						)

																					SCREAMING_SNAKE_CASE__       :      Tuple     =						idx
																					if l < len(_UpperCAmelCase						) and array[l] < array[idx]:
																												SCREAMING_SNAKE_CASE__       :      List[Any]     =						l
																					if r < len(_UpperCAmelCase						) and array[r] < array[smallest]:
																												SCREAMING_SNAKE_CASE__       :      int     =						r

																					if smallest != idx:
																												SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      Any     =						array[smallest], array[idx]
																												(
																												    (
																												    SCREAMING_SNAKE_CASE__
																												)   ,(
																												    SCREAMING_SNAKE_CASE__
																												)   ,
																												)       :      Optional[Any]     =						(
																												    self.idx_of_element[array[smallest]],
																												    self.idx_of_element[array[idx]],
																												)
																												SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						smallest
																					else:
																												break






							def  A_     ( self     :					Union[str, Any],			_UpperCAmelCase     :					str						)				->			Any:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      Any     =						self.get_parent_idx(_UpperCAmelCase						)
														while p >= 0 and self.heap[p] > self.heap[idx]:
																					SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      Any     =						self.heap[idx], self.heap[p]
																					SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      int     =						(
																					    self.idx_of_element[self.heap[idx]],
																					    self.idx_of_element[self.heap[p]],
																					)
																					SCREAMING_SNAKE_CASE__       :      Dict     =						p
																					SCREAMING_SNAKE_CASE__       :      List[str]     =						self.get_parent_idx(_UpperCAmelCase						)






							def  A_     ( self     :					str						)				->			List[str]:





														"""simple docstring"""
														return self.heap[0]






							def  A_     ( self     :					Tuple						)				->			List[Any]:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      Optional[int]     =						self.heap[-1], self.heap[0]
														SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      List[Any]     =						(
														    self.idx_of_element[self.heap[-1]],
														    self.idx_of_element[self.heap[0]],
														)

														SCREAMING_SNAKE_CASE__       :      Any     =						self.heap.pop()
														del self.idx_of_element[x]
														self.sift_down(0,			self.heap						)
														return x






							def  A_     ( self     :					Union[str, Any],			_UpperCAmelCase     :					str						)				->			Optional[Any]:





														"""simple docstring"""
														self.heap.append(_UpperCAmelCase						)
														SCREAMING_SNAKE_CASE__       :      str     =						len(self.heap						) - 1
														SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						node.val
														self.sift_up(len(self.heap						) - 1						)






							def  A_     ( self     :					Optional[int]						)				->			int:





														"""simple docstring"""
														return len(self.heap						) == 0






							def  A_     ( self     :					Any,			_UpperCAmelCase     :					Tuple,			_UpperCAmelCase     :					str						)				->			Dict:





														"""simple docstring"""
														assert (
														    self.heap[self.idx_of_element[node]].val > new_value
														), "newValue must be less that current value"
														SCREAMING_SNAKE_CASE__       :      Tuple     =						new_value
														SCREAMING_SNAKE_CASE__       :      List[Any]     =						new_value
														self.sift_up(self.idx_of_element[node]						)


_lowerCamelCase :    Tuple  							= Node('''R''', -1)
_lowerCamelCase :    int  							= Node('''B''', 6)
_lowerCamelCase :    str  							= Node('''A''', 3)
_lowerCamelCase :    Optional[Any]  							= Node('''X''', 1)
_lowerCamelCase :    str  							= Node('''E''', 4)

# Use one of these two ways to generate Min-Heap

# Generating Min-Heap from array
_lowerCamelCase :    int  							= MinHeap([r, b, a, x, e])

# Generating Min-Heap by Insert method
# myMinHeap.insert(a)
# myMinHeap.insert(b)
# myMinHeap.insert(x)
# myMinHeap.insert(r)
# myMinHeap.insert(e)

# Before
print('''Min Heap - before decrease key''')
for i in my_min_heap.heap:
		print(i)

print('''Min Heap - After decrease key of node [B -> -17]''')
my_min_heap.decrease_key(b, -1_7)

# After
for i in my_min_heap.heap:
		print(i)

if __name__ == "__main__":
		import doctest

		doctest.testmod()






