I've been stuck in the Python transpose exercise for almost a month now.
Here's my code:
def transpose(lines):
lines = lines.split('\n')
res = []
max_len = 0
for i, col in enumerate(lines):
max_len = len(col) if len(col) > max_len else max_len
for i in range(max_len):
res.append([])
for j, row in enumerate(lines):
for i, col in enumerate(row.ljust(max_len, " ")):
if not col.strip() and i == (max_len - 1) and j == len(lines) - 1:
continue
print(i, repr(col), max_len)
res[i].append(col)
return "\n".join(map(lambda x: "".join(x), res))
It solves all the test cases except lines = ["The longest line.", "A long line.", "A longer line.", "A line."].
Here's the diff between what's expected and what my function returns:
TAAA
h
elll
ooi
lnnn
ogge
n e.
- glr
? -
+ glr
- ei
? -
+ ei
- snl
? -
+ snl
- tei
? -
+ tei
- .n
? -
+ .n
- l e
? -
+ l e
- i .
? -
+ i .
- n
- e
- . + n
+ e
+ .
I'm guessing it's some subtle bug - could you help me out here?
Thanks!