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