| 153 | |
| 154 | print("Example 3") |
| 155 | class ColumnPrinter: |
| 156 | def __init__(self): |
| 157 | self.columns = [] |
| 158 | |
| 159 | def append(self, data): |
| 160 | self.columns.append(data) |
| 161 | |
| 162 | def __str__(self): |
| 163 | row_count = 1 |
| 164 | for data in self.columns: |
| 165 | row_count = max(row_count, len(data.splitlines()) + 1) |
| 166 | |
| 167 | rows = [""] * row_count |
| 168 | for j in range(row_count): |
| 169 | for i, data in enumerate(self.columns): |
| 170 | line = data.splitlines()[max(0, j - 1)] |
| 171 | if j == 0: |
| 172 | padding = " " * (len(line) // 2) |
| 173 | rows[j] += padding + str(i) + padding |
| 174 | else: |
| 175 | rows[j] += line |
| 176 | |
| 177 | if (i + 1) < len(self.columns): |
| 178 | rows[j] += " | " |
| 179 | |
| 180 | return "\n".join(rows) |
| 181 | |
| 182 | |
| 183 | grid = LockingGrid(5, 9) |