Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Scripting Games 2008 Advanced Windows PowerShell Event 7

My solution for Event 7

 

[Collections.ArrayList]$Teams = "A,B,C,D,E,F" -split ','
$games = 1..($teams.count - 1) |% {foreach ($team in $teams[0]) {$teams |? {$_ -ne $team} |% {$team + " vs. " + $_};$teams.remove($team)}}
$r = new-object random
($Games | % { $in = $_; 1 | select @{n='Game';e={$in}},@{n='val';e={$r.Next()}} } | sort val) | select Game

I casted the teams to an arraylist  so that I could use the remove() method later on to remove a team after all his matches are scheduled in the next line that creates the teamlist

 

PoSH> [Collections.ArrayList]$Teams = "A,B,C,D,E,F" -split ','                                                          
PoSH> $games = 1..($teams.count - 1) |% {foreach ($team in $teams[0]) {$teams |? {$_ -ne $team} |% {$team + " vs. " + $_
};$teams.remove($team)}}                                                                                                
PoSH> $games                                                                                                            
A vs. B                                                                                                                 
A vs. C                                                                                                                 
A vs. D                                                                                                                 
A vs. E                                                                                                                 
A vs. F                                                                                                                 
B vs. C                                                                                                                 
B vs. D                                                                                                                 
B vs. E                                                                                                                 
B vs. F                                                                                                                 
C vs. D                                                                                                                 
C vs. E                                                                                                                 
C vs. F                                                                                                                 
D vs. E                                                                                                                 
D vs. F                                                                                                                 
E vs. F                                                                                                                 
PoSH> $r = new-object random                                                                                            
PoSH> ($Games | % { $in = $_; 1 | select @{n='Game';e={$in}},@{n='val';e={$r.Next()}} } | sort val) | select Game       
                                                                                                                        
Game                                                                                                                    
----                                                                                                                    
E vs. F                                                                                                                 
B vs. F                                                                                                                 
A vs. B                                                                                                                 
D vs. F                                                                                                                 
D vs. E                                                                                                                 
B vs. E                                                                                                                 
B vs. C                                                                                                                 
A vs. E                                                                                                                 
C vs. D                                                                                                                 
A vs. F                                                                                                                 
C vs. F                                                                                                                 
B vs. D                                                                                                                 
A vs. D                                                                                                                 
C vs. E                                                                                                                 
A vs. C                                                                                                                 
                                                                                                                        
                                                                                                                        
PoSH>                                                                                      

The next interesting line is line 4 that uses the random object from line 3 to randomize the list, I had that around from Scripting Games 2007 Advanced PowerShell Event 10 

and I got this from Lee Holmes in a comment on this post on my old blog : PowerShell : How Do I randomize a list, and remove some elements ? , you can see (especially if your still working on the solution for Event 10: Blackjack! ), this is a handy function to have around ;-)

Enjoy,

Greetings /\/\o\/\/

Published Thursday, February 28, 2008 2:37 PM by MoW

Comments

# re: Scripting Games 2008 Advanced Windows PowerShell Event 7

What about:

$games | sort {$r.Next()}

?

Thursday, February 28, 2008 4:33 PM by Bruno Gomes

# re: Scripting Games 2008 Advanced Windows PowerShell Event 7

@ bruno

Cool solution !

completly overlooked that possibility

Greetings /\/\o\/\/

Thursday, February 28, 2008 7:17 PM by MoW

# PowerShell out-random function

Bruno left a comment on the post about my solution for Scripting Games 2008 Advanced Windows PowerShell

Thursday, February 28, 2008 7:40 PM by The PowerShell Guy

# Scripting Games: Advanced PowerShell - Event 7 « PowerShell ?? ???????????? ??????????????

Anonymous comments are disabled