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