| 1 | 1 | def transpose(text: str) -> str: |
| 2 | | pass |
| 2 | # Edge Case: Empty input string |
| 3 | if not text: |
| 4 | return "" |
| 5 | |
| 6 | # Split the input text into lines |
| 7 | lines = text.split('\n') |
| 8 | |
| 9 | # Edge Case: Single line input |
| 10 | if len(lines) == 1: |
| 11 | return '\n'.join(lines[0]) if lines[0] else "" |
| 12 | |
| 13 | # Edge Case: Empty lines after splitting |
| 14 | if all(not line for line in lines): |
| 15 | return "" |
| 16 | |
| 17 | # Find the maximum length among all lines |
| 18 | max_length = max(len(line) for line in lines) if lines else 0 |
| 19 | |
| 20 | # Edge Case: All lines are empty |
| 21 | if max_length == 0: |
| 22 | return "" |
| 23 | |
| 24 | # Pad each line to the maximum length with spaces on the right |
| 25 | padded_lines = [line.ljust(max_length) for line in lines] |
| 26 | |
| 27 | # Transpose the matrix |
| 28 | transposed = [] |
| 29 | for col in range(max_length): |
| 30 | new_row = ''.join(padded_lines[row][col] for row in range(len(padded_lines))) |
| 31 | transposed.append(new_row) |
| 32 | |
| 33 | # Remove trailing spaces from each row but keep leading spaces |
| 34 | result_lines = [] |
| 35 | for line in transposed: |
| 36 | # Edge Case: Line with only spaces |
| 37 | if line.isspace() or not line: |
| 38 | result_lines.append(line) |
| 39 | else: |
| 40 | # Remove trailing spaces but keep leading spaces |
| 41 | result_lines.append(line.rstrip(' ')) |
| 42 | |
| 43 | return '\n'.join(result_lines) |
| 44 | |
| 45 | # Handled Edge Cases: Empty input string, Single line input, Empty lines after splitting, All lines are empty, Line with only spaces |