| f | from collections import Counter | f | from collections import Counter |
| | | |
| class Spiral: | | class Spiral: |
| n | text_content = None | n | mystr = None |
| char_counter = None | | mydict = None |
| | | |
| n | def __init__(self, input_string): | n | def __init__(self, s): |
| self.char_counter = Counter(input_string) | | self.mydict = Counter(s) |
| self.text_content = ''.join([character * count for character, co | | self.mystr = ''.join([i * j for i, j in self.mydict.items()]) |
| unt in self.char_counter.items()]) | | |
| | | |
| n | def __add__(self, other_instance): | n | def __add__(self, other): |
| combined_counter = self.char_counter + other_instance.char_count | | d = self.mydict + other.mydict |
| er | | |
| return Spiral(''.join((char * freq for char, freq in combined_co | | return Spiral(''.join((i * j for i, j in d.items()))) |
| unter.items()))) | | |
| | | |
| n | def __sub__(self, other_instance): | n | def __sub__(self, other): |
| difference_counter = self.char_counter - other_instance.char_cou | | d = self.mydict - other.mydict |
| nter | | |
| filtered_counter = {char: count for char, count in difference_co | | d = {i: j for i, j in d.items() if j > 0} |
| unter.items() if count > 0} | | |
| return Spiral(''.join((char * count for char, count in filtered_ | | return Spiral(''.join((i * j for i, j in d.items()))) |
| counter.items()))) | | |
| | | |
| n | def __mul__(self, multiplier): | n | def __mul__(self, other): |
| return Spiral(''.join((char * (count * multiplier) for char, cou | | return Spiral(''.join((i * j * other for i, j in self.mydict.ite |
| nt in self.char_counter.items()))) | | ms()))) |
| | | |
| def __iter__(self): | | def __iter__(self): |
| n | return iter(self.text_content) | n | return iter(self.mystr) |
| | | |
| def __str__(self): | | def __str__(self): |
| n | if len(self.text_content) < 3: | n | if len(self.mystr) < 3: |
| return self.text_content | | return self.mystr |
| segments = [] | | l = [] |
| total_length = len(self.text_content) | | n = len(self.mystr) |
| remaining_text = str(self.text_content) | | tmp = str(self.mystr) |
| max_rows, max_cols = (0, 0) | | row, col = (0, 0) |
| for segment_size in range(2, total_length): | | for i in range(2, n): |
| if remaining_text[:segment_size]: | | if tmp[:i]: |
| if segment_size % 2 != 0: | | if i % 2 != 0: |
| if len(remaining_text[:segment_size]) + 2 > max_rows | | if len(tmp[:i]) + 2 > row: |
| : | | |
| max_rows = len(remaining_text[:segment_size]) + | | row = len(tmp[:i]) + 2 |
| 2 | | |
| segments.append(remaining_text[:segment_size]) | | l.append(tmp[:i]) |
| remaining_text = remaining_text[segment_size - 1:] | | tmp = tmp[i - 1:] |
| else: | | else: |
| n | if len(remaining_text[:segment_size]) > max_cols: | n | if len(tmp[:i]) > col: |
| max_cols = len(remaining_text[:segment_size]) | | col = len(tmp[:i]) |
| segments.append(remaining_text[:segment_size]) | | l.append(tmp[:i]) |
| remaining_text = remaining_text[segment_size - 1:] | | tmp = tmp[i - 1:] |
| else: | | else: |
| break | | break |
| t | spiral_grid = [[' ' for _ in range(max_cols + 1)] for _ in range | t | grid = [[' ' for _ in range(col + 1)] for _ in range(row + 1)] |
| (max_rows + 1)] | | |
| directions = [(0, 1), (-1, 0), (0, -1), (1, 0)] | | d = [(0, 1), (-1, 0), (0, -1), (1, 0)] |
| direction_index = 0 | | step = 0 |
| current_x, current_y = (max_rows // 2, max_cols // 2) | | x, y = (row // 2, col // 2) |
| for segment in segments: | | for i in l: |
| for position, character in enumerate(segment): | | for j, el in enumerate(i): |
| spiral_grid[current_x + position * directions[direction_ | | grid[x + j * d[step][0]][y + j * d[step][1]] = el |
| index][0]][current_y + position * directions[direction_index][1]] = char | | |
| acter | | |
| current_x = current_x + (len(segment) - 1) * directions[dire | | x = x + (len(i) - 1) * d[step][0] |
| ction_index][0] | | |
| current_y = current_y + (len(segment) - 1) * directions[dire | | y = y + (len(i) - 1) * d[step][1] |
| ction_index][1] | | |
| direction_index = (direction_index + 1) % 4 | | step = (step + 1) % 4 |
| non_empty_rows = [row for row in spiral_grid if any((cell != ' ' | | filtered_rows = [row for row in grid if any((cell != ' ' for cel |
| for cell in row))] | | l in row))] |
| transposed_grid = list(zip(*non_empty_rows)) | | transposed = list(zip(*filtered_rows)) |
| non_empty_cols = [col for col in transposed_grid if any((cell != | | filtered_cols = [col for col in transposed if any((cell != ' ' f |
| ' ' for cell in col))] | | or cell in col))] |
| final_grid = [list(row) for row in zip(*non_empty_cols)] | | res = [list(row) for row in zip(*filtered_cols)] |
| return '\n'.join((''.join(row) for row in final_grid)) | | return '\n'.join((''.join(row) for row in res)) |