Хайруллина Алина SpiralString 6166
action22k SpiralString 6197
t1from collections import Countert1from collections import Counter
22
3class Spiral:3class Spiral:
4    mystr = None4    mystr = None
5    mydict = None5    mydict = None
66
7    def __init__(self, s):7    def __init__(self, s):
8        self.mydict = Counter(s)8        self.mydict = Counter(s)
9        self.mystr = ''.join([i * j for i, j in self.mydict.items()])9        self.mystr = ''.join([i * j for i, j in self.mydict.items()])
1010
11    def __add__(self, other):11    def __add__(self, other):
12        d = self.mydict + other.mydict12        d = self.mydict + other.mydict
13        return Spiral(''.join((i * j for i, j in d.items())))13        return Spiral(''.join((i * j for i, j in d.items())))
1414
15    def __sub__(self, other):15    def __sub__(self, other):
16        d = self.mydict - other.mydict16        d = self.mydict - other.mydict
17        d = {i: j for i, j in d.items() if j > 0}17        d = {i: j for i, j in d.items() if j > 0}
18        return Spiral(''.join((i * j for i, j in d.items())))18        return Spiral(''.join((i * j for i, j in d.items())))
1919
20    def __mul__(self, other):20    def __mul__(self, other):
21        return Spiral(''.join((i * j * other for i, j in self.mydict.ite21        return Spiral(''.join((i * j * other for i, j in self.mydict.ite
>ms())))>ms())))
2222
23    def __iter__(self):23    def __iter__(self):
24        return iter(self.mystr)24        return iter(self.mystr)
2525
26    def __str__(self):26    def __str__(self):
27        if len(self.mystr) < 3:27        if len(self.mystr) < 3:
28            return self.mystr28            return self.mystr
29        l = []29        l = []
30        n = len(self.mystr)30        n = len(self.mystr)
31        tmp = str(self.mystr)31        tmp = str(self.mystr)
32        row, col = (0, 0)32        row, col = (0, 0)
33        for i in range(2, n):33        for i in range(2, n):
34            if tmp[:i]:34            if tmp[:i]:
35                if i % 2 != 0:35                if i % 2 != 0:
36                    if len(tmp[:i]) + 2 > row:36                    if len(tmp[:i]) + 2 > row:
37                        row = len(tmp[:i]) + 237                        row = len(tmp[:i]) + 2
38                    l.append(tmp[:i])38                    l.append(tmp[:i])
39                    tmp = tmp[i - 1:]39                    tmp = tmp[i - 1:]
40                else:40                else:
41                    if len(tmp[:i]) > col:41                    if len(tmp[:i]) > col:
42                        col = len(tmp[:i])42                        col = len(tmp[:i])
43                    l.append(tmp[:i])43                    l.append(tmp[:i])
44                    tmp = tmp[i - 1:]44                    tmp = tmp[i - 1:]
45            else:45            else:
46                break46                break
47        grid = [[' ' for _ in range(col + 1)] for _ in range(row + 1)]47        grid = [[' ' for _ in range(col + 1)] for _ in range(row + 1)]
48        d = [(0, 1), (-1, 0), (0, -1), (1, 0)]48        d = [(0, 1), (-1, 0), (0, -1), (1, 0)]
49        step = 049        step = 0
50        x, y = (row // 2, col // 2)50        x, y = (row // 2, col // 2)
51        for i in l:51        for i in l:
52            for j, el in enumerate(i):52            for j, el in enumerate(i):
53                grid[x + j * d[step][0]][y + j * d[step][1]] = el53                grid[x + j * d[step][0]][y + j * d[step][1]] = el
54            x = x + (len(i) - 1) * d[step][0]54            x = x + (len(i) - 1) * d[step][0]
55            y = y + (len(i) - 1) * d[step][1]55            y = y + (len(i) - 1) * d[step][1]
56            step = (step + 1) % 456            step = (step + 1) % 4
57        filtered_rows = [row for row in grid if any((cell != ' ' for cel57        filtered_rows = [row for row in grid if any((cell != ' ' for cel
>l in row))]>l in row))]
58        transposed = list(zip(*filtered_rows))58        transposed = list(zip(*filtered_rows))
59        filtered_cols = [col for col in transposed if any((cell != ' ' f59        filtered_cols = [col for col in transposed if any((cell != ' ' f
>or cell in col))]>or cell in col))]
60        res = [list(row) for row in zip(*filtered_cols)]60        res = [list(row) for row in zip(*filtered_cols)]
61        return '\n'.join((''.join(row) for row in res))61        return '\n'.join((''.join(row) for row in res))
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op