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.

49 lines
1.1 KiB

## slice
### Instructions
Write a **function** that replicates the javascript function `slice`.
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.
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.
The integers can be lower than 0.
### Expected function
```go
func Slice(a []string, nbrs... int) []string{
}
```
5 years ago
### Usage
Here is a possible program to test your function :
```go
package main
import "fmt"
func main(){
a := []string{"coding", "algorithm", "ascii", "package", "golang"}
fmt.Println(Slice(a, 1))
fmt.Println(Slice(a, 2, 4))
fmt.Println(Slice(a, -3))
fmt.Println(Slice(a, -2, -1))
fmt.Println(Slice(a, 2, 0))
}
```
```console
student@ubuntu:~/student/test$ go build
student@ubuntu:~/student/test$ ./test
[algorithm ascii package golang]
[ascii package]
[ascii package golang]
[package]
[]
```