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.
87 lines
1.4 KiB
87 lines
1.4 KiB
5 years ago
|
package main
|
||
|
|
||
5 years ago
|
import (
|
||
5 years ago
|
student "student"
|
||
|
|
||
4 years ago
|
"github.com/01-edu/public/go/tests/lib"
|
||
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 {
|
||
|
if len(nbr) == 0 {
|
||
|
return a
|
||
|
}
|
||
|
|
||
|
first := nbr[0]
|
||
|
if len(nbr) == 1 {
|
||
|
if first < 0 {
|
||
|
first = len(a) + first
|
||
|
if first < 0 {
|
||
|
return a
|
||
|
}
|
||
|
}
|
||
|
return a[first:]
|
||
|
}
|
||
|
second := nbr[1]
|
||
|
|
||
|
first = ifNegative(a, first)
|
||
|
second = ifNegative(a, second)
|
||
|
|
||
|
if first > second {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
return a[first:second]
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
func main() {
|
||
5 years ago
|
elems := [][]interface{}{
|
||
5 years ago
|
{
|
||
|
[]string{"coding", "algorithm", "ascii", "package", "golang"},
|
||
|
1,
|
||
|
},
|
||
|
{
|
||
|
[]string{"coding", "algorithm", "ascii", "package", "golang"},
|
||
|
-3,
|
||
|
},
|
||
|
{
|
||
|
[]string{"coding", "algorithm", "ascii", "package", "golang"},
|
||
|
2, 4,
|
||
|
},
|
||
|
{
|
||
|
[]string{"coding", "algorithm", "ascii", "package", "golang"},
|
||
|
-2, -1,
|
||
|
},
|
||
|
{
|
||
|
[]string{"coding", "algorithm", "ascii", "package", "golang"},
|
||
|
2, 0,
|
||
|
},
|
||
|
}
|
||
|
|
||
5 years ago
|
s := lib.MultRandWords()
|
||
5 years ago
|
|
||
5 years ago
|
elems = append(elems, []interface{}{s, -len(s) - 10, -len(s) - 5})
|
||
5 years ago
|
|
||
|
for i := 0; i < 3; i++ {
|
||
5 years ago
|
s = lib.MultRandWords()
|
||
5 years ago
|
elems = append(elems, []interface{}{s, lib.RandIntBetween(-len(s)-10, len(s)+10), lib.RandIntBetween(-len(s)-8, len(s)+10)})
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
for _, a := range elems {
|
||
5 years ago
|
lib.Challenge("Slice", student.Slice, slice, a...)
|
||
5 years ago
|
}
|
||
|
}
|