Last active
August 16, 2017 18:14
-
-
Save Cleanse/d3873353ff561db8b1b370dbcca028dd to your computer and use it in GitHub Desktop.
Added reverse() method so round one could be used to seed a tourney.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
fmt.Println(schedule(7, []int{1, 2, 3, 4, 5, 6, 7, 8})) | |
} | |
func reverse(ss [][][]int) { | |
last := len(ss) - 1 | |
for i := 0; i < len(ss)/2; i++ { | |
ss[i], ss[last-i] = ss[last-i], ss[i] | |
} | |
} | |
func schedule(weeks int, teams []int) [][][]int { | |
//Check if team count is odd | |
if len(teams)%2 != 0 { | |
teams = append(teams, len(teams)+1) | |
} | |
halfTeams := len(teams) / 2 | |
mTeams := teams[1:] | |
count := len(mTeams) | |
sched := make([][][]int, weeks) | |
for i := 0; i < weeks; i++ { | |
teamIndex := i % count | |
sched[i] = make([][]int, halfTeams) | |
sched[i][0] = make([]int, 2) | |
sched[i][0][0] = teams[0] | |
sched[i][0][1] = mTeams[teamIndex] | |
for j := 1; j < halfTeams; j++ { | |
teamA := (i + j) % count | |
teamB := (i + count - j) % count | |
sched[i][j] = make([]int, 2) | |
sched[i][j][0] = mTeams[teamA] | |
sched[i][j][1] = mTeams[teamB] | |
} | |
} | |
reverse(sched) | |
return sched | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment