builtins.py 6.1 KB

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