| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- import sys as _sys
- def print(*args, sep=' ', end='\n'):
- s = sep.join([str(i) for i in args])
- _sys.stdout.write(s + end)
- def abs(x):
- return -x if x < 0 else x
- def max(*args):
- if len(args) == 0:
- raise TypeError('max expected 1 arguments, got 0')
- if len(args) == 1:
- args = args[0]
- args = iter(args)
- res = next(args)
- if res is StopIteration:
- raise ValueError('max() arg is an empty sequence')
- while True:
- i = next(args)
- if i is StopIteration:
- break
- if i > res:
- res = i
- return res
- def min(*args):
- if len(args) == 0:
- raise TypeError('min expected 1 arguments, got 0')
- if len(args) == 1:
- args = args[0]
- args = iter(args)
- res = next(args)
- if res is StopIteration:
- raise ValueError('min() arg is an empty sequence')
- while True:
- i = next(args)
- if i is StopIteration:
- break
- if i < res:
- res = i
- return res
- def all(iterable):
- for i in iterable:
- if not i:
- return False
- return True
- def any(iterable):
- for i in iterable:
- if i:
- return True
- return False
- def enumerate(iterable, start=0):
- n = start
- for elem in iterable:
- yield n, elem
- ++n
- def sum(iterable):
- res = 0
- for i in iterable:
- res += i
- return res
- def map(f, iterable):
- for i in iterable:
- yield f(i)
- def filter(f, iterable):
- for i in iterable:
- if f(i):
- yield i
- def zip(a, b):
- a = iter(a)
- b = iter(b)
- while True:
- ai = next(a)
- bi = next(b)
- if ai is StopIteration or bi is StopIteration:
- break
- yield ai, bi
- def reversed(iterable):
- a = list(iterable)
- a.reverse()
- return a
- def sorted(iterable, reverse=False, key=None):
- a = list(iterable)
- a.sort(reverse=reverse, key=key)
- return a
- ##### str #####
- def __f(self, sep=None):
- flag = sep is None
- sep = sep or ' '
- if sep == "":
- return list(self)
- res = []
- i = 0
- while i < len(self):
- if self[i:i+len(sep)] == sep:
- res.append(self[:i])
- self = self[i+len(sep):]
- i = 0
- else:
- ++i
- res.append(self)
- if flag:
- return [i for i in res if i != '']
- return res
- str.split = __f
- def __f(self, s: str):
- if type(s) is not str:
- raise TypeError('must be str, not ' + type(s).__name__)
- if s == '':
- return len(self) + 1
- res = 0
- i = 0
- while i < len(self):
- if self[i:i+len(s)] == s:
- ++res
- i += len(s)
- else:
- ++i
- return res
- str.count = __f
- def __f(self, *args):
- if '{}' in self:
- for i in range(len(args)):
- self = self.replace('{}', str(args[i]), 1)
- else:
- for i in range(len(args)):
- self = self.replace('{'+str(i)+'}', str(args[i]))
- return self
- str.format = __f
- def __f(self, chars=None):
- chars = chars or ' \t\n\r'
- i = 0
- while i < len(self) and self[i] in chars:
- ++i
- return self[i:]
- str.lstrip = __f
- def __f(self, chars=None):
- chars = chars or ' \t\n\r'
- j = len(self) - 1
- while j >= 0 and self[j] in chars:
- --j
- return self[:j+1]
- str.rstrip = __f
- def __f(self, chars=None):
- chars = chars or ' \t\n\r'
- i = 0
- while i < len(self) and self[i] in chars:
- ++i
- j = len(self) - 1
- while j >= 0 and self[j] in chars:
- --j
- return self[i:j+1]
- str.strip = __f
- def __f(self, width: int):
- delta = width - len(self)
- if delta <= 0:
- return self
- return '0' * delta + self
- str.zfill = __f
- def __f(self, width: int, fillchar=' '):
- delta = width - len(self)
- if delta <= 0:
- return self
- assert len(fillchar) == 1
- return fillchar * delta + self
- str.rjust = __f
- def __f(self, width: int, fillchar=' '):
- delta = width - len(self)
- if delta <= 0:
- return self
- assert len(fillchar) == 1
- return self + fillchar * delta
- str.ljust = __f
- ##### list #####
- list.__repr__ = lambda self: '[' + ', '.join([repr(i) for i in self]) + ']'
- list.__json__ = lambda self: '[' + ', '.join([i.__json__() for i in self]) + ']'
- tuple.__json__ = lambda self: '[' + ', '.join([i.__json__() for i in self]) + ']'
- def __f(self):
- if len(self) == 1:
- return '(' + repr(self[0]) + ',)'
- return '(' + ', '.join([repr(i) for i in self]) + ')'
- tuple.__repr__ = __f
- def __qsort(a: list, L: int, R: int, key):
- if L >= R: return;
- mid = a[(R+L)//2];
- mid = key(mid)
- i, j = L, R
- while i<=j:
- while key(a[i])<mid: ++i;
- while key(a[j])>mid: --j;
- if i<=j:
- a[i], a[j] = a[j], a[i]
- ++i; --j;
- __qsort(a, L, j, key)
- __qsort(a, i, R, key)
- def __f(self, reverse=False, key=None):
- if key is None:
- key = lambda x:x
- __qsort(self, 0, len(self)-1, key)
- if reverse:
- self.reverse()
- list.sort = __f
- def __f(self, other):
- for i, j in zip(self, other):
- if i != j:
- return i < j
- return len(self) < len(other)
- tuple.__lt__ = __f
- list.__lt__ = __f
- def __f(self, other):
- for i, j in zip(self, other):
- if i != j:
- return i > j
- return len(self) > len(other)
- tuple.__gt__ = __f
- list.__gt__ = __f
- def __f(self, other):
- for i, j in zip(self, other):
- if i != j:
- return i <= j
- return len(self) <= len(other)
- tuple.__le__ = __f
- list.__le__ = __f
- def __f(self, other):
- for i, j in zip(self, other):
- if i != j:
- return i >= j
- return len(self) >= len(other)
- tuple.__ge__ = __f
- list.__ge__ = __f
- type.__repr__ = lambda self: "<class '" + self.__name__ + "'>"
- def help(obj):
- if hasattr(obj, '__func__'):
- obj = obj.__func__
- print(obj.__signature__)
- print(obj.__doc__)
- del __f
- from _long import long
|