builtins.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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):
  87. if '{}' in self:
  88. for i in range(len(args)):
  89. self = self.replace('{}', str(args[i]), 1)
  90. else:
  91. for i in range(len(args)):
  92. self = self.replace('{'+str(i)+'}', str(args[i]))
  93. return self
  94. str.format = __f
  95. def __f(self, chars=None):
  96. chars = chars or ' \t\n\r'
  97. i = 0
  98. while i < len(self) and self[i] in chars:
  99. ++i
  100. return self[i:]
  101. str.lstrip = __f
  102. def __f(self, chars=None):
  103. chars = chars or ' \t\n\r'
  104. j = len(self) - 1
  105. while j >= 0 and self[j] in chars:
  106. --j
  107. return self[:j+1]
  108. str.rstrip = __f
  109. def __f(self, chars=None):
  110. chars = chars or ' \t\n\r'
  111. i = 0
  112. while i < len(self) and self[i] in chars:
  113. ++i
  114. j = len(self) - 1
  115. while j >= 0 and self[j] in chars:
  116. --j
  117. return self[i:j+1]
  118. str.strip = __f
  119. def __f(self, width: int):
  120. delta = width - len(self)
  121. if delta <= 0:
  122. return self
  123. return '0' * delta + self
  124. str.zfill = __f
  125. def __f(self, width: int, fillchar=' '):
  126. delta = width - len(self)
  127. if delta <= 0:
  128. return self
  129. assert len(fillchar) == 1
  130. return fillchar * delta + self
  131. str.rjust = __f
  132. def __f(self, width: int, fillchar=' '):
  133. delta = width - len(self)
  134. if delta <= 0:
  135. return self
  136. assert len(fillchar) == 1
  137. return self + fillchar * delta
  138. str.ljust = __f
  139. ##### list #####
  140. def __qsort(a: list, L: int, R: int, key):
  141. if L >= R: return;
  142. mid = a[(R+L)//2];
  143. mid = key(mid)
  144. i, j = L, R
  145. while i<=j:
  146. while key(a[i])<mid: ++i;
  147. while key(a[j])>mid: --j;
  148. if i<=j:
  149. a[i], a[j] = a[j], a[i]
  150. ++i; --j;
  151. __qsort(a, L, j, key)
  152. __qsort(a, i, R, key)
  153. def __f(self, reverse=False, key=None):
  154. if key is None:
  155. key = lambda x:x
  156. __qsort(self, 0, len(self)-1, key)
  157. if reverse:
  158. self.reverse()
  159. list.sort = __f
  160. def __f(self, other):
  161. for i, j in zip(self, other):
  162. if i != j:
  163. return i < j
  164. return len(self) < len(other)
  165. tuple.__lt__ = __f
  166. list.__lt__ = __f
  167. def __f(self, other):
  168. for i, j in zip(self, other):
  169. if i != j:
  170. return i > j
  171. return len(self) > len(other)
  172. tuple.__gt__ = __f
  173. list.__gt__ = __f
  174. def __f(self, other):
  175. for i, j in zip(self, other):
  176. if i != j:
  177. return i <= j
  178. return len(self) <= len(other)
  179. tuple.__le__ = __f
  180. list.__le__ = __f
  181. def __f(self, other):
  182. for i, j in zip(self, other):
  183. if i != j:
  184. return i >= j
  185. return len(self) >= len(other)
  186. tuple.__ge__ = __f
  187. list.__ge__ = __f
  188. type.__repr__ = lambda self: "<class '" + self.__name__ + "'>"
  189. type.__getitem__ = lambda self, *args: self # for generics
  190. def help(obj):
  191. if hasattr(obj, '__func__'):
  192. obj = obj.__func__
  193. print(obj.__signature__)
  194. print(obj.__doc__)
  195. del __f
  196. class Exception: pass
  197. from _long import long