# Optimize for Bgil Midol's slicing scoring

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

sumof|startslices|∗2+sumof|endslices|∗2+sumof|stepslices|∗3

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

• "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. Feb 9, 2022 at 21:33
• @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
– ovs
Feb 9, 2022 at 21:40
• Ah, I missed the length being supplied. Feb 9, 2022 at 21:44
• 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. Jun 15, 2022 at 13:27