builtins.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import sys as _sys
  2. def print(*args, sep=' ', end='\n'):
  3. s = sep.join([str(i) for i in args])
  4. _sys.stdout.write(s + end)
  5. def max(*args, key=None):
  6. if key is None:
  7. key = lambda x: x
  8. if len(args) == 0:
  9. raise TypeError('max expected 1 arguments, got 0')
  10. if len(args) == 1:
  11. args = args[0]
  12. args = iter(args)
  13. res = next(args)
  14. if res is StopIteration:
  15. raise ValueError('max() arg is an empty sequence')
  16. while True:
  17. i = next(args)
  18. if i is StopIteration:
  19. break
  20. if key(i) > key(res):
  21. res = i
  22. return res
  23. def min(*args, key=None):
  24. if key is None:
  25. key = lambda x: x
  26. if len(args) == 0:
  27. raise TypeError('min expected 1 arguments, got 0')
  28. if len(args) == 1:
  29. args = args[0]
  30. args = iter(args)
  31. res = next(args)
  32. if res is StopIteration:
  33. raise ValueError('min() arg is an empty sequence')
  34. while True:
  35. i = next(args)
  36. if i is StopIteration:
  37. break
  38. if key(i) < key(res):
  39. res = i
  40. return res
  41. def all(iterable):
  42. for i in iterable:
  43. if not i:
  44. return False
  45. return True
  46. def any(iterable):
  47. for i in iterable:
  48. if i:
  49. return True
  50. return False
  51. def enumerate(iterable, start=0):
  52. n = start
  53. for elem in iterable:
  54. yield n, elem
  55. ++n
  56. def sum(iterable):
  57. res = 0
  58. for i in iterable:
  59. res += i
  60. return res
  61. def map(f, iterable):
  62. for i in iterable:
  63. yield f(i)
  64. def filter(f, iterable):
  65. for i in iterable:
  66. if f(i):
  67. yield i
  68. def zip(a, b):
  69. a = iter(a)
  70. b = iter(b)
  71. while True:
  72. ai = next(a)
  73. bi = next(b)
  74. if ai is StopIteration or bi is StopIteration:
  75. break
  76. yield ai, bi
  77. def reversed(iterable):
  78. a = list(iterable)
  79. a.reverse()
  80. return a
  81. def sorted(iterable, reverse=False, key=None):
  82. a = list(iterable)
  83. a.sort(reverse=reverse, key=key)
  84. return a
  85. ##### str #####
  86. def __f(self, *args, **kwargs):
  87. if '{}' in self:
  88. for i in range(len(args)):
  89. self = self.replace('{}', str(args[i]), 1)
  90. else:
  91. # Positional arguments will be followed by keyword arguments
  92. # 1. Replace the positional arguments
  93. for i,a in enumerate(args):
  94. self = self.replace('{'+str(i)+'}', str(a))
  95. # 2. Replace the keyword arguments
  96. for k,v in kwargs.items():
  97. self = self.replace('{'+k+'}', str(v))
  98. return self
  99. str.format = __f
  100. def __f(self, chars=None):
  101. chars = chars or ' \t\n\r'
  102. i = 0
  103. while i < len(self) and self[i] in chars:
  104. ++i
  105. return self[i:]
  106. str.lstrip = __f
  107. def __f(self, chars=None):
  108. chars = chars or ' \t\n\r'
  109. j = len(self) - 1
  110. while j >= 0 and self[j] in chars:
  111. --j
  112. return self[:j+1]
  113. str.rstrip = __f
  114. def __f(self, chars=None):
  115. chars = chars or ' \t\n\r'
  116. i = 0
  117. while i < len(self) and self[i] in chars:
  118. ++i
  119. j = len(self) - 1
  120. while j >= 0 and self[j] in chars:
  121. --j
  122. return self[i:j+1]
  123. str.strip = __f
  124. def __f(self, width: int):
  125. delta = width - len(self)
  126. if delta <= 0:
  127. return self
  128. return '0' * delta + self
  129. str.zfill = __f
  130. def __f(self, width: int, fillchar=' '):
  131. delta = width - len(self)
  132. if delta <= 0:
  133. return self
  134. assert len(fillchar) == 1
  135. return fillchar * delta + self
  136. str.rjust = __f
  137. def __f(self, width: int, fillchar=' '):
  138. delta = width - len(self)
  139. if delta <= 0:
  140. return self
  141. assert len(fillchar) == 1
  142. return self + fillchar * delta
  143. str.ljust = __f
  144. ##### list #####
  145. def __qsort(a: list, L: int, R: int, key):
  146. if L >= R: return;
  147. mid = a[(R+L)//2];
  148. mid = key(mid)
  149. i, j = L, R
  150. while i<=j:
  151. while key(a[i])<mid: ++i;
  152. while key(a[j])>mid: --j;
  153. if i<=j:
  154. a[i], a[j] = a[j], a[i]
  155. ++i; --j;
  156. __qsort(a, L, j, key)
  157. __qsort(a, i, R, key)
  158. def __f(self, reverse=False, key=None):
  159. if key is None:
  160. key = lambda x:x
  161. __qsort(self, 0, len(self)-1, key)
  162. if reverse:
  163. self.reverse()
  164. list.sort = __f
  165. def __f(self, other):
  166. for i, j in zip(self, other):
  167. if i != j:
  168. return i < j
  169. return len(self) < len(other)
  170. tuple.__lt__ = __f
  171. list.__lt__ = __f
  172. def __f(self, other):
  173. for i, j in zip(self, other):
  174. if i != j:
  175. return i > j
  176. return len(self) > len(other)
  177. tuple.__gt__ = __f
  178. list.__gt__ = __f
  179. def __f(self, other):
  180. for i, j in zip(self, other):
  181. if i != j:
  182. return i <= j
  183. return len(self) <= len(other)
  184. tuple.__le__ = __f
  185. list.__le__ = __f
  186. def __f(self, other):
  187. for i, j in zip(self, other):
  188. if i != j:
  189. return i >= j
  190. return len(self) >= len(other)
  191. tuple.__ge__ = __f
  192. list.__ge__ = __f
  193. type.__repr__ = lambda self: "<class '" + self.__name__ + "'>"
  194. type.__getitem__ = lambda self, *args: self # for generics
  195. def help(obj):
  196. if hasattr(obj, '__func__'):
  197. obj = obj.__func__
  198. print(obj.__signature__)
  199. print(obj.__doc__)
  200. del __f
  201. class Exception: pass
  202. from _long import long