| f | from collections import OrderedDict | f | from collections import OrderedDict |
| n | import random | n | |
| import string | | |
| | | |
| class Spiral: | | class Spiral: |
| | | |
| n | def __init__(self, s): | n | def __init__(self, input_data): |
| self.seq = OrderedDict() | | self.sequences = OrderedDict() |
| if isinstance(s, (str, list, tuple)): | | if type(input_data) in (str, list, tuple): |
| for char in s: | | for element in input_data: |
| if char not in self.seq: | | if element in self.sequences: |
| self.seq[char] = 1 | | self.sequences[element] += 1 |
| else: | | else: |
| n | self.seq[char] += 1 | n | self.sequences[element] = 1 |
| elif isinstance(s, OrderedDict): | | elif isinstance(input_data, OrderedDict): |
| self.seq = s.copy() | | self.sequences = input_data.copy() |
| else: | | else: |
| n | materialized_str = ''.join((str(char) for char in s)) | n | text_representation = ''.join((str(elem) for elem in input_d |
| | | ata)) |
| for char in materialized_str: | | for element in text_representation: |
| if char not in self.seq: | | if element in self.sequences: |
| self.seq[char] = 1 | | self.sequences[element] += 1 |
| else: | | else: |
| n | self.seq[char] += 1 | n | self.sequences[element] = 1 |
| | | |
| n | def _get_char_array(self): | n | def _generate_character_list(self): |
| char_arr = [] | | result_chars = [] |
| for char, count in self.seq.items(): | | for character, quantity in self.sequences.items(): |
| char_arr.extend([char] * count) | | result_chars.extend([character] * quantity) |
| return char_arr | | return result_chars |
| | | |
| n | def _spiral_string(self): | n | def _create_spiral_layout(self): |
| char_arr = self._get_char_array() | | characters = self._generate_character_list() |
| total_chars = len(char_arr) | | total_length = len(characters) |
| if total_chars == 0: | | if total_length == 0: |
| return '' | | return '' |
| n | size = 1 | n | dimension = 1 |
| while size * size < total_chars * 2: | | while dimension * dimension < total_length * 2: |
| size += 1 | | dimension += 1 |
| size = max(size + 10, 50) | | dimension = max(dimension + 8, 45) |
| matrix = [[' ' for _ in range(size)] for _ in range(size)] | | grid = [[' ' for _ in range(dimension)] for _ in range(dimension |
| | | )] |
| x, y = (size // 2, size // 2) | | center_x, center_y = (dimension // 2, dimension // 2) |
| char_idx = 0 | | current_index = 0 |
| if char_idx < total_chars: | | if current_index < total_length: |
| matrix[x][y] = char_arr[char_idx] | | grid[center_x][center_y] = characters[current_index] |
| char_idx += 1 | | current_index += 1 |
| directions = [(0, 1), (-1, 0), (0, -1), (1, 0)] | | movement_patterns = [(0, 1), (-1, 0), (0, -1), (1, 0)] |
| dir_idx = 0 | | current_direction = 0 |
| step_length = 1 | | current_step_size = 1 |
| while char_idx < total_chars: | | while current_index < total_length: |
| dx, dy = directions[dir_idx] | | move_x, move_y = movement_patterns[current_direction] |
| for step in range(step_length): | | for step in range(current_step_size): |
| if char_idx >= total_chars: | | if current_index >= total_length: |
| break | | break |
| n | x += dx | n | center_x += move_x |
| y += dy | | center_y += move_y |
| if 0 <= x < size and 0 <= y < size: | | if 0 <= center_x < dimension and 0 <= center_y < dimensi |
| | | on: |
| matrix[x][y] = char_arr[char_idx] | | grid[center_x][center_y] = characters[current_index] |
| char_idx += 1 | | current_index += 1 |
| dir_idx = (dir_idx + 1) % 4 | | current_direction = (current_direction + 1) % 4 |
| step_length += 1 | | current_step_size += 1 |
| min_row, max_row = (size, -1) | | min_row, max_row = (dimension, -1) |
| min_col, max_col = (size, -1) | | min_col, max_col = (dimension, -1) |
| for i in range(size): | | for row in range(dimension): |
| for j in range(size): | | for col in range(dimension): |
| if matrix[i][j] != ' ': | | if grid[row][col] != ' ': |
| min_row = min(min_row, i) | | min_row = min(min_row, row) |
| max_row = max(max_row, i) | | max_row = max(max_row, row) |
| min_col = min(min_col, j) | | min_col = min(min_col, col) |
| max_col = max(max_col, j) | | max_col = max(max_col, col) |
| if min_row > max_row or min_col > max_col: | | if min_row > max_row or min_col > max_col: |
| n | for i in range(size): | n | for row in range(dimension): |
| for j in range(size): | | for col in range(dimension): |
| if matrix[i][j] != ' ': | | if grid[row][col] != ' ': |
| return matrix[i][j] | | return grid[row][col] |
| return '' | | return '' |
| n | result_lines = [] | n | output_lines = [] |
| for i in range(min_row, max_row + 1): | | for row in range(min_row, max_row + 1): |
| line = ''.join(matrix[i][min_col:max_col + 1]) | | line_content = ''.join(grid[row][min_col:max_col + 1]) |
| result_lines.append(line.rstrip()) | | output_lines.append(line_content.rstrip()) |
| return '\n'.join(result_lines) | | return '\n'.join(output_lines) |
| | | |
| def __str__(self): | | def __str__(self): |
| n | return self._spiral_string() | n | return self._create_spiral_layout() |
| | | |
| def __add__(self, other): | | def __add__(self, other): |
| n | result_dict = OrderedDict() | n | combined_sequences = OrderedDict() |
| for char, count in self.seq.items(): | | for char, count in self.sequences.items(): |
| result_dict[char] = count | | combined_sequences[char] = count |
| for char, count in other.seq.items(): | | for char, count in other.sequences.items(): |
| if char in result_dict: | | if char in combined_sequences: |
| result_dict[char] += count | | combined_sequences[char] += count |
| else: | | else: |
| n | result_dict[char] = count | n | combined_sequences[char] = count |
| return Spiral(result_dict) | | return Spiral(combined_sequences) |
| | | |
| def __sub__(self, other): | | def __sub__(self, other): |
| n | result_dict = OrderedDict() | n | result_sequences = OrderedDict() |
| for char, count in self.seq.items(): | | for char, count in self.sequences.items(): |
| if char in other.seq: | | if char in other.sequences: |
| new_count = count - other.seq[char] | | remaining = count - other.sequences[char] |
| if new_count > 0: | | if remaining > 0: |
| result_dict[char] = new_count | | result_sequences[char] = remaining |
| else: | | else: |
| n | result_dict[char] = count | n | result_sequences[char] = count |
| return Spiral(result_dict) | | return Spiral(result_sequences) |
| | | |
| n | def __mul__(self, n): | n | def __mul__(self, multiplier): |
| result_dict = OrderedDict() | | multiplied_sequences = OrderedDict() |
| for char, count in self.seq.items(): | | for char, count in self.sequences.items(): |
| result_dict[char] = count * n | | multiplied_sequences[char] = count * multiplier |
| return Spiral(result_dict) | | return Spiral(multiplied_sequences) |
| | | |
| def __iter__(self): | | def __iter__(self): |
| t | for char, count in self.seq.items(): | t | for character, quantity in self.sequences.items(): |
| for _ in range(count): | | for _ in range(quantity): |
| yield char | | yield character |