Page range labels
EasyOnline Assessment
Write the "pages to print" label for a print dialog from the page numbers a user ticked.
The user can tick pages in any order and may tick the same page twice. Build the label like this:
- Ignore repeats and sort the distinct page numbers.
- Split them into runs of consecutive page numbers (
4, 5, 6is a run;4, 6is two runs). - A run with at least
min_lenpages is written asfirst-last. A shorter run is written as its pages one by one, each separated by a comma. - Join everything in ascending order with commas and no spaces.
For example, with min_len 3 the pages 1 2 3 5 6 9 become 1-3,5,6,9: the run 1 2 3 is long enough, but 5 6 is only 2 pages. With min_len 2 the same pages become 1-3,5-6,9.
A single page is a run of length 1, so it is written as just the number (never 9-9), whatever min_len is. If no pages were ticked, the label is the empty string.
Input: pages, the ticked page numbers in the order they were ticked; min_len, the shortest run that is written as a range.
Output: the label string.
Example 1
- Input:
- [[1,2,3,5,6,9],3]
- Output:
- "1-3,5,6,9"
- Explanation
- Distinct sorted pages are 1, 2, 3, 5, 6, 9. The run 1-3 has 3 pages, enough for a range. The run 5-6 has only 2, below the minimum of 3, so it is written
5,6. The lone 9 is written plainly. Label1-3,5,6,9.
Example 2
- Input:
- [[10,8,20,7,9,8],4]
- Output:
- "7-10,20"
- Explanation
- After removing repeats and sorting: 7, 8, 9, 10, 20. The run 7-10 has 4 pages, which meets the minimum of 4 exactly, so it is
7-10. The 20 stands alone. Label7-10,20.
Example 3
- Input:
- [[4,7,6],1]
- Output:
- "4,6-7"
- Explanation
- With
min_len1 every run would qualify by length, but the lone page 4 is a single page and is written4, not4-4. The run 6-7 is written6-7. Label4,6-7.
Constraints
0 <= pages.length <= 5001 <= pages[i] <= 100,0001 <= min_len <= 600