push all exercices
This commit is contained in:
BIN
ch7/2/ch7_2.exe
Normal file
BIN
ch7/2/ch7_2.exe
Normal file
Binary file not shown.
52
ch7/2/ch7_2.go
Normal file
52
ch7/2/ch7_2.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type Team struct {
|
||||
Name string
|
||||
Players []string
|
||||
}
|
||||
|
||||
type League struct {
|
||||
name string
|
||||
Teams map[string]Team
|
||||
Wins map[string]int
|
||||
}
|
||||
|
||||
func (l *League) MatchResult(firstTeam string, firstScore int, secondTeam string, secondScore int) {
|
||||
if _, ok := l.Teams[firstTeam]; ok {
|
||||
return
|
||||
}
|
||||
if _, ok := l.Teams[secondTeam]; ok {
|
||||
return
|
||||
}
|
||||
if firstScore == secondScore {
|
||||
return
|
||||
}
|
||||
if firstScore > secondScore {
|
||||
l.Wins[firstTeam]++
|
||||
} else if secondScore > firstScore {
|
||||
l.Wins[secondTeam]++
|
||||
}
|
||||
}
|
||||
|
||||
func (l League) Ranking() []string {
|
||||
teamRanking := make([]string, 0, len(l.Wins))
|
||||
for winsK := range l.Wins {
|
||||
teamRanking = append(teamRanking, winsK)
|
||||
}
|
||||
sort.Slice(teamRanking, func(i, j int) bool {
|
||||
return l.Wins[teamRanking[i]] < l.Wins[teamRanking[j]]
|
||||
})
|
||||
return teamRanking
|
||||
}
|
||||
|
||||
func main() {
|
||||
league := League{"newLeague", map[string]Team{}, map[string]int{"first": 1, "second": 2, "third": 6, "fourth": 3, "fifth": 4}}
|
||||
league.MatchResult("third", 5, "second", 4)
|
||||
fmt.Println(league)
|
||||
fmt.Println(league.Ranking())
|
||||
}
|
||||
3
ch7/2/go.mod
Normal file
3
ch7/2/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module ch7_2
|
||||
|
||||
go 1.25.0
|
||||
Reference in New Issue
Block a user