SpiralString/abrivan
SpiralString/yervandsargsyan
t1from collections import *t1from collections import *
22
33
4class Spiral:4class Spiral:
55
6    def __init__(self, s):6    def __init__(self, s):
7        self.c = Counter(s)7        self.c = Counter(s)
88
9    def __add__(self, other):9    def __add__(self, other):
10        return type(self)(list(self) + list(other))10        return type(self)(list(self) + list(other))
1111
12    def __sub__(self, other):12    def __sub__(self, other):
13        return type(self)(self.c - other.c)13        return type(self)(self.c - other.c)
1414
15    def __mul__(self, n):15    def __mul__(self, n):
16        return type(self)(list(self)*n)16        return type(self)(list(self)*n)
1717
18    def __iter__(self):18    def __iter__(self):
19        return self.c.elements()19        return self.c.elements()
20    __rmul__ = __mul__20    __rmul__ = __mul__
2121
22    def __len__(self):22    def __len__(self):
23        return sum(self.c.values())23        return sum(self.c.values())
2424
25    def _square(self):25    def _square(self):
26        dx, dy = (0, 1, 0, -1), (1, 0, -1, 0)26        dx, dy = (0, 1, 0, -1), (1, 0, -1, 0)
27        F = {}27        F = {}
28        x = y = n = k = j = mx = Mx = my = My = 028        x = y = n = k = j = mx = Mx = my = My = 0
29        for i, c in enumerate(self):29        for i, c in enumerate(self):
30            F[x, y] = c30            F[x, y] = c
31            mx, my, Mx, My = min(mx, x), min(my, y), max(Mx, x), max(My, y)31            mx, my, Mx, My = min(mx, x), min(my, y), max(Mx, x), max(My, y)
32            if i >= n:32            if i >= n:
33                k += 133                k += 1
34                n += k34                n += k
35                j = (j+1) % 435                j = (j+1) % 4
36            x, y = x+dx[j], y+dy[j]36            x, y = x+dx[j], y+dy[j]
37        return F, (mx, Mx), (my, My)37        return F, (mx, Mx), (my, My)
3838
39    def __str__(self):39    def __str__(self):
40        F, (mx, Mx), (my, My) = self._square()40        F, (mx, Mx), (my, My) = self._square()
41        return "\n".join(41        return "\n".join(
42            "".join(42            "".join(
43                F.get(43                F.get(
44                    (x,44                    (x,
45                     y),45                     y),
46                    " ") for x in range(46                    " ") for x in range(
47                    mx,47                    mx,
48                    Mx +48                    Mx +
49                    1)) for y in range(49                    1)) for y in range(
50                my,50                my,
51                My +51                My +
52                1))52                1))
5353
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op