mirror of https://github.com/01-edu/public.git
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.
121 lines
2.1 KiB
121 lines
2.1 KiB
5 years ago
|
package main
|
||
5 years ago
|
|
||
|
import (
|
||
|
"sort"
|
||
|
|
||
5 years ago
|
"./student"
|
||
5 years ago
|
)
|
||
|
|
||
5 years ago
|
func isSorted(f func(int, int) int, arr []int) bool {
|
||
|
ascendingOrdered := true
|
||
|
descendingOrdered := true
|
||
|
|
||
|
for i := 1; i < len(arr); i++ {
|
||
|
if !(f(arr[i-1], arr[i]) >= 0) {
|
||
|
ascendingOrdered = false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for i := 1; i < len(arr); i++ {
|
||
|
if !(f(arr[i-1], arr[i]) <= 0) {
|
||
|
descendingOrdered = false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return ascendingOrdered || descendingOrdered
|
||
|
}
|
||
|
|
||
5 years ago
|
func isSortedBy1(a, b int) int {
|
||
|
if a-b < 0 {
|
||
|
return -1
|
||
|
}
|
||
|
if a-b > 0 {
|
||
|
return 1
|
||
|
}
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
func isSortedBy10(a, b int) int {
|
||
|
if a-b < 0 {
|
||
|
return -10
|
||
|
}
|
||
|
if a-b > 0 {
|
||
|
return 10
|
||
|
}
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
func isSortedByDiff(a, b int) int {
|
||
|
return a - b
|
||
|
}
|
||
|
|
||
5 years ago
|
func main() {
|
||
5 years ago
|
functions := []func(int, int) int{isSortedByDiff, isSortedBy1, isSortedBy10}
|
||
5 years ago
|
|
||
|
type node struct {
|
||
5 years ago
|
f func(int, int) int
|
||
|
a []int
|
||
5 years ago
|
}
|
||
|
|
||
|
table := []node{}
|
||
|
|
||
5 years ago
|
// 5 unordered slices
|
||
5 years ago
|
for i := 0; i < 5; i++ {
|
||
5 years ago
|
function := functions[lib.RandIntBetween(0, len(functions)-1)]
|
||
5 years ago
|
val := node{
|
||
5 years ago
|
f: function,
|
||
5 years ago
|
a: lib.MultRandIntBetween(-1000000, 1000000),
|
||
5 years ago
|
}
|
||
|
table = append(table, val)
|
||
|
}
|
||
|
|
||
5 years ago
|
// 5 slices ordered in ascending order
|
||
5 years ago
|
for i := 0; i < 5; i++ {
|
||
5 years ago
|
function := functions[lib.RandIntBetween(0, len(functions)-1)]
|
||
|
ordered := lib.MultRandIntBetween(-1000000, 1000000)
|
||
5 years ago
|
sort.Ints(ordered)
|
||
5 years ago
|
|
||
|
val := node{
|
||
5 years ago
|
f: function,
|
||
5 years ago
|
a: ordered,
|
||
5 years ago
|
}
|
||
|
table = append(table, val)
|
||
|
}
|
||
|
|
||
5 years ago
|
// 5 slices ordered in descending order
|
||
5 years ago
|
for i := 0; i < 5; i++ {
|
||
5 years ago
|
function := functions[lib.RandIntBetween(0, len(functions)-1)]
|
||
|
reversed := lib.MultRandIntBetween(-1000000, 1000000)
|
||
5 years ago
|
sort.Sort(sort.Reverse(sort.IntSlice(reversed)))
|
||
5 years ago
|
val := node{
|
||
5 years ago
|
f: function,
|
||
5 years ago
|
a: reversed,
|
||
5 years ago
|
}
|
||
|
table = append(table, val)
|
||
|
}
|
||
|
|
||
|
table = append(table, node{
|
||
5 years ago
|
f: isSortedByDiff,
|
||
5 years ago
|
a: []int{1, 2, 3, 4, 5, 6},
|
||
5 years ago
|
})
|
||
|
|
||
|
table = append(table, node{
|
||
5 years ago
|
f: isSortedByDiff,
|
||
5 years ago
|
a: []int{6, 5, 4, 3, 2, 1},
|
||
5 years ago
|
})
|
||
|
|
||
|
table = append(table, node{
|
||
5 years ago
|
f: isSortedByDiff,
|
||
5 years ago
|
a: []int{0, 0, 0, 0, 0, 0, 0},
|
||
5 years ago
|
})
|
||
|
|
||
|
table = append(table, node{
|
||
5 years ago
|
f: isSortedByDiff,
|
||
5 years ago
|
a: []int{0},
|
||
5 years ago
|
})
|
||
|
|
||
|
for _, arg := range table {
|
||
5 years ago
|
lib.Challenge("IsSorted", student.IsSorted, isSorted, arg.f, arg.a)
|
||
5 years ago
|
}
|
||
|
}
|