Everyone loves slicing in python, as in array[5]
to access the fifth element or array[:5]
to access the zero through fourth element or array[::-1]
to reverse an array. However, these all have seemingly distinct notations. When and where should the colons and integers be placed?
Task
Your task is to change any valid slice syntax into a slice with as many colons and integers as possible. To help with that, we will make some definitions. Valid slice syntax is anything directly after "Given a slice" in this section.
Define <POS>
to be any positive integer, that is any digit 1-9
followed by any amount of digits 0-9
.
Define <NEG>
to be any negative integer, that is a -
followed by <POS>
Define <INT>
to be <POS>
, <NEG>
, or <ZERO>
Define sgn(a)
to be the sign of a
: 1 for positive a
, -1 for negative a
, and 0 for a=0
.
For all of the following, a
and b
are <INT>
and c
is <POS>
or <NEG>
unless otherwise specified.
- Given a slice
[]
, your program may have undefined behavior. - Given a slice
[a]
, your program or function must return[a:a+k:c]
for some<POS>
or<NEG>
value ofc
and0 < abs(k) <= abs(c)
and (a+k < 0
ifc < 0
) and (a+b > 0
ifc > 0
) - Given a slice
[:]
, your program must return[0::1]
. - Given a slice
[a:]
, your program must return[a::1]
. - Given a slice
[:b]
, your program must return[0:b:1]
. - Given a slice
[a:b]
wherea < b
andsgn(a)+sgn(b) != 0
your program must return[a:b:1]
. - Given a slice
[::]
, your program must return[0::1]
. - Given a slice
[a::]
, your program must return[a::1]
. - Given a slice
[:b:]
, your program must return[0:b:1]
. - Given a slice
[::c]
, your program must return[0::c]
ifc
is<POS>
and[-1::c]
otherwise. - Given a slice
[a:b:]
wherea < b
andsgn(a)+sgn(b) != 0
, your program must return[a:b:1]
. - Given a slice
[a::c]
, your program must return[a::c]
. - Given a slice
[:b:c]
, your program must return[0:b:c]
ifc
is<POS>
and[-1:b:c]
otherwise. - Given a slice
[a:b:c]
your program must return[a:b:c]
.
Example
The "slice" [5]
gives the fifth element of an array.
Your program or function can output [5:6:1]
or [5:7:2]
or [5:1000:1000]
or [5:4:-1]
or [5:0:-5]
.
The program cannot output [5:7:1]
or [5:-6:1]
or [5:-6:-1]
or [5:6:-1]
.
Test Cases
Input Output
[1] [1:2:1] [Output may vary]
[-1] [-1:-2:-1] [Output may vary]
[:] [0::1]
[1:] [1::1]
[-1:] [-1::1]
[0:3] [0:3:1]
[-3:-1] [-3:-1:1]
[::] [0::1]
[0::] [0::1]
[:5:] [0:5:1]
[::2] [0::2]
[::-2] [-1::-2]
[2:4:] [2:4:1]
[2::3] [2::3]
[:2:1] [0:2:1]
[:2:-1] [-1:2:-1]
[1:2:3] [1:2:3]
Rules
- You do not have to do any input validation.
- The output and input may include or exclude the brackets
[]
. - The output and input may be a 3-tuple instead of a
:
-delimited string. - This is code-golf, so shortest code in each language wins.
[a:]
and[:b]
have different behavior despite having the same elements. \$\endgroup\$e
) that would represent an empty slot, since[a:]
would be(a,e)
and[:b]
would be(e,b)
, so if that's the only reason not to use variable-size tuples (or nested tuples) then I'll go ahead and do it unless I can think of a more convenient encoding. \$\endgroup\$