builtins.py 5.8 KB

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