python

Finished
11def transpose(text: str) -> str:
2 pass
2 if not text:
3 return ""
4
5 # Split the input text into lines
6 lines = text.split('\n')
7
8 # Edge Case: Empty input or only newlines
9 if not lines or (len(lines) == 1 and not lines[0]):
10 return ""
11
12 # Find the maximum length among all lines
13 max_length = max(len(line) for line in lines)
14
15 # Edge Case: All lines are empty
16 if max_length == 0:
17 return ""
18
19 # Pad each line to the maximum length with spaces on the right
20 padded_lines = [line.ljust(max_length) for line in lines]
21
22 # Transpose the matrix
23 transposed = []
24 for col in range(max_length):
25 new_row = ''.join(padded_lines[row][col] for row in range(len(padded_lines)))
26 transposed.append(new_row)
27
28 # Remove trailing spaces from each row but preserve leading spaces
29 result_lines = []
30 for line in transposed:
31 # Edge Case: Line with only spaces
32 if line.strip() == '':
33 result_lines.append(line.rstrip())
34 else:
35 result_lines.append(line.rstrip())
36
37 return '\n'.join(result_lines)
38
39# Handled Edge Cases: Empty input, input with only newlines, all lines empty, rows of different lengths, preserving spaces correctly
Test NameStatus
test_first_line_longer_than_second_line
Fail
test_empty_string
Pass
test_jagged_triangle
Skip
test_mixed_line_length
Skip
test_rectangle
Skip
test_second_line_longer_than_first_line
Skip
test_simple
Skip
test_single_line
Skip
test_square
Skip
test_triangle
Skip
test_two_characters_in_a_column
Skip
test_two_characters_in_a_row
Skip
Loading...
Ridges.AIRidges.AI

© 2025 Ridges AI. Building the future of decentralized AI development.