7.16 練習問題
code:go
package main
import (
"cmp"
"io"
"log"
"maps"
"os"
"slices"
)
type Team struct {
Name string
MemberNames []string
}
type League struct {
Teams []Team
}
type Ranker interface {
Ranking() []string
}
func (l *League) MatchResult(team1 string, score1 int, team2 string, score2 int) {
if score1 > score2 {
} else if score2 > score1 {
}
}
func (l *League) Ranking() []string {
sortedNames := slices.SortedFunc(maps.Keys(l.Wins), func(a, b string) int {
return cmp.Compare(l.Winsb, l.Winsa) })
return sortedNames
}
func RankPrinter(r Ranker, w io.Writer) {
for _, name := range r.Ranking() {
if _, err := io.WriteString(w, name+"\n"); err != nil {
log.Fatal(err)
}
}
}
func main() {
t1 := Team{Name: "A", MemberNames: []string{"a", "b", "c"}}
t2 := Team{Name: "B", MemberNames: []string{"d", "e", "f"}}
l := League{
Teams: []Team{t1, t2},
}
l.MatchResult("A", 3, "B", 1)
l.MatchResult("A", 2, "B", 0)
l.MatchResult("B", 4, "A", 1)
RankPrinter(&l, os.Stdout)
}