diff --git a/subjects/compact.en.md b/subjects/compact.en.md index 4f35ffe7..5c637f85 100644 --- a/subjects/compact.en.md +++ b/subjects/compact.en.md @@ -2,14 +2,17 @@ ### Instructions -Write a function that will take a pointer to a array as parameter and overwrites any element that points to `nil`. +Write a function `Compact` that takes a pointer to a slice of strings as the argument. +This function must: -- If you not sure what the function does. It exists in Ruby. +- Return the number of elements with non-`nil`. + +- Compact, i.e., delete the elements with `nil` in the slice. ### Expected functions ```go -func Compact(ptr *[]string, length int) int { +func Compact(ptr *[]string) int { } ``` @@ -21,13 +24,29 @@ Here is a possible [program](TODO-LINK) to test your function : ```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) + } } ``` @@ -36,6 +55,15 @@ And its output : ```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$ ``` diff --git a/subjects/compact.fr.md b/subjects/compact.fr.md index 4f35ffe7..aaeb5b8e 100644 --- a/subjects/compact.fr.md +++ b/subjects/compact.fr.md @@ -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$ ```