10
\$\begingroup\$

I'm making a page selector for my super cool terminal-based blog program. In order for it to fit correctly into the design, it has to have some constraints.

The page selector can only display a certain number of numbers at a time due to the constraints of the UI system. If there are more characters then can fit, they must be removed and replaced with an ellipsis (which itself counts as a number). The system will try to display up to three numbers around the selected one (which is displayed surrounded by square brackets) and distribute the rest of the numbers evenly at the ends.

Your task is to, given a number of pages, selector width (always more than or equal to 3, and always odd), and currently selected number, all of which are both required and positive, print to STDOUT the resulting page selector.

Examples

In: 5 5 1
Out: < [1] 2 3 4 5 >

In: 5 3 1
Out: < [1] ... 5 >

In: 7 5 3
Out: < 1 ... [3] ... 7 >

In: 11 7 4
Out: < 1 ... 3 [4] 5 ... 11 >

In: 11 7 7
Out: < 1 ... 6 [7] 8 ... 11 >

In: 11 9 7
Out: < 1 2 ... 6 [7] 8 ... 10 11 >

In: 7 3 3
Out: < ... [3] ... >
\$\endgroup\$
16
  • 5
    \$\begingroup\$ Also, from the examples, it seems the first and last page numbers are mandatory, and higher precedence than the three numbers around the selected one. Could you please mention that in the text too? \$\endgroup\$
    – Sundar R
    Mar 5 at 14:03
  • 2
    \$\begingroup\$ 1) The second-last testcase seems to display seven characters despite the character limit being 6. Could you please fix this? 2) Your test cases seem to imply that we must always use the greatest number of characters possible. Are we allowed to not do that, meaning output less characters than the maximum? For instance, it might be advantageous to always output odd numbers of characters. \$\endgroup\$
    – ophact
    Mar 5 at 15:11
  • 2
    \$\begingroup\$ Not sure why the problem statement talks about "characters" and then the problem itself seems to be about objects (page-numbers and ellipses). \$\endgroup\$ Mar 5 at 17:21
  • 2
    \$\begingroup\$ So, what's the output for 7 3 3? And for 7 4 3? \$\endgroup\$ Mar 5 at 18:45
  • 3
    \$\begingroup\$ I still don't see what we do for cases like 7 3 3 and 7 4 3. The only current answer gives weird output for these inputs too (and those outputs cannot be correct). \$\endgroup\$ Mar 5 at 20:16

1 Answer 1

3
\$\begingroup\$

Python3, 380 bytes:

lambda r,w,c:'< '+' '.join(map(str,Q([*R(1,c)],J:=(min((w-1)//2+(w-1)%2,c-1)),w+~J,len(V:=[*R(c+1,r+1)]))+[f'[{c}]']+W(V,w+~J)))+' >'
R,*E=range,'...'
W=lambda r,l:E if l<2and len(r)>1else(r if l==len(r)else r[:l-2]+E+[r[-1]])
Q=lambda r,l,L,V:r[:l-1]+E if L==1and V>1else(E if l<2and len(r)>1else(r if L-2or r==[]else r[:-1]+E)if l==len(r)else[r[0],*E,r[-1]]if L-2else r[:l-1]+E)

Try it online!

Output:

< [1] 2 3 4 5 >
< [1] ... 5 >
< 1 ... [3] ... 7 >
< 1 2 3 [4] 5 ... 11 >
< 1 ... 6 [7] 8 ... 11 >
< 1 2 ... [7] ... 11 >
< 1 2 ... [6] ... 11 >
< ... [3] ... >
< 1 ... [3] ... >
\$\endgroup\$
4
  • \$\begingroup\$ Not that this makes the correctness any less dubious, but 380 with some simple golfs applied. May be possible to go lower moving to tuples. \$\endgroup\$ Mar 6 at 0:20
  • \$\begingroup\$ You may want to try this again considering I just cleaned up the specs a bunch \$\endgroup\$
    – Ginger
    Mar 6 at 15:13
  • \$\begingroup\$ @GingerIndustries why is 11 9 7 < 1 2 ... 6 [7] 8 ... 10 11 > and not < 1 2 ... 6 [7] 8 9 10 11 >? Surely the second ... can be replaced with 9? \$\endgroup\$
    – Ajax1234
    Mar 6 at 15:17
  • 1
    \$\begingroup\$ @Ajax1234 In the interest of having consistent specs \$\endgroup\$
    – Ginger
    Mar 6 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.