mirror of https://github.com/01-edu/public.git
augusto-mantilla
5 years ago
committed by
GitHub
2 changed files with 77 additions and 21 deletions
@ -1,41 +1,69 @@
|
||||
## Compact |
||||
## compact |
||||
|
||||
### Instructions |
||||
|
||||
Write a function that will take a pointer to a array as parameter and overwrites any element that points to `nil`. |
||||
Écrire une fonction `Compact` qui prend un pointeur sur slice de `strings` comme paramètre. |
||||
Cette fonction doit: |
||||
|
||||
- If you not sure what the function does. It exists in Ruby. |
||||
- Retourner le nombre d'éléments avec des valeurs non-`nil` |
||||
|
||||
### Expected functions |
||||
- Comprimer, c.à.d., effacer les éléments qui ont une valeur `nil` dans la slice. |
||||
|
||||
### Fonction attendue |
||||
|
||||
```go |
||||
func Compact(ptr *[]string, length int) int { |
||||
func Compact(ptr *[]string) int { |
||||
|
||||
} |
||||
``` |
||||
|
||||
### Usage |
||||
### Utilisation |
||||
|
||||
Here is a possible [program](TODO-LINK) to test your function : |
||||
Voici un éventuel [programme](TODO-LINK) pour tester votre fonction : |
||||
|
||||
```go |
||||
package main |
||||
|
||||
import fmt |
||||
import ( |
||||
"fmt" |
||||
|
||||
piscine ".." |
||||
) |
||||
|
||||
const N = 6 |
||||
|
||||
func main() { |
||||
array := []string{"hello", " ", "there", " ", "bye"} |
||||
arr := make([]string, N) |
||||
arr[0] = "a" |
||||
arr[2] = "b" |
||||
arr[4] = "c" |
||||
|
||||
for _, v := range arr { |
||||
fmt.Println(v) |
||||
} |
||||
|
||||
ptr := &array |
||||
fmt.Println(Compact(ptr, len(array))) |
||||
fmt.Println("Size after compacting:", piscine.Compact(&arr)) |
||||
|
||||
for _, v := range arr { |
||||
fmt.Println(v) |
||||
} |
||||
} |
||||
``` |
||||
|
||||
And its output : |
||||
Et son résultat : |
||||
|
||||
```console |
||||
student@ubuntu:~/piscine/test$ go build |
||||
student@ubuntu:~/piscine/test$ ./test |
||||
3 |
||||
a |
||||
|
||||
b |
||||
|
||||
c |
||||
|
||||
Size after compacting: 3 |
||||
a |
||||
b |
||||
c |
||||
student@ubuntu:~/piscine/test$ |
||||
``` |
||||
|
Loading…
Reference in new issue