Омаров Микаил, 321 группа SpiralString 5635
Алтухов Егор, 321 группа SpiralString 5628
f1class Spiral:f1class Spiral:
22
n3    def __init__(self, text: str):n3    def __init__(self, s: str):
4        self.char_count = {}4        self.counts = {}
5        for char in text:5        for ch in s:
6            self.char_count[char] = self.char_count.get(char, 0) + 16            self.counts[ch] = self.counts.get(ch, 0) + 1
7        self.spiral_string = ''.join((char * count for char, count in se7        self.spiral = ''.join((ch * count for ch, count in self.counts.i
>lf.char_count.items()))>tems()))
88
n9    def build_spiral_layer(self, current_index, previous_layer, size, ton9    def f(self, n, s1, k, l):
>tal_length): 
10        if size > 3:10        if k > 3:
11            spiral_grid = Spiral.create_matrix(previous_layer, size)11            s = Spiral.matrix(s1, k)
12        else:12        else:
n13            spiral_grid = []n13            s = []
14            spiral_grid.append(' ' * size)14            s.append(' ' * k)
15            spiral_grid.append(' ' * size)15            s.append(' ' * k)
16            spiral_grid.append(' ' * size)16            s.append(' ' * k)
17        for row in range(2, size):17        for i in range(2, k):
18            if current_index == total_length:18            if n == l:
19                break19                break
n20            spiral_grid[row] = spiral_grid[row][:1] + self.spiral_stringn20            s[i] = s[i][:1] + self.spiral[n] + s[i][2:]
>[current_index] + spiral_grid[row][2:] 
21            current_index += 121            n += 1
22        for col in range(2, size):22        for i in range(2, k):
23            if current_index == total_length:23            if n == l:
24                break24                break
n25            spiral_grid[size - 1] = spiral_grid[size - 1][:col] + self.sn25            s[k - 1] = s[k - 1][:i] + self.spiral[n] + s[k - 1][i + 1:]
>piral_string[current_index] + spiral_grid[size - 1][col + 1:] 
26            current_index += 126            n += 1
27        for row in range(size - 2, 0, -1):27        for i in range(k - 2, 0, -1):
28            if current_index == total_length:28            if n == l:
29                break29                break
n30            spiral_grid[row] = spiral_grid[row][:size - 1] + self.spiraln30            s[i] = s[i][:k - 1] + self.spiral[n]
>_string[current_index] 
31            current_index += 131            n += 1
32        for col in range(size):32        for i in range(k):
33            if current_index == total_length:33            if n == l:
34                break34                break
n35            spiral_grid[0] = spiral_grid[0][:size - 1 - col] + self.spirn35            s[0] = s[0][:k - 1 - i] + self.spiral[n] + s[0][k - i:]
>al_string[current_index] + spiral_grid[0][size - col:] 
36            current_index += 136            n += 1
37        return (current_index, spiral_grid)37        return (n, s)
3838
n39    def create_matrix(previous_layer, size):n39    def matrix(s1, k):
40        grid = []40        s = []
41        grid.append(' ' * size)41        s.append(' ' * k)
42        grid.append(' ' * size)42        s.append(' ' * k)
43        for line in previous_layer:43        for i in s1:
44            grid.append(' ' * 2 + line + ' ' * 2)44            s.append(' ' * 2 + i + ' ' * 2)
45        grid.append(' ' * size)45        s.append(' ' * k)
46        grid.append(' ' * size)46        s.append(' ' * k)
47        return grid47        return s
4848
49    def __str__(self):49    def __str__(self):
n50        current_index = 0n50        n = 0
51        spiral_size = 351        k = 3
52        total_length = len(self.spiral_string)52        l = len(self.spiral)
53        result = self.build_spiral_layer(current_index, None, spiral_siz53        res = self.f(n, None, k, l)
>e, total_length) 
54        while result[0] != total_length:54        while res[0] != l:
55            spiral_size += 455            k += 4
56            result = self.build_spiral_layer(result[0], result[1], spira56            res = self.f(res[0], res[1], k, l)
>l_size, total_length) 
57        cropped_result = Spiral.crop_matrix(result[1])57        cropped = Spiral.crop(res[1])
58        return '\n'.join(cropped_result)58        return '\n'.join(cropped)
5959
n60    def crop_matrix(matrix_lines):n60    def crop(matrix_lines):
61        lines = [line.rstrip() for line in matrix_lines]61        lines = [line.rstrip() for line in matrix_lines]
62        while lines and (not lines[0].strip()):62        while lines and (not lines[0].strip()):
63            lines.pop(0)63            lines.pop(0)
64        while lines and (not lines[-1].strip()):64        while lines and (not lines[-1].strip()):
65            lines.pop(-1)65            lines.pop(-1)
n66        left_margin = 0n66        left = 0
67        right_margin = max((len(line) for line in lines))67        right = max((len(line) for line in lines))
68        while left_margin < right_margin and all((len(line) <= left_marg68        while left < right and all((len(line) <= left or line[left] == '
>in or line[left_margin] == ' ' for line in lines)):> ' for line in lines)):
69            left_margin += 169            left += 1
70        while right_margin > left_margin and all((len(line) <= right_mar70        while right > left and all((len(line) <= right - 1 or line[right
>gin - 1 or line[right_margin - 1] == ' ' for line in lines)):> - 1] == ' ' for line in lines)):
71            right_margin -= 171            right -= 1
72        return [line[left_margin:right_margin] for line in lines]72        return [line[left:right] for line in lines]
7373
74    def __add__(self, other):74    def __add__(self, other):
75        result = Spiral('')75        result = Spiral('')
n76        result.char_count = self.char_count.copy()n76        result.counts = self.counts.copy()
77        for char, count in other.char_count.items():77        for ch, count in other.counts.items():
78            if char in result.char_count:78            if ch in result.counts:
79                result.char_count[char] += count79                result.counts[ch] += count
80            else:80            else:
n81                result.char_count[char] = countn81                result.counts[ch] = count
82        result.spiral_string = ''.join((char * count for char, count in 82        result.spiral = ''.join((ch * count for ch, count in result.coun
>result.char_count.items()))>ts.items()))
83        return result83        return result
8484
85    def __sub__(self, other):85    def __sub__(self, other):
86        result = Spiral('')86        result = Spiral('')
n87        result.char_count = self.char_count.copy()n87        result.counts = self.counts.copy()
88        for char, count in other.char_count.items():88        for ch, count in other.counts.items():
89            if char in result.char_count:89            if ch in result.counts:
90                result.char_count[char] -= count90                result.counts[ch] -= count
91                if result.char_count[char] <= 0:91                if result.counts[ch] <= 0:
92                    del result.char_count[char]92                    del result.counts[ch]
93        result.spiral_string = ''.join((char * count for char, count in 93        result.spiral = ''.join((ch * count for ch, count in result.coun
>result.char_count.items()))>ts.items()))
94        return result94        return result
9595
n96    def __mul__(self, multiplier):n96    def __mul__(self, n):
97        result = Spiral('')97        result = Spiral('')
n98        result.char_count = {char: count * multiplier for char, count inn98        result.counts = {ch: count * n for ch, count in self.counts.item
> self.char_count.items()}>s()}
99        result.spiral_string = ''.join((char * count for char, count in 99        result.spiral = ''.join((ch * count for ch, count in result.coun
>result.char_count.items()))>ts.items()))
100        return result100        return result
101101
102    def __iter__(self):102    def __iter__(self):
n103        for char, count in self.char_count.items():n103        for ch, count in self.counts.items():
104            for _ in range(count):104            for _ in range(count):
t105                yield chart105                yield ch
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op