mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
534 B
43 lines
534 B
5 years ago
|
package correct
|
||
5 years ago
|
|
||
5 years ago
|
func ifNegative(a []string, n int) int {
|
||
|
if n < 0 {
|
||
|
n = len(a) + n
|
||
|
}
|
||
|
|
||
|
if n < 0 {
|
||
|
n = 0
|
||
|
} else if n > len(a) {
|
||
|
n = len(a)
|
||
|
}
|
||
|
|
||
|
return n
|
||
|
}
|
||
|
|
||
|
func Slice(a []string, nbr ...int) []string {
|
||
5 years ago
|
if len(nbr) == 0 {
|
||
5 years ago
|
return a
|
||
5 years ago
|
}
|
||
|
|
||
|
first := nbr[0]
|
||
|
if len(nbr) == 1 {
|
||
|
if first < 0 {
|
||
5 years ago
|
first = len(a) + first
|
||
5 years ago
|
if first < 0 {
|
||
5 years ago
|
return a
|
||
5 years ago
|
}
|
||
|
}
|
||
5 years ago
|
return a[first:]
|
||
5 years ago
|
}
|
||
5 years ago
|
second := nbr[1]
|
||
5 years ago
|
|
||
5 years ago
|
first = ifNegative(a, first)
|
||
|
second = ifNegative(a, second)
|
||
5 years ago
|
|
||
5 years ago
|
if first > second {
|
||
|
return nil
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
return a[first:second]
|
||
5 years ago
|
}
|