Browse Source

fix of compact

content-update
Christopher Fremond 5 years ago
parent
commit
79e6a7709b
  1. 48
      subjects/compact.en.md
  2. 44
      subjects/compact.fr.md

48
subjects/compact.en.md

@ -1,15 +1,18 @@
## compact
## Compact
### Instructions
Write a function `Compact` that takes a pointer to an array as parameter and overwrites the elements that points to `nil`.
Write a function `Compact` that takes a pointer to a slice of strings as the argument.
This function must:
- Hint : This fonction exists in Ruby.
- Return the number of elements with non-`nil`.
### Expected function
- 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$
```

44
subjects/compact.fr.md

@ -2,14 +2,17 @@
### Instructions
Écrire une fonction `Compact` qui prend un pointeur sur tableau comme paramètre et qui réécris sur les éléments qui pointent sur `nil`.
Écrire une fonction `Compact` qui prend un pointeur sur slice de `strings` comme paramètre.
Cette fonction doit:
- Indice : Cette fonction existe in Ruby.
- Retourner le nombre d'éléments avec des valeurs non-`nil`
- 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 {
}
```
@ -21,13 +24,29 @@ 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)
}
}
```
@ -36,6 +55,15 @@ 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…
Cancel
Save