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.

69 lines
777 B

5 years ago
## Compact
### Instructions
Write a function `Compact` that takes a pointer to a slice of `string` as the argument.
5 years ago
This function must:
- Return the number of elements with non-zero value.
- Compact, i.e., delete the elements with zero-values 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() {
a := make([]string, N)
a[0] = "a"
a[2] = "b"
a[4] = "c"
5 years ago
for _, v := range a {
5 years ago
fmt.Println(v)
}
fmt.Println("Size after compacting:", piscine.Compact(&a))
5 years ago
for _, v := range a {
5 years ago
fmt.Println(v)
}
}
```
And its output :
```console
$ go run .
5 years ago
a
b
c
Size after compacting: 3
a
b
c
$
```