forked from root/public
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.
53 lines
1.3 KiB
53 lines
1.3 KiB
5 years ago
|
## slice
|
||
|
|
||
|
### Instructions
|
||
|
|
||
![]()
4 years ago
|
Write a **function** that replicates the JavaScript function `slice`.
|
||
5 years ago
|
|
||
![]()
4 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
|
|
||
![]()
4 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
|
|
||
![]()
4 years ago
|
The integers can be lower than 0.
|
||
5 years ago
|
|
||
|
### Expected function
|
||
|
|
||
|
```go
|
||
![]()
4 years ago
|
func Slice(a []string, nbrs... int) []string{
|
||
5 years ago
|
|
||
|
}
|
||
|
```
|
||
|
|
||
![]()
4 years ago
|
### Usage
|
||
5 years ago
|
|
||
|
Here is a possible program to test your function :
|
||
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
![]()
4 years ago
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
piscine ".."
|
||
|
)
|
||
5 years ago
|
|
||
|
func main(){
|
||
![]()
4 years ago
|
a := []string{"coding", "algorithm", "ascii", "package", "golang"}
|
||
![]()
4 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
|
||
![]()
4 years ago
|
[]string{"algorithm", "ascii", "package", "golang"}
|
||
|
[]string{"ascii", "package"}
|
||
|
[]string{"ascii", "package", "golang"}
|
||
|
[]string{"package"}
|
||
|
[]string(nil)
|
||
![]()
5 years ago
|
```
|