builtins.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. sep = sep or ' '
  94. if sep == "":
  95. return list(self)
  96. res = []
  97. i = 0
  98. while i < len(self):
  99. if self[i:i+len(sep)] == sep:
  100. res.append(self[:i])
  101. self = self[i+len(sep):]
  102. i = 0
  103. else:
  104. ++i
  105. res.append(self)
  106. return res
  107. str.split = __f
  108. def __f(self, *args):
  109. if '{}' in self:
  110. for i in range(len(args)):
  111. self = self.replace('{}', str(args[i]), 1)
  112. else:
  113. for i in range(len(args)):
  114. self = self.replace('{'+str(i)+'}', str(args[i]))
  115. return self
  116. str.format = __f
  117. def __f(self, chars=None):
  118. chars = chars or ' \t\n\r'
  119. i = 0
  120. while i < len(self) and self[i] in chars:
  121. ++i
  122. return self[i:]
  123. str.lstrip = __f
  124. def __f(self, chars=None):
  125. chars = chars or ' \t\n\r'
  126. j = len(self) - 1
  127. while j >= 0 and self[j] in chars:
  128. --j
  129. return self[:j+1]
  130. str.rstrip = __f
  131. def __f(self, chars=None):
  132. chars = chars or ' \t\n\r'
  133. i = 0
  134. while i < len(self) and self[i] in chars:
  135. ++i
  136. j = len(self) - 1
  137. while j >= 0 and self[j] in chars:
  138. --j
  139. return self[i:j+1]
  140. str.strip = __f
  141. def __f(self, width: int):
  142. delta = width - len(self)
  143. if delta <= 0:
  144. return self
  145. return '0' * delta + self
  146. str.zfill = __f
  147. def __f(self, width: int, fillchar=' '):
  148. delta = width - len(self)
  149. if delta <= 0:
  150. return self
  151. assert len(fillchar) == 1
  152. return fillchar * delta + self
  153. str.rjust = __f
  154. def __f(self, width: int, fillchar=' '):
  155. delta = width - len(self)
  156. if delta <= 0:
  157. return self
  158. assert len(fillchar) == 1
  159. return self + fillchar * delta
  160. str.ljust = __f
  161. ##### list #####
  162. list.__repr__ = lambda self: '[' + ', '.join([repr(i) for i in self]) + ']'
  163. list.__json__ = lambda self: '[' + ', '.join([i.__json__() for i in self]) + ']'
  164. tuple.__json__ = lambda self: '[' + ', '.join([i.__json__() for i in self]) + ']'
  165. def __f(self):
  166. if len(self) == 1:
  167. return '(' + repr(self[0]) + ',)'
  168. return '(' + ', '.join([repr(i) for i in self]) + ')'
  169. tuple.__repr__ = __f
  170. def __qsort(a: list, L: int, R: int, key):
  171. if L >= R: return;
  172. mid = a[(R+L)//2];
  173. mid = key(mid)
  174. i, j = L, R
  175. while i<=j:
  176. while key(a[i])<mid: ++i;
  177. while key(a[j])>mid: --j;
  178. if i<=j:
  179. a[i], a[j] = a[j], a[i]
  180. ++i; --j;
  181. __qsort(a, L, j, key)
  182. __qsort(a, i, R, key)
  183. def __f(self, reverse=False, key=None):
  184. if key is None:
  185. key = lambda x:x
  186. __qsort(self, 0, len(self)-1, key)
  187. if reverse:
  188. self.reverse()
  189. list.sort = __f
  190. def __f(self, other):
  191. for i, j in zip(self, other):
  192. if i != j:
  193. return i < j
  194. return len(self) < len(other)
  195. tuple.__lt__ = __f
  196. list.__lt__ = __f
  197. def __f(self, other):
  198. for i, j in zip(self, other):
  199. if i != j:
  200. return i > j
  201. return len(self) > len(other)
  202. tuple.__gt__ = __f
  203. list.__gt__ = __f
  204. def __f(self, other):
  205. for i, j in zip(self, other):
  206. if i != j:
  207. return i <= j
  208. return len(self) <= len(other)
  209. tuple.__le__ = __f
  210. list.__le__ = __f
  211. def __f(self, other):
  212. for i, j in zip(self, other):
  213. if i != j:
  214. return i >= j
  215. return len(self) >= len(other)
  216. tuple.__ge__ = __f
  217. list.__ge__ = __f
  218. type.__repr__ = lambda self: "<class '" + self.__name__ + "'>"
  219. def help(obj):
  220. if hasattr(obj, '__func__'):
  221. obj = obj.__func__
  222. if hasattr(obj, '__doc__'):
  223. print(obj.__doc__)
  224. else:
  225. print("No docstring found")
  226. del __f
  227. from _long import long