





"""simple docstring"""




#
# Sort large text files in a minimum amount of memory
#
import argparse
import os




class 				_a		:
				UpperCamelCase			=					"""block_{0}.dat"""






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




										'''simple docstring'''



										_UpperCamelCase    :      Union[str, Any]						     =					filename
										_UpperCamelCase    :      List[Any]						     =					[]






				def        snake_case			(				self	:					Optional[Any],				lowerCAmelCase__	:					Optional[Any],				lowerCAmelCase__	:					Optional[Any]							)			->   List[str]:




										'''simple docstring'''



										_UpperCamelCase    :      Optional[Any]						     =					self.BLOCK_FILENAME_FORMAT.format(lowerCAmelCase_							)
										with open(lowerCAmelCase_,				'''w'''							) as file:
																file.write(lowerCAmelCase_							)
										self.block_filenames.append(lowerCAmelCase_							)






				def        snake_case			(				self	:					Tuple							)			->   str:




										'''simple docstring'''



										return self.block_filenames






				def        snake_case			(				self	:					List[Any],				lowerCAmelCase__	:					Tuple,				lowerCAmelCase__	:					Optional[int]=None							)			->   Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase    :      Optional[Any]						     =					0
										with open(self.filename							) as file:
																while True:
																						_UpperCamelCase    :      Optional[Any]						     =					file.readlines(lowerCAmelCase_							)

																						if lines == []:
																												break

																						if sort_key is None:
																												lines.sort()
																						else:
																												lines.sort(key=lowerCAmelCase_							)

																						self.write_block(''''''.join(lowerCAmelCase_							),				lowerCAmelCase_							)
																						i += 1






				def        snake_case			(				self	:					Any							)			->   str:




										'''simple docstring'''



										map(os.remove,				self.block_filenames							)




class 				_a		:






				def        snake_case			(				self	:					Any,				lowerCAmelCase__	:					str							)			->   Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase    :      Optional[Any]						     =					-1
										_UpperCamelCase    :      List[str]						     =					None

										for i in range(len(lowerCAmelCase_							)							):
																if min_str is None or choices[i] < min_str:
																						_UpperCamelCase    :      Optional[int]						     =					i

										return min_index




class 				_a		:






				def __init__(				self	:					Tuple,				lowerCAmelCase__	:					Tuple							)			->   Union[str, Any]:




										'''simple docstring'''



										_UpperCamelCase    :      List[Any]						     =					files
										_UpperCamelCase    :      str						     =					set()
										_UpperCamelCase    :      Optional[int]						     =					len(lowerCAmelCase_							)
										_UpperCamelCase    :      Any						     =					{i: None for i in range(self.num_buffers							)}






				def        snake_case			(				self	:					int							)			->   Any:




										'''simple docstring'''



										return {
										    i: self.buffers[i] for i in range(self.num_buffers							) if i not in self.empty
										}






				def        snake_case			(				self	:					Dict							)			->   int:




										'''simple docstring'''



										for i in range(self.num_buffers							):
																if self.buffers[i] is None and i not in self.empty:
																						_UpperCamelCase    :      Optional[Any]						     =					self.files[i].readline()

																						if self.buffers[i] == "":
																												self.empty.add(lowerCAmelCase_							)
																												self.files[i].close()

										if len(self.empty							) == self.num_buffers:
																return False

										return True






				def        snake_case			(				self	:					int,				lowerCAmelCase__	:					Union[str, Any]							)			->   int:




										'''simple docstring'''



										_UpperCamelCase    :      Dict						     =					self.buffers[index]
										_UpperCamelCase    :      Dict						     =					None

										return value




class 				_a		:






				def __init__(				self	:					Any,				lowerCAmelCase__	:					int							)			->   Union[str, Any]:




										'''simple docstring'''



										_UpperCamelCase    :      Union[str, Any]						     =					merge_strategy






				def        snake_case			(				self	:					List[str],				lowerCAmelCase__	:					Dict,				lowerCAmelCase__	:					Union[str, Any],				lowerCAmelCase__	:					Union[str, Any]							)			->   str:




										'''simple docstring'''



										_UpperCamelCase    :      Dict						     =					FilesArray(self.get_file_handles(lowerCAmelCase_,				lowerCAmelCase_							)							)
										with open(lowerCAmelCase_,				'''w''',				lowerCAmelCase_							) as outfile:
																while buffers.refresh():
																						_UpperCamelCase    :      List[str]						     =					self.merge_strategy.select(buffers.get_dict()							)
																						outfile.write(buffers.unshift(lowerCAmelCase_							)							)






				def        snake_case			(				self	:					List[str],				lowerCAmelCase__	:					Union[str, Any],				lowerCAmelCase__	:					str							)			->   Tuple:




										'''simple docstring'''



										_UpperCamelCase    :      Union[str, Any]						     =					{}

										for i in range(len(lowerCAmelCase_							)							):
																_UpperCamelCase    :      Any						     =					open(filenames[i],				'''r''',				lowerCAmelCase_							)  # noqa: UP015

										return files




class 				_a		:






				def __init__(				self	:					int,				lowerCAmelCase__	:					List[Any]							)			->   str:




										'''simple docstring'''



										_UpperCamelCase    :      str						     =					block_size






				def        snake_case			(				self	:					Optional[Any],				lowerCAmelCase__	:					Optional[int],				lowerCAmelCase__	:					int=None							)			->   int:




										'''simple docstring'''



										_UpperCamelCase    :      Union[str, Any]						     =					self.get_number_blocks(lowerCAmelCase_,				self.block_size							)
										_UpperCamelCase    :      List[Any]						     =					FileSplitter(lowerCAmelCase_							)
										splitter.split(self.block_size,				lowerCAmelCase_							)

										_UpperCamelCase    :      Optional[int]						     =					FileMerger(NWayMerge()							)
										_UpperCamelCase    :      Any						     =					self.block_size / (num_blocks + 1)
										merger.merge(splitter.get_block_filenames(),				filename + '''.out''',				lowerCAmelCase_							)

										splitter.cleanup()






				def        snake_case			(				self	:					int,				lowerCAmelCase__	:					Dict,				lowerCAmelCase__	:					Tuple							)			->   List[Any]:




										'''simple docstring'''



										return (os.stat(lowerCAmelCase_							).st_size / block_size) + 1





def  a_       (							_lowercase     ):



						if string[-1].lower() == "k":
												return int(string[:-1]     ) * 1024
						elif string[-1].lower() == "m":
												return int(string[:-1]     ) * 1024 * 1024
						elif string[-1].lower() == "g":
												return int(string[:-1]     ) * 1024 * 1024 * 1024
						else:
												return int(_lowercase     )





def  a_       (							):
						_UpperCamelCase    :      Dict						     =					argparse.ArgumentParser()
						parser.add_argument(
						    '''-m''' ,		'''--mem''' ,		help='''amount of memory to use for sorting''' ,		default='''100M'''     )
						parser.add_argument(
						    '''filename''' ,		metavar='''<filename>''' ,		nargs=1 ,		help='''name of file to sort'''     )
						_UpperCamelCase    :      Union[str, Any]						     =					parser.parse_args()

						_UpperCamelCase    :      Dict						     =					ExternalSort(parse_memory(args.mem     )     )
						sorter.sort(args.filename[0]     )


if __name__ == "__main__":
							main()










"""simple docstring"""




def  a_       (							_lowercase     ):
						return 1 if input_a == 0 else 0





def  a_       (							):
						assert not_gate(0     ) == 1
						assert not_gate(1     ) == 0


if __name__ == "__main__":
						print(not_gate(0))
						print(not_gate(1))



