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.
44 lines
584 B
44 lines
584 B
5 years ago
|
package solutions
|
||
|
|
||
|
func Slice(arr []string, nbr ...int) []string {
|
||
|
if len(nbr) == 0 {
|
||
|
return arr
|
||
|
}
|
||
|
|
||
|
first := nbr[0]
|
||
|
if len(nbr) == 1 {
|
||
|
if first < 0 {
|
||
|
first = len(arr) + first
|
||
|
if first < 0 {
|
||
|
return arr
|
||
|
}
|
||
|
}
|
||
|
return arr[first:]
|
||
|
} else {
|
||
|
second := nbr[1]
|
||
|
|
||
|
first = ifNegative(arr, first)
|
||
|
second = ifNegative(arr, second)
|
||
|
|
||
|
if first > second {
|
||
|
return []string{}
|
||
|
}
|
||
|
|
||
|
return arr[first:second]
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func ifNegative(arr []string, n int) int {
|
||
|
if n < 0 {
|
||
|
n = len(arr) + n
|
||
|
}
|
||
|
|
||
|
if n < 0 {
|
||
|
n = 0
|
||
|
} else if n > len(arr) {
|
||
|
n = len(arr)
|
||
|
}
|
||
|
|
||
|
return n
|
||
|
}
|