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.

70 lines
881 B

5 years ago
## Compact
### Instructions
5 years ago
Write a function `Compact` that takes a pointer to a slice of strings as the argument.
This function must:
- Return the number of elements with non-`nil`.
- Compact, i.e., delete the elements with `nil` in the slice.
5 years ago
### Expected functions
```go
5 years ago
func Compact(ptr *[]string) int {
}
```
### Usage
Here is a possible program to test your function :
```go
package main
5 years ago
import (
"fmt"
5 years ago
piscine ".."
5 years ago
)
const N = 6
func main() {
5 years ago
arr := make([]string, N)
arr[0] = "a"
arr[2] = "b"
arr[4] = "c"
for _, v := range arr {
fmt.Println(v)
}
5 years ago
fmt.Println("Size after compacting:", piscine.Compact(&arr))
for _, v := range arr {
fmt.Println(v)
}
}
```
And its output :
```console
student@ubuntu:~/[[ROOT]]/test$ go build
student@ubuntu:~/[[ROOT]]/test$ ./test
5 years ago
a
b
c
Size after compacting: 3
a
b
c
student@ubuntu:~/[[ROOT]]/test$
```