| t | from collections import Counter | t | from collections import Counter |
| | | |
| class Spiral: | | class Spiral: |
| mystr = None | | mystr = None |
| mydict = None | | mydict = None |
| | | |
| def __init__(self, s): | | def __init__(self, s): |
| self.mydict = Counter(s) | | self.mydict = Counter(s) |
| self.mystr = ''.join([i * j for i, j in self.mydict.items()]) | | self.mystr = ''.join([i * j for i, j in self.mydict.items()]) |
| | | |
| def __add__(self, other): | | def __add__(self, other): |
| d = self.mydict + other.mydict | | d = self.mydict + other.mydict |
| return Spiral(''.join((i * j for i, j in d.items()))) | | return Spiral(''.join((i * j for i, j in d.items()))) |
| | | |
| def __sub__(self, other): | | def __sub__(self, other): |
| d = self.mydict - other.mydict | | d = self.mydict - other.mydict |
| d = {i: j for i, j in d.items() if j > 0} | | d = {i: j for i, j in d.items() if j > 0} |
| return Spiral(''.join((i * j for i, j in d.items()))) | | return Spiral(''.join((i * j for i, j in d.items()))) |
| | | |
| def __mul__(self, other): | | def __mul__(self, other): |
| return Spiral(''.join((i * j * other for i, j in self.mydict.ite | | return Spiral(''.join((i * j * other for i, j in self.mydict.ite |
| ms()))) | | ms()))) |
| | | |
| def __iter__(self): | | def __iter__(self): |
| return iter(self.mystr) | | return iter(self.mystr) |
| | | |
| def __str__(self): | | def __str__(self): |
| if len(self.mystr) < 3: | | if len(self.mystr) < 3: |
| return self.mystr | | return self.mystr |
| l = [] | | l = [] |
| n = len(self.mystr) | | n = len(self.mystr) |
| tmp = str(self.mystr) | | tmp = str(self.mystr) |
| row, col = (0, 0) | | row, col = (0, 0) |
| for i in range(2, n): | | for i in range(2, n): |
| if tmp[:i]: | | if tmp[:i]: |
| if i % 2 != 0: | | if i % 2 != 0: |
| if len(tmp[:i]) + 2 > row: | | if len(tmp[:i]) + 2 > row: |
| row = len(tmp[:i]) + 2 | | row = len(tmp[:i]) + 2 |
| l.append(tmp[:i]) | | l.append(tmp[:i]) |
| tmp = tmp[i - 1:] | | tmp = tmp[i - 1:] |
| else: | | else: |
| if len(tmp[:i]) > col: | | if len(tmp[:i]) > col: |
| col = len(tmp[:i]) | | col = len(tmp[:i]) |
| l.append(tmp[:i]) | | l.append(tmp[:i]) |
| tmp = tmp[i - 1:] | | tmp = tmp[i - 1:] |
| else: | | else: |
| break | | break |
| grid = [[' ' for _ in range(col + 1)] for _ in range(row + 1)] | | grid = [[' ' for _ in range(col + 1)] for _ in range(row + 1)] |
| d = [(0, 1), (-1, 0), (0, -1), (1, 0)] | | d = [(0, 1), (-1, 0), (0, -1), (1, 0)] |
| step = 0 | | step = 0 |
| x, y = (row // 2, col // 2) | | x, y = (row // 2, col // 2) |
| for i in l: | | for i in l: |
| for j, el in enumerate(i): | | for j, el in enumerate(i): |
| grid[x + j * d[step][0]][y + j * d[step][1]] = el | | grid[x + j * d[step][0]][y + j * d[step][1]] = el |
| x = x + (len(i) - 1) * d[step][0] | | x = x + (len(i) - 1) * d[step][0] |
| y = y + (len(i) - 1) * d[step][1] | | y = y + (len(i) - 1) * d[step][1] |
| step = (step + 1) % 4 | | step = (step + 1) % 4 |
| filtered_rows = [row for row in grid if any((cell != ' ' for cel | | filtered_rows = [row for row in grid if any((cell != ' ' for cel |
| l in row))] | | l in row))] |
| transposed = list(zip(*filtered_rows)) | | transposed = list(zip(*filtered_rows)) |
| filtered_cols = [col for col in transposed if any((cell != ' ' f | | filtered_cols = [col for col in transposed if any((cell != ' ' f |
| or cell in col))] | | or cell in col))] |
| res = [list(row) for row in zip(*filtered_cols)] | | res = [list(row) for row in zip(*filtered_cols)] |
| return '\n'.join((''.join(row) for row in res)) | | return '\n'.join((''.join(row) for row in res)) |