Note that there are some explanatory texts on larger screens.

plurals
  1. POCode Golf: Zigzag pattern scanning
    text
    copied!<h2>The Challenge</h2> <p>The shortest code by character count that takes a single input integer <code>N</code> (N >= 3) and returns an array of indices that when iterated would traverse an <code>N</code>x<code>N</code> matrix according to the JPEG "zigzag" scan pattern. The following is an example traversal over an 8x8 matrix<sup><a href="http://en.wikipedia.org/wiki/Jpeg" rel="nofollow noreferrer">src</a></sup>:</p> <p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/JPEG_ZigZag.svg/220px-JPEG_ZigZag.svg.png" alt="zigzag layout pattern"></p> <h3>Examples</h3> <p>(The middle matrix is not part of the input or output, just a representation of the NxN matrix the input represents.)</p> <pre><code> 1 2 3 (Input) 3 --&gt; 4 5 6 --&gt; 1 2 4 7 5 3 6 8 9 (Output) 7 8 9 1 2 3 4 (Input) 4 --&gt; 5 6 7 8 --&gt; 1 2 5 9 6 3 4 7 10 13 14 11 8 12 15 16 (Output) 9 10 11 12 13 14 15 16 </code></pre> <h3>Notes</h3> <ul> <li>The resulting array's base should be appropriate for your language (e.g., Matlab arrays are 1-based, C++ arrays are 0-based).</li> <li>This is related to <a href="https://stackoverflow.com/questions/3024939">this question</a>.</li> </ul> <h2>Bonus</h2> <p>Extend your answer to take two inputs <code>N</code> and <code>M</code> (N, M >=3) and perform the same scan over an <code>N</code>x<code>M</code> matrix. (In this case <code>N</code> would be the number of columns and <code>M</code> the number of rows.)</p> <h3>Bonus Examples</h3> <pre><code> 1 2 3 4 (Input) 4 3 --&gt; 5 6 7 8 --&gt; 1 2 5 9 6 3 4 7 10 11 8 12 (Output) 9 10 11 12 1 2 3 (Input) 3 4 --&gt; 4 5 6 --&gt; 1 2 4 7 5 3 6 8 10 11 9 12 (Output) 7 8 9 10 11 12 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload