

def convert(s, n_rows):
    if n_rows == 1:
        return s
    out = []
    for row in xrange(n_rows):
        idx = row
        while idx < len(s):
            out.append(s[idx])
            if row != 0 and row != n_rows - 1:
                i = idx + 2 * (n_rows - 1 - row)
                if i < len(s):
                    out.append(s[i])
            idx += 2 * n_rows - 2
    return ''.join(out)


if __name__ == '__main__':
    print convert('ABCDE', 4)
    print convert('PAYPALISHIRING', 3)
    print convert('PAYPALISHIRING', 4)
    print convert('PAYPALISHIRING', 5)
