Skip to content

Instantly share code, notes, and snippets.

@bkyrlach
Created July 11, 2012 19:56
Show Gist options
  • Select an option

  • Save bkyrlach/3092868 to your computer and use it in GitHub Desktop.

Select an option

Save bkyrlach/3092868 to your computer and use it in GitHub Desktop.
Bad loop
def cyclic(players: List[Player], teams: List[Team], pick: Int = 1): List[Team] = {
if(players.isEmpty) {
teams
} else {
val team = teams.head
val player = choosePlayer(players)
team.copy(players = team.players + player)
val movedLast = teams.tail :+ team
if(pick % numberOfTeams == 0) {
cyclic(players.filterNot(_ == player), movedLast.reverse, pick + 1)
} else {
cyclic(players.filterNot(_ == player), movedLast, pick + 1)
}
}
}
val r = new java.util.Random
def shuffle[A](l: List[A]): List[A] = {
if(l.isEmpty) {
Nil
} else {
val x = r.nextInt(l.size)
val item = l(x)
if(x == 0) {
item :: shuffle(l.tail)
} else if (x == l.size - 1) {
item :: shuffle(l.reverse.tail)
} else {
item :: shuffle(l.take(x - 1) ++ l.drop(x))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment