SpiralString/Stephan
SpiralString/svetych
n1from collections import Countern1from collections import Counter as c
22
33
4class Spiral:4class Spiral:
n5 n
6    def __init__(self, string=""):5    def __init__(self, s=''):
7        self.cells = Counter(string)6        self.cells = c(s)
87
9    def __iter__(self):8    def __iter__(self):
10        return self.cells.elements()9        return self.cells.elements()
1110
12    def __add__(self, other):11    def __add__(self, other):
13        return type(self)(self.cells + other.cells)12        return type(self)(self.cells + other.cells)
1413
15    def __sub__(self, other):14    def __sub__(self, other):
16        return type(self)(self.cells - other.cells)15        return type(self)(self.cells - other.cells)
1716
18    def __mul__(self, num):17    def __mul__(self, num):
19        return type(self)(list(self)*num)18        return type(self)(list(self)*num)
2019
21    __rmul__ = __mul__20    __rmul__ = __mul__
2221
23    def __len__(self):22    def __len__(self):
24        return sum(self.cells.values())23        return sum(self.cells.values())
2524
26    def _square(self):25    def _square(self):
27        dx, dy = (0, 1, 0, -1), (1, 0, -1, 0)26        dx, dy = (0, 1, 0, -1), (1, 0, -1, 0)
28        F = {}27        F = {}
29        x = y = n = k = j = mx = Mx = my = My = 028        x = y = n = k = j = mx = Mx = my = My = 0
30        for i, c in enumerate(self):29        for i, c in enumerate(self):
31            F[x, y] = c30            F[x, y] = c
32            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)
33            if i >= n:32            if i >= n:
34                k += 133                k += 1
35                n += k34                n += k
36                j = (j+1) % 435                j = (j+1) % 4
37            x, y = x+dx[j], y+dy[j]36            x, y = x+dx[j], y+dy[j]
38        return F, (mx, Mx), (my, My)37        return F, (mx, Mx), (my, My)
3938
40    def __str__(self):39    def __str__(self):
41        F, (mx, Mx), (my, My) = self._square()40        F, (mx, Mx), (my, My) = self._square()
n42        return "\n".join(n41        return '\n'.join(
43            "".join(42            ''.join(
44                F.get(43                F.get(
45                    (x,44                    (x,
46                     y),45                     y),
t47                    " ") for x in range(t46                    ' ') for x in range(
48                    mx,47                    mx,
49                    Mx +48                    Mx +
50                    1)) for y in range(49                    1)) for y in range(
51                my,50                my,
52                My +51                My +
53                1))52                1))
5453
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op