Дю Василий Андреевич КФ МГУ SubString 7300
Подлужный Олег 304 КФ SubString 6628
t1class SubString(str):t1class SubString(str):
22
3    def __new__(cls, value=''):3    def __new__(cls, value=''):
4        return super().__new__(cls, str(value))4        return super().__new__(cls, str(value))
55
6    def __sub__(self, other):6    def __sub__(self, other):
7        if not isinstance(other, str):7        if not isinstance(other, str):
8            return NotImplemented8            return NotImplemented
9        counts = {}9        counts = {}
10        for ch in other:10        for ch in other:
11            counts[ch] = counts.get(ch, 0) + 111            counts[ch] = counts.get(ch, 0) + 1
12        res = []12        res = []
13        for ch in self:13        for ch in self:
14            if counts.get(ch, 0) > 0:14            if counts.get(ch, 0) > 0:
15                counts[ch] -= 115                counts[ch] -= 1
16            else:16            else:
17                res.append(ch)17                res.append(ch)
18        return SubString(''.join(res))18        return SubString(''.join(res))
1919
20    def __rsub__(self, other):20    def __rsub__(self, other):
21        if not isinstance(other, str):21        if not isinstance(other, str):
22            return NotImplemented22            return NotImplemented
23        return SubString(other).__sub__(self)23        return SubString(other).__sub__(self)
2424
25    def __add__(self, other):25    def __add__(self, other):
26        if not isinstance(other, str):26        if not isinstance(other, str):
27            return NotImplemented27            return NotImplemented
28        return SubString(str.__add__(self, other))28        return SubString(str.__add__(self, other))
2929
30    def __radd__(self, other):30    def __radd__(self, other):
31        if not isinstance(other, str):31        if not isinstance(other, str):
32            return NotImplemented32            return NotImplemented
33        return SubString(str.__add__(other, self))33        return SubString(str.__add__(other, self))
3434
35    def __mul__(self, n):35    def __mul__(self, n):
36        return SubString(str.__mul__(self, n))36        return SubString(str.__mul__(self, n))
3737
38    def __rmul__(self, n):38    def __rmul__(self, n):
39        return SubString(str.__mul__(self, n))39        return SubString(str.__mul__(self, n))
4040
41    def __getitem__(self, key):41    def __getitem__(self, key):
42        res = str.__getitem__(self, key)42        res = str.__getitem__(self, key)
43        return SubString(res) if isinstance(res, str) else res43        return SubString(res) if isinstance(res, str) else res
4444
45    def __getattribute__(self, name):45    def __getattribute__(self, name):
46        attr = super().__getattribute__(name)46        attr = super().__getattribute__(name)
47        if callable(attr):47        if callable(attr):
4848
49            def wrapper(*args, **kwargs):49            def wrapper(*args, **kwargs):
50                res = attr(*args, **kwargs)50                res = attr(*args, **kwargs)
51                if isinstance(res, str):51                if isinstance(res, str):
52                    return SubString(res)52                    return SubString(res)
53                if isinstance(res, tuple):53                if isinstance(res, tuple):
54                    return tuple((SubString(x) if isinstance(x, str) els54                    return tuple((SubString(x) if isinstance(x, str) els
>e x for x in res))>e x for x in res))
55                return res55                return res
56            return wrapper56            return wrapper
57        return attr57        return attr
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op