In Bgil Midol's question, the score of a sequence of slice is defined as


Such scoring method makes multiple slices possibly take less score, e.g.

  • [::25] 75
  • [::5][::5] 30

Given the length of a string, an upper bound of number of slices1, and the one slice it equals to2, output a way of slicing with lowest score. You can assume that the result after slicing isn't empty.

6 1 [3:4]         -> [3:-2]
6 9 [3:4]         -> [:-2][-1:]
1000 1 [::25]     -> [::25]
1000 2 [::25]     -> [::5][::5]
1000 9 [1::25]    -> [1::5][::5]
1000 9 [5::25]    -> [::5][1::5]
9100 9 [90::91]   -> [::-91][::-1]

Shortest code in each language wins.

1. Notice that [:] is a valid slice with no effect to score

2. It can be proven that every sequence of slice can equal to one slice for a fixed string before slice

  • \$\begingroup\$ "It can be proven that every sequence of slice can equal to one slice." Is this true with negative indices? If so, what one slice is equivalent to [::-2][::-1]? (Similarly, can the input slices contain negative indices?) Test cases containing ends should probably also be supplied. \$\endgroup\$ Feb 9, 2022 at 21:33
  • 1
    \$\begingroup\$ @UnrelatedString I'm quite sure that is true as long as the length of the string is known beforehand (which is the case here and in the related challenge). The step of the combined slice is the product of all steps, so in your case either [1::2] or [0::2](==[::2]) depending on the parity of the length of the string \$\endgroup\$
    – ovs
    Feb 9, 2022 at 21:40
  • \$\begingroup\$ Ah, I missed the length being supplied. \$\endgroup\$ Feb 9, 2022 at 21:44
  • \$\begingroup\$ What is startslices, endslices, and stepslices? And what does the [::] notation mean? This seems underspecified, or at least it requires some background knowledge not provided in the question. \$\endgroup\$ Jun 15, 2022 at 13:27


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.