Note that there are some explanatory texts on larger screens.

plurals
  1. POCode Golf - Generate nearby page numbers based on the current page
    text
    copied!<p>The challenge is to create an algorithm for generating a specifically-sized subset of numbers in a sequence based on the current position in that sequence.</p> <p>While navigating through the many pages of content on a busy site like Stack Overflow or Digg it is often desirable to give the user a way to quickly jump to the first page, the last page or a specific page which is <em>near</em> the current page they are viewing. </p> <h2>Requirements</h2> <ul> <li>First and last page numbers are <em>always</em> displayed</li> <li>The subset of page numbers will contain the current page number as well as page numbers before and/or after it (depending on current page)</li> <li>The subset of page numbers will always be a fixed number of pages and can never exceed or fall short of that fixed number unless: <ul> <li><code>totalPages &lt; fixedWidth</code></li> </ul></li> <li>The position of the current page number in the subset is fixed unless: <ul> <li><code>1 &lt;= currentPage &lt; (fixedWidth - defaultPostion)</code> or</li> <li><code>(totalPages - currentPage) &lt; (fixedWidth - defaultPostion)</code></li> </ul></li> <li>Output should indicate when there is a difference <em>greater than 0</em> between the first page of data and the first page of the subset as well as between the last page of the subset and the last page of data. This indicator should appear at most once in either position.</li> </ul> <p>If you can't picture this yet, take a look at your Stack Overflow profile under questions/answers. If you have more than 10 of either one, you should see paging links at the bottom which are generated in exactly this fashion. That, or scroll to the bottom of <a href="http://digg.com" rel="nofollow noreferrer">http://digg.com</a> and observe their paging control.</p> <h2>Examples</h2> <p>All examples assume a subset size of 5 and the current page in position 3, but these should be configurable in your solution. <code>...</code> indicates the gap between page numbers, <code>[x]</code> indicates the current page.</p> <hr> <p>Current Page: 1 of 30</p> <p>Output: <code>[x][2][3][4][5]...[30]</code></p> <hr> <p>Current Page: 2 of 30</p> <p>Output: <code>[1][x][3][4][5]...[30]</code></p> <hr> <p>Current Page: 13 of 30</p> <p>Output: <code>[1]...[11][12][x][14][15]...[30]</code></p> <hr> <p>Current Page: 27 of 30</p> <p>Output: <code>[1]...[25][26][x][28][29][30]</code></p> <hr> <p>Current Page: 30 of 30</p> <p>Output: <code>[1]...[26][27][28][29][x]</code></p> <hr> <p>Current Page: 3 of 6</p> <p>Output: <code>[1][2][x][4][5][6]</code></p> <hr> <p>Current Page: 4 of 7</p> <p>Output: <code>[1][2][3][x][5][6][7]</code></p> <hr> <h2>Additional Clarifications</h2> <ul> <li>First and last pages do not count toward <code>numberOfPages</code> unless they are sequentially part of <code>numberOfPages</code> as in <code>[1][x][3][4][5]...[30]</code> or <code>[1]...[26][27][28][x][30]</code>, but not in <code>[1]...[8][9][x][11][12]...[30]</code></li> <li>No gap indicator should be included if the distance between either end of the subset and the first or last page is less than 1. Thus, it is possible to have a non-breaking sequence of pages up to <code>fixedWidth + 2</code> as in <code>[1][2][3][x][5][6]...[15]</code> or <code>[1][2][3][x][5][6][7]</code></li> </ul> <p><strong>Solutions in any and all languages are welcome.</strong></p> <p><strong>Good luck!</strong></p>
 

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