





import argparse
import os

class 						UpperCamelCase__					:



     '''simple docstring'''

     __snake_case       :      Optional[Any]						      =     """block_{0}.dat"""






     def __init__(				self		:       List[str]		,lowerCamelCase__		:       Optional[int]			)	->						Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		filename
           SCREAMING_SNAKE_CASE						=		[]






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Optional[Any]		,lowerCamelCase__		:       Optional[int]		,lowerCamelCase__		:       Tuple			)	->						Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.BLOCK_FILENAME_FORMAT.format(_lowerCamelCase			)
           with open(_lowerCamelCase		,"""w"""			) as file:
                 file.write(_lowerCamelCase			)
           self.block_filenames.append(_lowerCamelCase			)






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




           '''simple docstring'''


           return self.block_filenames






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Dict		,lowerCamelCase__		:       int		,lowerCamelCase__		:       Union[str, Any]=None			)	->						Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		0
           with open(self.filename			) as file:
                 while True:
                       SCREAMING_SNAKE_CASE						=		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 							SCREAMING_SNAKE_CASE__    (				self		:       Dict			)	->						int:




           '''simple docstring'''


           map(os.remove		,self.block_filenames			)

class 						UpperCamelCase__					:



     '''simple docstring'''

     def 							SCREAMING_SNAKE_CASE__    (				self		:       str		,lowerCamelCase__		:       List[str]			)	->						Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		-1
           SCREAMING_SNAKE_CASE						=		None

           for i in range(len(_lowerCamelCase			)			):
                 if min_str is None or choices[i] < min_str:
                       SCREAMING_SNAKE_CASE						=		i

           return min_index

class 						UpperCamelCase__					:



     '''simple docstring'''

     def __init__(				self		:       str		,lowerCamelCase__		:       int			)	->						Dict:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		files
           SCREAMING_SNAKE_CASE						=		set()
           SCREAMING_SNAKE_CASE						=		len(_lowerCamelCase			)
           SCREAMING_SNAKE_CASE						=		{i: None for i in range(self.num_buffers			)}






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Optional[Any]			)	->						Any:




           '''simple docstring'''


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






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




           '''simple docstring'''


           for i in range(self.num_buffers			):
                 if self.buffers[i] is None and i not in self.empty:
                       SCREAMING_SNAKE_CASE						=		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 							SCREAMING_SNAKE_CASE__    (				self		:       List[str]		,lowerCamelCase__		:       List[str]			)	->						Union[str, Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.buffers[index]
           SCREAMING_SNAKE_CASE						=		None

           return value

class 						UpperCamelCase__					:



     '''simple docstring'''

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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		merge_strategy






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Tuple		,lowerCamelCase__		:       List[str]		,lowerCamelCase__		:       Any		,lowerCamelCase__		:       Optional[int]			)	->						Union[str, Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		FilesArray(self.get_file_handles(_lowerCamelCase		,_lowerCamelCase			)			)
           with open(_lowerCamelCase		,"""w"""		,_lowerCamelCase			) as outfile:
                 while buffers.refresh():
                       SCREAMING_SNAKE_CASE						=		self.merge_strategy.select(buffers.get_dict()			)
                       outfile.write(buffers.unshift(_lowerCamelCase			)			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Any		,lowerCamelCase__		:       Union[str, Any]		,lowerCamelCase__		:       Any			)	->						Dict:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		{}

           for i in range(len(_lowerCamelCase			)			):
                 SCREAMING_SNAKE_CASE						=		open(filenames[i]		,"""r"""		,_lowerCamelCase			)  # noqa: UP015

           return files

class 						UpperCamelCase__					:



     '''simple docstring'''

     def __init__(				self		:       Tuple		,lowerCamelCase__		:       int			)	->						Optional[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		block_size






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Tuple		,lowerCamelCase__		:       List[str]		,lowerCamelCase__		:       Tuple=None			)	->						Union[str, Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.get_number_blocks(_lowerCamelCase		,self.block_size			)
           SCREAMING_SNAKE_CASE						=		FileSplitter(_lowerCamelCase			)
           splitter.split(self.block_size		,_lowerCamelCase			)

           SCREAMING_SNAKE_CASE						=		FileMerger(NWayMerge()			)
           SCREAMING_SNAKE_CASE						=		self.block_size / (num_blocks + 1)
           merger.merge(splitter.get_block_filenames()		,filename + """.out"""		,_lowerCamelCase			)

           splitter.cleanup()






     def 							SCREAMING_SNAKE_CASE__    (				self		:       List[str]		,lowerCamelCase__		:       Union[str, Any]		,lowerCamelCase__		:       List[str]			)	->						str:




           '''simple docstring'''


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



def   __lowercase  ( _SCREAMING_SNAKE_CASE      )					->  Union[str, Any]:


      '''simple docstring'''


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



def   __lowercase  ( )					->  Any:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE						=		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"""      )
      SCREAMING_SNAKE_CASE						=		parser.parse_args()

      SCREAMING_SNAKE_CASE						=		ExternalSort(parse_memory(args.mem      )      )
      sorter.sort(args.filename[0]      )


if __name__ == "__main__":
      main()








"""simple docstring"""

from __future__ import annotations

def 		lowercase_   (		_UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase						):




	"""simple docstring"""






	assert (
	    c % greatest_common_divisor(_UpperCAmelCase	,   _UpperCAmelCase						) == 0
	)  # greatest_common_divisor(a,b) function implemented below
	((A_) ,			(A_) ,			(A_))	:					List[Any]						 =		extended_gcd(_UpperCAmelCase	,   _UpperCAmelCase						)  # extended_gcd(a,b) function implemented below
	A_	:					int						 =		c / d
	return (r * x, r * y)

def 		lowercase_   (		_UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase = 2						):




	"""simple docstring"""






	((A_) ,			(A_))	:					Any						 =		diophantine(_UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase						)  # Initial value
	A_	:					Union[str, Any]						 =		greatest_common_divisor(_UpperCAmelCase	,   _UpperCAmelCase						)
	A_	:					List[str]						 =		a // d
	A_	:					str						 =		b // d

	for i in range(_UpperCAmelCase						):
		A_	:					int						 =		xa + i * q
		A_	:					str						 =		ya - i * p
		print(_UpperCAmelCase	,   _UpperCAmelCase						)

def 		lowercase_   (		_UpperCAmelCase	,   _UpperCAmelCase						):




	"""simple docstring"""






	if a < b:
		A_ ,			A_	:					Tuple						 =		b, a

	while a % b != 0:
		A_ ,			A_	:					int						 =		b, a % b

	return b

def 		lowercase_   (		_UpperCAmelCase	,   _UpperCAmelCase						):




	"""simple docstring"""






	assert a >= 0 and b >= 0

	if b == 0:
		A_ ,			A_ ,			A_	:					str						 =		a, 1, 0
	else:
		((A_) ,			(A_) ,			(A_))	:					List[str]						 =		extended_gcd(_UpperCAmelCase	,   a % b						)
		A_	:					Optional[int]						 =		q
		A_	:					Tuple						 =		p - q * (a // b)

	assert a % d == 0 and b % d == 0
	assert d == a * x + b * y

	return (d, x, y)


if __name__ == "__main__":
		from doctest import testmod

		testmod(name='diophantine', verbose=True)
		testmod(name='diophantine_all_soln', verbose=True)
		testmod(name='extended_gcd', verbose=True)
		testmod(name='greatest_common_divisor', verbose=True)





