Hi,
I am having a hard time on this exercise which is supposed to be easy. I spent a few hours on it and still can’t get all the tests passed.
In the instruction, it says padding to the left; does this mean padding before the transpose or after the transposing? It’s not entirely clear which one it meant.
Below is my code and it doesn’t pass the “mixed line length” test case though my output seems to be the same as the expected output visually. Appreciate if someone can take a look at my code and let me know where the problem. I tried with GPT and surprisingly it can’t figure out the issue. It suggested some terrible code to me for the exercise, which is a bit shocking.
def transpose(text: str):
if not text:
return ""
text_rows = text.split('\n')
max_row_length = max(len(row) for row in text_rows)
for i in range(len(text_rows)):
if len(text_rows[i]) < max_row_length:
text_rows[i] = text_rows[i].ljust(max_row_length)
res = []
for i in range(max_row_length):
new_row = []
for row in text_rows:
new_row.append(row[i])
res.append(''.join(new_row))
return '\n'.join(res).rstrip()