Created
July 11, 2012 19:56
-
-
Save bkyrlach/3092868 to your computer and use it in GitHub Desktop.
Bad loop
This file contains hidden or 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
| 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) | |
| } | |
| } | |
| } |
This file contains hidden or 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
| 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