t | from collections import * | t | from collections import * |
| | | |
| | | |
| class Spiral: | | class Spiral: |
| | | |
| def __init__(self, s): | | def __init__(self, s): |
| self.c = Counter(s) | | self.c = Counter(s) |
| | | |
| def __add__(self, other): | | def __add__(self, other): |
| return type(self)(list(self) + list(other)) | | return type(self)(list(self) + list(other)) |
| | | |
| def __sub__(self, other): | | def __sub__(self, other): |
| return type(self)(self.c - other.c) | | return type(self)(self.c - other.c) |
| | | |
| def __mul__(self, n): | | def __mul__(self, n): |
| return type(self)(list(self)*n) | | return type(self)(list(self)*n) |
| | | |
| def __iter__(self): | | def __iter__(self): |
| return self.c.elements() | | return self.c.elements() |
| __rmul__ = __mul__ | | __rmul__ = __mul__ |
| | | |
| def __len__(self): | | def __len__(self): |
| return sum(self.c.values()) | | return sum(self.c.values()) |
| | | |
| def _square(self): | | def _square(self): |
| dx, dy = (0, 1, 0, -1), (1, 0, -1, 0) | | dx, dy = (0, 1, 0, -1), (1, 0, -1, 0) |
| F = {} | | F = {} |
| x = y = n = k = j = mx = Mx = my = My = 0 | | x = y = n = k = j = mx = Mx = my = My = 0 |
| for i, c in enumerate(self): | | for i, c in enumerate(self): |
| F[x, y] = c | | F[x, y] = c |
| mx, my, Mx, My = min(mx, x), min(my, y), max(Mx, x), max(My, y) | | mx, my, Mx, My = min(mx, x), min(my, y), max(Mx, x), max(My, y) |
| if i >= n: | | if i >= n: |
| k += 1 | | k += 1 |
| n += k | | n += k |
| j = (j+1) % 4 | | j = (j+1) % 4 |
| x, y = x+dx[j], y+dy[j] | | x, y = x+dx[j], y+dy[j] |
| return F, (mx, Mx), (my, My) | | return F, (mx, Mx), (my, My) |
| | | |
| def __str__(self): | | def __str__(self): |
| F, (mx, Mx), (my, My) = self._square() | | F, (mx, Mx), (my, My) = self._square() |
| return "\n".join( | | return "\n".join( |
| "".join( | | "".join( |
| F.get( | | F.get( |
| (x, | | (x, |
| y), | | y), |
| " ") for x in range( | | " ") for x in range( |
| mx, | | mx, |
| Mx + | | Mx + |
| 1)) for y in range( | | 1)) for y in range( |
| my, | | my, |
| My + | | My + |
| 1)) | | 1)) |
| | | |