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