builtins.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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):
  93. if sep == "":
  94. return list(self)
  95. res = []
  96. i = 0
  97. while i < len(self):
  98. if self[i:i+len(sep)] == sep:
  99. res.append(self[:i])
  100. self = self[i+len(sep):]
  101. i = 0
  102. else:
  103. ++i
  104. res.append(self)
  105. return res
  106. str.split = __f
  107. def __f(self, *args):
  108. if '{}' in self:
  109. for i in range(len(args)):
  110. self = self.replace('{}', str(args[i]), 1)
  111. else:
  112. for i in range(len(args)):
  113. self = self.replace('{'+str(i)+'}', str(args[i]))
  114. return self
  115. str.format = __f
  116. def __f(self, chars=None):
  117. chars = chars or ' \t\n\r'
  118. i = 0
  119. while i < len(self) and self[i] in chars:
  120. ++i
  121. j = len(self) - 1
  122. while j >= 0 and self[j] in chars:
  123. --j
  124. return self[i:j+1]
  125. str.strip = __f
  126. ##### list #####
  127. list.__repr__ = lambda self: '[' + ', '.join([repr(i) for i in self]) + ']'
  128. tuple.__repr__ = lambda self: '(' + ', '.join([repr(i) for i in self]) + ')'
  129. list.__json__ = lambda self: '[' + ', '.join([i.__json__() for i in self]) + ']'
  130. tuple.__json__ = lambda self: '[' + ', '.join([i.__json__() for i in self]) + ']'
  131. def __qsort(a: list, L: int, R: int, key):
  132. if L >= R: return;
  133. mid = a[(R+L)//2];
  134. mid = key(mid)
  135. i, j = L, R
  136. while i<=j:
  137. while key(a[i])<mid: ++i;
  138. while key(a[j])>mid: --j;
  139. if i<=j:
  140. a[i], a[j] = a[j], a[i]
  141. ++i; --j;
  142. __qsort(a, L, j, key)
  143. __qsort(a, i, R, key)
  144. def __f(self, reverse=False, key=None):
  145. if key is None:
  146. key = lambda x:x
  147. __qsort(self, 0, len(self)-1, key)
  148. if reverse:
  149. self.reverse()
  150. list.sort = __f
  151. def staticmethod(f):
  152. return f # no effect
  153. type.__repr__ = lambda self: "<class '" + self.__name__ + "'>"
  154. def help(obj):
  155. if hasattr(obj, '__func__'):
  156. obj = obj.__func__
  157. if hasattr(obj, '__doc__'):
  158. print(obj.__doc__)
  159. else:
  160. print("No docstring found")
  161. del __f