1
# Runtime: 96 ms (Top 61.23%) | Memory: 14 MB (Top 75.15%)3
def convert(self, s: str, numRows: int) -> str:5
# safety check to not process single row9
# safety check to not process strings shorter/equal than numRows13
# safety check to not process double rows15
# slice every other character16
return s[0::2] + s[1::2]18
# list that stores the lines19
# add lines with initial letters20
lines: list[str] = [letter for letter in s[:numRows]]22
# positive direction goes down23
# lines are created, so it's going up26
# track the position at which the letter will be added27
# position after bouncing off, after adding initial lines28
line_index: int = numRows - 231
# 0 can only be reached by going up32
# numRows only by going down33
edges: set[int] = {0, numRows}35
for letter in s[numRows:]:36
# add letter at tracked index position37
lines[line_index] += letter39
# prepare index before next loop iteration40
line_index += direction42
# reaching one of the edges43
if line_index in edges:45
direction = -direction46
# bounce off if bottom edge47
if line_index == numRows:48
line_index += direction * 2