SpiralString/svetych
SpiralString/Stephan
n1from collections import Counter as cn1from collections import Counter
22
33
4class Spiral:4class Spiral:
nn5 
5    def __init__(self, s=''):6    def __init__(self, string=""):
6        self.cells = c(s)7        self.cells = Counter(string)
78
8    def __iter__(self):9    def __iter__(self):
9        return self.cells.elements()10        return self.cells.elements()
1011
11    def __add__(self, other):12    def __add__(self, other):
12        return type(self)(self.cells + other.cells)13        return type(self)(self.cells + other.cells)
1314
14    def __sub__(self, other):15    def __sub__(self, other):
15        return type(self)(self.cells - other.cells)16        return type(self)(self.cells - other.cells)
1617
17    def __mul__(self, num):18    def __mul__(self, num):
18        return type(self)(list(self)*num)19        return type(self)(list(self)*num)
1920
20    __rmul__ = __mul__21    __rmul__ = __mul__
2122
22    def __len__(self):23    def __len__(self):
23        return sum(self.cells.values())24        return sum(self.cells.values())
2425
25    def _square(self):26    def _square(self):
26        dx, dy = (0, 1, 0, -1), (1, 0, -1, 0)27        dx, dy = (0, 1, 0, -1), (1, 0, -1, 0)
27        F = {}28        F = {}
28        x = y = n = k = j = mx = Mx = my = My = 029        x = y = n = k = j = mx = Mx = my = My = 0
29        for i, c in enumerate(self):30        for i, c in enumerate(self):
30            F[x, y] = c31            F[x, y] = c
31            mx, my, Mx, My = min(mx, x), min(my, y), max(Mx, x), max(My, y)32            mx, my, Mx, My = min(mx, x), min(my, y), max(Mx, x), max(My, y)
32            if i >= n:33            if i >= n:
33                k += 134                k += 1
34                n += k35                n += k
35                j = (j+1) % 436                j = (j+1) % 4
36            x, y = x+dx[j], y+dy[j]37            x, y = x+dx[j], y+dy[j]
37        return F, (mx, Mx), (my, My)38        return F, (mx, Mx), (my, My)
3839
39    def __str__(self):40    def __str__(self):
40        F, (mx, Mx), (my, My) = self._square()41        F, (mx, Mx), (my, My) = self._square()
n41        return '\n'.join(n42        return "\n".join(
42            ''.join(43            "".join(
43                F.get(44                F.get(
44                    (x,45                    (x,
45                     y),46                     y),
t46                    ' ') for x in range(t47                    " ") for x in range(
47                    mx,48                    mx,
48                    Mx +49                    Mx +
49                    1)) for y in range(50                    1)) for y in range(
50                my,51                my,
51                My +52                My +
52                1))53                1))
5354
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op