f | from collections import Counter | f | from collections import Counter |
| | | |
| class Spiral: | | class Spiral: |
| | | |
n | def __init__(self, s): | n | def __init__(self, st): |
| if isinstance(s, str): | | if isinstance(st, str): |
| self.c = Counter(s) | | self.c = Counter(st) |
| else: | | else: |
n | self.c = s | n | self.c = st |
| | | |
| def __iter__(self): | | def __iter__(self): |
| for i in self.c.elements(): | | for i in self.c.elements(): |
| yield i | | yield i |
| | | |
| def __repr__(self): | | def __repr__(self): |
n | l = sum(self.c.values()) | n | length = sum(self.c.values()) |
| if l < 3: | | if length < 3: |
| return ''.join(list(self.c.elements())) | | return ''.join(list(self.c.elements())) |
| a_min = b_min = b_max = 0 | | a_min = b_min = b_max = 0 |
| a_max = 1 | | a_max = 1 |
n | dir_ = 0 | n | direct = 0 |
| x = 1 | | x = 1 |
| y = 0 | | y = 0 |
n | cnt = l - 1 | n | cnt = length - 1 |
| i = 2 | | i = 2 |
| while cnt > i: | | while cnt > i: |
n | if dir_ == 0: | n | if direct == 0: |
| y -= i | | y -= i |
| b_min = min(y, b_min) | | b_min = min(y, b_min) |
n | elif dir_ == 1: | n | elif direct == 1: |
| x -= i | | x -= i |
| a_min = min(x, a_min) | | a_min = min(x, a_min) |
n | elif dir_ == 2: | n | elif direct == 2: |
| y += i | | y += i |
| b_max = max(y, b_max) | | b_max = max(y, b_max) |
n | elif dir_ == 3: | n | elif direct == 3: |
| x += i | | x += i |
| a_max = max(x, a_max) | | a_max = max(x, a_max) |
| cnt -= i | | cnt -= i |
| i += 1 | | i += 1 |
n | dir_ = (dir_ + 1) % 4 | n | direct = (direct + 1) % 4 |
| cnt -= 1 | | cnt -= 1 |
n | if dir_ == 0: | n | if direct == 0: |
| y -= cnt | | y -= cnt |
| b_min = min(y, b_min) | | b_min = min(y, b_min) |
n | elif dir_ == 1: | n | elif direct == 1: |
| x -= cnt | | x -= cnt |
| a_min = min(x, a_min) | | a_min = min(x, a_min) |
n | elif dir_ == 2: | n | elif direct == 2: |
| y += cnt | | y += cnt |
| b_max = max(y, b_max) | | b_max = max(y, b_max) |
n | elif dir_ == 3: | n | elif direct == 3: |
| x += cnt | | x += cnt |
| a_max = max(x, a_max) | | a_max = max(x, a_max) |
| a = a_max - a_min + 1 | | a = a_max - a_min + 1 |
| b = b_max - b_min + 1 | | b = b_max - b_min + 1 |
| x = -a_min | | x = -a_min |
| y = -b_min | | y = -b_min |
| matr = [[' ' for i in range(a)] for j in range(b)] | | matr = [[' ' for i in range(a)] for j in range(b)] |
n | dir_ = 0 | n | direct = 0 |
| i = j = 1 | | i = j = 1 |
| for k in self.c.elements(): | | for k in self.c.elements(): |
| matr[y][x] = k | | matr[y][x] = k |
n | if dir_ == 0: | n | if direct == 0: |
| x += 1 | | x += 1 |
n | elif dir_ == 1: | n | elif direct == 1: |
| y -= 1 | | y -= 1 |
n | elif dir_ == 2: | n | elif direct == 2: |
| x -= 1 | | x -= 1 |
n | elif dir_ == 3: | n | elif direct == 3: |
| y += 1 | | y += 1 |
| if x < 0 or y < 0: | | if x < 0 or y < 0: |
| print('ERRRRRRROR') | | print('ERRRRRRROR') |
| j -= 1 | | j -= 1 |
| if j == 0: | | if j == 0: |
n | dir_ = (dir_ + 1) % 4 | n | direct = (direct + 1) % 4 |
| i += 1 | | i += 1 |
| j = i | | j = i |
| res = '' | | res = '' |
| for i in range(b): | | for i in range(b): |
| res += ''.join(matr[i]) + '\n' | | res += ''.join(matr[i]) + '\n' |
| return res | | return res |
| | | |
n | def __add__(self, sp): | n | def __add__(self, other): |
| return Spiral(self.c + sp.c) | | return Spiral(self.c + other.c) |
| | | |
n | def __sub__(self, sp): | n | def __sub__(self, other): |
| return Spiral(self.c - sp.c) | | return Spiral(self.c - other.c) |
| | | |
| def __mul__(self, num): | | def __mul__(self, num): |
t | a = Counter(self.c) | t | tmp = Counter(self.c) |
| for i in a: | | for i in tmp: |
| a[i] *= num | | tmp[i] *= num |
| return Spiral(a) | | return Spiral(tmp) |
| '\nS = Spiral("abbcccddddeeeee")\nI = Spiral("abcdefghi")\n\nprint(f"{S}\n")\nprint(S+I, "\n")\nprint(S-I, "\n")\nprint(I*2, "\n")\nprint(I*2-S, "\n")\nprint(*list(S+I))\n' | | |