<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p>

<pre>
P   A   H   N
A P L S I I G
Y   I   R
</pre>

<p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p>

<p>Write the code that will take a string and make this conversion given a number of rows:</p>

<pre>
string convert(string s, int numRows);
</pre>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3
<strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot;
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4
<strong>Output:</strong> &quot;PINALSIGYAHRPI&quot;
<strong>Explanation:</strong>
P     I    N
A   L S  I G
Y A   H R
P     I
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;A&quot;, numRows = 1
<strong>Output:</strong> &quot;A&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 1000</code></li>
	<li><code>s</code> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li>
	<li><code>1 &lt;= numRows &lt;= 1000</code></li>
</ul>

class Solution:
    def convert(self, s: str, numRows: int) -> str:
        arrays = []
        is_down = True
        row_ix = 0
        a = []
        for c in s:
            if is_down:
                a += [c]
                if row_ix != numRows-1:
                    row_ix += 1
                else:
                    arrays += [a]
                    if numRows > 2:
                        is_down = False
                        row_ix -= 1    
                        del a
                    else:
                        row_ix = 0
                        a=[]
            else:
                a = [None]*numRows
                a[row_ix] = c
                arrays += [a]
                row_ix -= 1
                if row_ix == 0:
                    is_down = True
                    a=[]
        if is_down:
            arrays += [a]
        #print(arrays)

        out = []
        for row_ix in range(numRows):
            for a in arrays:
                if row_ix < len(a) and a[row_ix] != None:
                    out += [a[row_ix]]
        return "".join(out)






class Solution2:
    def ix_to_zigzag_loc_old(ix, nrows):
        " assumes 0 indexed input"
        ix += 1
        #treat as 1ix
        ncols = (nrows-1)*(ix//(2*nrows-2))
        rem = ix % (2*nrows-2)
        ncols += 1 if rem > 0 and rem <= nrows else rem+1 - nrows
        if rem == 0:
            row = 1
        elif rem <= nrows:
            row = nrows -1
        else:
            row = nrows - (rem - nrows) -1
        return row, ncols

    def ix_to_zigzag_loc(ix, nrows):
        " assumes 0 indexed input"
        ncols = (nrows-1)*(ix//(2*nrows-2))
        rem = ix % (2*nrows-2)
        if rem > 0:
            ncols += 1 if rem <= nrows else rem+1 - nrows
        
        if ncols > 0:
            if rem == 0:
                row =  1
            elif rem <= nrows:
                row = rem-1
            else:
                row = nrows - (rem - nrows)
        else:
            if rem <= nrows:
                row = rem
            else:
                row = nrows - (rem - nrows)-1
        return row, ncols

    def str_to_zigzag(s, nrows):
        "returns 2d array of the word streched into tall N's"
        if nrows == 1:
            return [[s]]
        _, ncols = Solution.ix_to_zigzag_loc(len(s), nrows)
        out = [[" "]*(ncols+1) for _ in range(nrows)]
        for ix,c in enumerate(s):
            row,col = Solution.ix_to_zigzag_loc(ix, nrows)
            print(row, col, ix, c)
            out[row][col] = c
        print(out)
        return out

    def zigzag_to_out(arr):
        "reads 2d array right to left, top to bottom stripping all emptry str"
        return "".join([i for row in arr for i in row if i !=' '])

    def convert(self, s: str, numRows: int) -> str:
        if numRows == 0:
            return ""
        arr=Solution.str_to_zigzag(s, numRows)
        return Solution.zigzag_to_out(arr)
18000