# Clean python code with helper function

• ``````class Solution:
# @return a string
def convert(self, text, nRows):

def back_and_forth(interval, repeat=1):
new_interval = interval + interval[-2:0:-1]
for _ in range(repeat):
for i in new_interval:
yield i

if nRows == 1:
return text
period = 2*(nRows-1)
n_periods = len(text) / period + 1
zigzag = back_and_forth(range(nRows), repeat=n_periods)
results = [[] for _ in range(nRows)]
for c, row in zip(text, zigzag):
results[row].append(c)
return ''.join(''.join(row) for row in results)``````

• ``````def convert(self, s, nRows):
def back_and_forth(interval, repeat=1):
new_interval = interval + interval[-2:0:-1]
for _ in range(repeat):
for i in new_interval:
yield i

if nRows <= 1 or len(s)<=nRows:
return s
period = 2*(nRows-1)
n_periods = len(s) / period + 1
zigzag = back_and_forth(range(nRows), repeat=n_periods)
results = [[] for _ in range(nRows)]
for c, row in zip(s, zigzag):
results[row].append(c)
return ''.join(''.join(row) for row in results)``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.