Михаил Авраменко, 441 группа SpiralString 6679
action22k SpiralString 6197
f1from collections import Counterf1from collections import Counter
22
3class Spiral:3class Spiral:
n4    text_content = Nonen4    mystr = None
5    char_counter = None5    mydict = None
66
n7    def __init__(self, input_string):n7    def __init__(self, s):
8        self.char_counter = Counter(input_string)8        self.mydict = Counter(s)
9        self.text_content = ''.join([character * count for character, co9        self.mystr = ''.join([i * j for i, j in self.mydict.items()])
>unt in self.char_counter.items()]) 
1010
n11    def __add__(self, other_instance):n11    def __add__(self, other):
12        combined_counter = self.char_counter + other_instance.char_count12        d = self.mydict + other.mydict
>er 
13        return Spiral(''.join((char * freq for charfreq in combined_co13        return Spiral(''.join((i * j for ij in d.items())))
>unter.items()))) 
1414
n15    def __sub__(self, other_instance):n15    def __sub__(self, other):
16        difference_counter = self.char_counter - other_instance.char_cou16        d = self.mydict - other.mydict
>nter 
17        filtered_counter = {char: count for char, count in difference_co17        d = {i: j for i, j in d.items() if j > 0}
>unter.items() if count > 0} 
18        return Spiral(''.join((char * count for charcount in filtered_18        return Spiral(''.join((i * j for ij in d.items())))
>counter.items()))) 
1919
n20    def __mul__(self, multiplier):n20    def __mul__(self, other):
21        return Spiral(''.join((char * (count * multiplier) for char, cou21        return Spiral(''.join((i * j * other for i, j in self.mydict.ite
>nt in self.char_counter.items())))>ms())))
2222
23    def __iter__(self):23    def __iter__(self):
n24        return iter(self.text_content)n24        return iter(self.mystr)
2525
26    def __str__(self):26    def __str__(self):
n27        if len(self.text_content) < 3:n27        if len(self.mystr) < 3:
28            return self.text_content28            return self.mystr
29        segments = []29        l = []
30        total_length = len(self.text_content)30        n = len(self.mystr)
31        remaining_text = str(self.text_content)31        tmp = str(self.mystr)
32        max_rowsmax_cols = (0, 0)32        row, col = (0, 0)
33        for segment_size in range(2, total_length):33        for i in range(2, n):
34            if remaining_text[:segment_size]:34            if tmp[:i]:
35                if segment_size % 2 != 0:35                if i % 2 != 0:
36                    if len(remaining_text[:segment_size]) + 2 > max_rows36                    if len(tmp[:i]) + 2 > row:
>: 
37                        max_rows = len(remaining_text[:segment_size]) + 37                        row = len(tmp[:i]) + 2
>2 
38                    segments.append(remaining_text[:segment_size])38                    l.append(tmp[:i])
39                    remaining_text = remaining_text[segment_size - 1:]39                    tmp = tmp[i - 1:]
40                else:40                else:
n41                    if len(remaining_text[:segment_size]) > max_cols:n41                    if len(tmp[:i]) > col:
42                        max_cols = len(remaining_text[:segment_size])42                        col = len(tmp[:i])
43                    segments.append(remaining_text[:segment_size])43                    l.append(tmp[:i])
44                    remaining_text = remaining_text[segment_size - 1:]44                    tmp = tmp[i - 1:]
45            else:45            else:
46                break46                break
t47        spiral_grid = [[' ' for _ in range(max_cols + 1)] for _ in ranget47        grid = [[' ' for _ in range(col + 1)] for _ in range(row + 1)]
>(max_rows + 1)] 
48        directions = [(0, 1), (-1, 0), (0, -1), (1, 0)]48        d = [(0, 1), (-1, 0), (0, -1), (1, 0)]
49        direction_index = 049        step = 0
50        current_x, current_y = (max_rows // 2, max_cols // 2)50        x, y = (row // 2, col // 2)
51        for segment in segments:51        for i in l:
52            for position, character in enumerate(segment):52            for j, el in enumerate(i):
53                spiral_grid[current_x + position * directions[direction_53                grid[x + j * d[step][0]][y + j * d[step][1]] = el
>index][0]][current_y + position * directions[direction_index][1]] = char 
>acter 
54            current_x = current_x + (len(segment) - 1) * directions[dire54            x = x + (len(i) - 1) * d[step][0]
>ction_index][0] 
55            current_y = current_y + (len(segment) - 1) * directions[dire55            y = y + (len(i) - 1) * d[step][1]
>ction_index][1] 
56            direction_index = (direction_index + 1) % 456            step = (step + 1) % 4
57        non_empty_rows = [row for row in spiral_grid if any((cell != ' '57        filtered_rows = [row for row in grid if any((cell != ' ' for cel
> for cell in row))]>l in row))]
58        transposed_grid = list(zip(*non_empty_rows))58        transposed = list(zip(*filtered_rows))
59        non_empty_cols = [col for col in transposed_grid if any((cell !=59        filtered_cols = [col for col in transposed if any((cell != ' ' f
> ' ' for cell in col))]>or cell in col))]
60        final_grid = [list(row) for row in zip(*non_empty_cols)]60        res = [list(row) for row in zip(*filtered_cols)]
61        return '\n'.join((''.join(row) for row in final_grid))61        return '\n'.join((''.join(row) for row in res))
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op