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.
51 lines
1.2 KiB
51 lines
1.2 KiB
5 years ago
|
## slice
|
||
|
|
||
|
### Instructions
|
||
|
|
||
5 years ago
|
The function receives a slice of strings and one or more integers, and returns a slice of strings. The returned slice is part of the received one but cut from the position indicated in the first int, until the position indicated by the second int.
|
||
5 years ago
|
|
||
5 years ago
|
In case there only exists one int, the resulting slice begins in the position indicated by the int and ends at the end of the received slice.
|
||
5 years ago
|
|
||
5 years ago
|
The integers can be lower than 0.
|
||
5 years ago
|
|
||
|
### Expected function
|
||
|
|
||
|
```go
|
||
5 years ago
|
func Slice(a []string, nbrs... int) []string{
|
||
5 years ago
|
|
||
|
}
|
||
|
```
|
||
|
|
||
5 years ago
|
### Usage
|
||
5 years ago
|
|
||
|
Here is a possible program to test your function :
|
||
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
5 years ago
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
piscine ".."
|
||
|
)
|
||
5 years ago
|
|
||
|
func main(){
|
||
5 years ago
|
a := []string{"coding", "algorithm", "ascii", "package", "golang"}
|
||
5 years ago
|
fmt.Printf("%#v\n", piscine.Slice(a, 1))
|
||
|
fmt.Printf("%#v\n", piscine.Slice(a, 2, 4))
|
||
|
fmt.Printf("%#v\n", piscine.Slice(a, -3))
|
||
|
fmt.Printf("%#v\n", piscine.Slice(a, -2, -1))
|
||
|
fmt.Printf("%#v\n", piscine.Slice(a, 2, 0))
|
||
5 years ago
|
}
|
||
|
```
|
||
|
|
||
|
```console
|
||
|
student@ubuntu:~/student/test$ go build
|
||
|
student@ubuntu:~/student/test$ ./test
|
||
5 years ago
|
[]string{"algorithm", "ascii", "package", "golang"}
|
||
|
[]string{"ascii", "package"}
|
||
|
[]string{"ascii", "package", "golang"}
|
||
|
[]string{"package"}
|
||
|
[]string(nil)
|
||
5 years ago
|
```
|