Transpose2026-04-24
My Solution: Exercism Transpose solution
Instructions
Transpose a list of strings so that rows become columns and shorter lines are padded with spaces only where needed.
Rules:
- Return an empty string for empty input.
- Preserve the shape implied by the longest line.
- Pad missing characters with spaces after trimming any trailing placeholder padding.
Solution
def transpose(text):
if text == "":
return ""
lines = text.split('\n')
max_len = 0
for line in lines:
max_len = max(max_len, len(line))
transpose = []
for col in range(max_len):
new_row = ""
for row in lines:
if col < len(row):
new_row += row[col]
else:
new_row += '~'
new_row = new_row.rstrip("~")
new_row = new_row.replace("~", " ")
transpose.append(new_row)
return "\n".join(transpose)
Syntax Notes
- The early return handles the empty-input edge case directly.
- The placeholder character
~makes it easy to distinguish real characters from padded gaps before converting them to spaces. - The outer loop builds each output row by walking one column at a time across the input lines.