Ordenar por maximo en Go
This commit is contained in:
62
src/Go/002.go
Normal file
62
src/Go/002.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func maxIntSlice(lst []int) int {
|
||||
m := lst[0]
|
||||
for _, v := range lst[1:] {
|
||||
if v > m {
|
||||
m = v
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func order_by_max_i32(lst [][]int) [][]int {
|
||||
result := append([][]int(nil), lst...) // copy outer slice (like Python sorted)
|
||||
|
||||
sort.SliceStable(result, func(i int, j int) bool {
|
||||
return maxIntSlice(result[i]) < maxIntSlice(result[j])
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func maxRune(s string) rune {
|
||||
var m rune
|
||||
|
||||
for _, r := range s {
|
||||
if r > m {
|
||||
m = r
|
||||
}
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func order_by_max_str(lst []string) []string {
|
||||
result := append([]string(nil), lst...) // copy (like Python sorted)
|
||||
|
||||
sort.SliceStable(result, func(i int, j int) bool {
|
||||
return maxRune(result[i]) < maxRune(result[j])
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
check := [][]int{{3, 2}, {6, 7, 5}, {1, 4}}
|
||||
fmt.Println(order_by_max_i32(check)) // [[3,2],[1,4],[6,7,5]]
|
||||
|
||||
check = [][]int{{1}, {0, 1}}
|
||||
fmt.Println(order_by_max_i32(check)) // [[1],[0,1]]
|
||||
|
||||
check = [][]int{{0, 1}, {1}}
|
||||
fmt.Println(order_by_max_i32(check)) // [[0,1],[1]]
|
||||
|
||||
checkStr := []string{"este", "es", "el", "primero"}
|
||||
fmt.Println(order_by_max_str(checkStr)) // ["el","primero","es","este"]
|
||||
}
|
||||
Reference in New Issue
Block a user