Ordenar por maximo en Go

This commit is contained in:
2026-05-09 17:03:18 +02:00
parent 1ab98c2047
commit 0c9bcca1ae

62
src/Go/002.go Normal file
View 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"]
}