f | from collections import Counter | f | from collections import Counter |
| | | |
| class Spiral: | | class Spiral: |
| | | |
n | def __init__(self, st): | n | def __init__(self, s): |
| if isinstance(st, str): | | if isinstance(s, str): |
| self.c = Counter(st) | | self.c = Counter(s) |
| else: | | else: |
n | self.c = st | n | self.c = s |
| | | |
| 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 | length = sum(self.c.values()) | n | l = sum(self.c.values()) |
| if length < 3: | | if l < 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 | direct = 0 | n | dir_ = 0 |
| x = 1 | | x = 1 |
| y = 0 | | y = 0 |
n | cnt = length - 1 | n | cnt = l - 1 |
| i = 2 | | i = 2 |
| while cnt > i: | | while cnt > i: |
n | if direct == 0: | n | if dir_ == 0: |
| y -= i | | y -= i |
| b_min = min(y, b_min) | | b_min = min(y, b_min) |
n | elif direct == 1: | n | elif dir_ == 1: |
| x -= i | | x -= i |
| a_min = min(x, a_min) | | a_min = min(x, a_min) |
n | elif direct == 2: | n | elif dir_ == 2: |
| y += i | | y += i |
| b_max = max(y, b_max) | | b_max = max(y, b_max) |
n | elif direct == 3: | n | elif dir_ == 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 | direct = (direct + 1) % 4 | n | dir_ = (dir_ + 1) % 4 |
| cnt -= 1 | | cnt -= 1 |
n | if direct == 0: | n | if dir_ == 0: |
| y -= cnt | | y -= cnt |
| b_min = min(y, b_min) | | b_min = min(y, b_min) |
n | elif direct == 1: | n | elif dir_ == 1: |
| x -= cnt | | x -= cnt |
| a_min = min(x, a_min) | | a_min = min(x, a_min) |
n | elif direct == 2: | n | elif dir_ == 2: |
| y += cnt | | y += cnt |
| b_max = max(y, b_max) | | b_max = max(y, b_max) |
n | elif direct == 3: | n | elif dir_ == 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 | direct = 0 | n | dir_ = 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 direct == 0: | n | if dir_ == 0: |
| x += 1 | | x += 1 |
n | elif direct == 1: | n | elif dir_ == 1: |
| y -= 1 | | y -= 1 |
n | elif direct == 2: | n | elif dir_ == 2: |
| x -= 1 | | x -= 1 |
n | elif direct == 3: | n | elif dir_ == 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 | direct = (direct + 1) % 4 | n | dir_ = (dir_ + 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, other): | n | def __add__(self, sp): |
| return Spiral(self.c + other.c) | | return Spiral(self.c + sp.c) |
| | | |
n | def __sub__(self, other): | n | def __sub__(self, sp): |
| return Spiral(self.c - other.c) | | return Spiral(self.c - sp.c) |
| | | |
| def __mul__(self, num): | | def __mul__(self, num): |
t | tmp = Counter(self.c) | t | a = Counter(self.c) |
| for i in tmp: | | for i in a: |
| tmp[i] *= num | | a[i] *= num |
| return Spiral(tmp) | | return Spiral(a) |
| | | '\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' |