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
903 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:
5 years ago
- Return the number of elements with non-`nil`.
5 years ago
- Compact, i.e., delete the elements with `nil` in the slice.
### Expected functions
```go
5 years ago
func Compact(ptr *[]string) int {
}
```
### Usage
Here is a possible [program](TODO-LINK) to test your function :
```go
package main
5 years ago
import (
"fmt"
piscine ".."
)
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:~/piscine/test$ go build
student@ubuntu:~/piscine/test$ ./test
5 years ago
a
b
c
Size after compacting: 3
a
b
c
student@ubuntu:~/piscine/test$
```