forked from root/public
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.
52 lines
984 B
52 lines
984 B
5 years ago
|
## issorted
|
||
5 years ago
|
|
||
5 years ago
|
### Instructions
|
||
5 years ago
|
|
||
|
Write a function `IsSorted` that returns `true` if the slice of `int` is sorted, and that returns `false` otherwise.
|
||
|
|
||
![]()
4 years ago
|
The function passed in parameter returns a positive `int` if `a` (the first argument) is superior to `b` (the second argument), it returns `0` if they are equal and it returns a negative `int` otherwise.
|
||
5 years ago
|
|
||
|
To do your testing you have to write your own `f` function.
|
||
|
|
||
5 years ago
|
### Expected function
|
||
5 years ago
|
|
||
|
```go
|
||
![]()
4 years ago
|
func IsSorted(f func(a, b int) int, a []int) bool {
|
||
![]()
5 years ago
|
|
||
5 years ago
|
}
|
||
|
```
|
||
|
|
||
5 years ago
|
### Usage
|
||
5 years ago
|
|
||
5 years ago
|
Here is a possible program to test your function (without `f`):
|
||
5 years ago
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
piscine ".."
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
![]()
4 years ago
|
a1 := []int{0, 1, 2, 3, 4, 5}
|
||
|
a2 := []int{0, 2, 1, 3}
|
||
5 years ago
|
|
||
![]()
4 years ago
|
result1 := piscine.IsSorted(f, a1)
|
||
|
result2 := piscine.IsSorted(f, a2)
|
||
5 years ago
|
|
||
|
fmt.Println(result1)
|
||
|
fmt.Println(result2)
|
||
|
}
|
||
|
```
|
||
|
|
||
|
And its output :
|
||
|
|
||
|
```console
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$ go build
|
||
|
student@ubuntu:~/[[ROOT]]/test$ ./test
|
||
5 years ago
|
true
|
||
|
false
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$
|
||
5 years ago
|
```
|