|
|
|
## countif
|
|
|
|
|
|
|
|
### Instructions
|
|
|
|
|
Refactor & Beautify & destruction commit
return early, remove else branches, reorder conditions and top-level functions, remove empty lines, remove unnecessary append(), fix typos, stop using testing package, remove dead code, fix mistakes in subjects, tests and solutions, remove disclaimers, reformat comments, simplify solutions, tests, add more instructions to subjects, remove obsolete files, etc.
Some of the reasons behind those modifications will be added to good-practices.en.md
Some of the exercises are now broken, they will have to be fixed, most of them have a "TODO:" comment.
5 years ago
|
|
|
Write a function `CountIf` that returns the number of elements of a `string` slice for which the `f` function returns `true`.
|
|
|
|
|
|
|
|
### Expected function
|
|
|
|
|
|
|
|
```go
|
|
|
|
func CountIf(f func(string) bool, tab []string) int {
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Usage
|
|
|
|
|
|
|
|
Here is a possible program to test your function :
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"piscine"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
tab1 := []string{"Hello", "how", "are", "you"}
|
|
|
|
tab2 := []string{"This","1", "is", "4", "you"}
|
|
|
|
answer1 := piscine.CountIf(piscine.IsNumeric, tab1)
|
|
|
|
answer2 := piscine.CountIf(piscine.IsNumeric, tab2)
|
|
|
|
fmt.Println(answer1)
|
|
|
|
fmt.Println(answer2)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
And its output :
|
|
|
|
|
|
|
|
```console
|
|
|
|
$ go run .
|
|
|
|
0
|
|
|
|
2
|
|
|
|
$
|
|
|
|
```
|