mirror of https://github.com/01-edu/public.git
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
879 B
70 lines
879 B
5 years ago
|
## Compact
|
||
6 years ago
|
|
||
6 years ago
|
### Instructions
|
||
6 years ago
|
|
||
5 years ago
|
Write a function `Compact` that takes a pointer to a slice of `string` as the argument.
|
||
5 years ago
|
This function must:
|
||
6 years ago
|
|
||
5 years ago
|
- Return the number of elements with non-zero value.
|
||
6 years ago
|
|
||
5 years ago
|
- Compact, i.e., delete the elements with zero-values in the slice.
|
||
5 years ago
|
|
||
|
### Expected functions
|
||
6 years ago
|
|
||
|
```go
|
||
5 years ago
|
func Compact(ptr *[]string) int {
|
||
6 years ago
|
|
||
|
}
|
||
|
```
|
||
6 years ago
|
|
||
6 years ago
|
### Usage
|
||
6 years ago
|
|
||
5 years ago
|
Here is a possible program to test your function :
|
||
6 years ago
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
5 years ago
|
import (
|
||
5 years ago
|
"fmt"
|
||
5 years ago
|
|
||
5 years ago
|
piscine ".."
|
||
5 years ago
|
)
|
||
|
|
||
|
const N = 6
|
||
6 years ago
|
|
||
|
func main() {
|
||
5 years ago
|
a := make([]string, N)
|
||
|
a[0] = "a"
|
||
|
a[2] = "b"
|
||
|
a[4] = "c"
|
||
5 years ago
|
|
||
5 years ago
|
for _, v := range a {
|
||
5 years ago
|
fmt.Println(v)
|
||
|
}
|
||
6 years ago
|
|
||
5 years ago
|
fmt.Println("Size after compacting:", piscine.Compact(&a))
|
||
5 years ago
|
|
||
5 years ago
|
for _, v := range a {
|
||
5 years ago
|
fmt.Println(v)
|
||
|
}
|
||
6 years ago
|
}
|
||
|
```
|
||
|
|
||
|
And its output :
|
||
|
|
||
|
```console
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$ go build
|
||
|
student@ubuntu:~/[[ROOT]]/test$ ./test
|
||
5 years ago
|
a
|
||
|
|
||
|
b
|
||
|
|
||
|
c
|
||
|
|
||
|
Size after compacting: 3
|
||
|
a
|
||
|
b
|
||
|
c
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$
|
||
6 years ago
|
```
|