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

Scripting Games 2008 Advanced Windows PowerShell Event 3

My Answer for Scripting Games 2008 Advanced Windows PowerShell Event 3 :

$votes = gc c:\scripts\votes.txt |% {,($_.Split(','))}
While ($true) {
  $round = ($votes |% {$_[0]} | group | sort -desc count)
  #$round ; "------"
  if ($round[0].count -gt 600) {"The winner is {0} with {1:n2} % of the vote" -f $round[0].name,($round[0].count / 12);return}
  foreach ($i in 0..($votes.count -1)) {
    $votes[$i] = $votes[$i] |? {$_ -ne $round[-1].name}
  }
}

 

Result, *Update* updated script to completly remove candidate, to avoid confusion ;-) 

PoSH> c:\scripts\event3.ps1                                                                                             
The winner is Pilar Ackerman with 50.17 % of the vote                                                                   
PoSH>                                                                                      

Can see I commented out a line, un comment it to show the intermediate  results :

PoSH> c:\scripts\event3.ps1                                                                                             
                                                                                                                        
Count Name                      Group                                                                                   
----- ----                      -----                                                                                   
  402 Ken Myer                  {Ken Myer, Ken Myer, Ken Myer, Ken Myer...}                                             
  380 Pilar Ackerman            {Pilar Ackerman, Pilar Ackerman, Pilar Ackerman, Pilar Ackerman...}                     
  250 Syed Abbas                {Syed Abbas, Syed Abbas, Syed Abbas, Syed Abbas...}                                     
  168 Jonathan Haas             {Jonathan Haas, Jonathan Haas, Jonathan Haas, Jonathan Haas...}                         
------                                                                                                                  
  475 Pilar Ackerman            {Pilar Ackerman, Pilar Ackerman, Pilar Ackerman, Pilar Ackerman...}                     
  458 Ken Myer                  {Ken Myer, Ken Myer, Ken Myer, Ken Myer...}                                             
  267 Syed Abbas                {Syed Abbas, Syed Abbas, Syed Abbas, Syed Abbas...}                                     
------                                                                                                                  
  602 Pilar Ackerman            {Pilar Ackerman, Pilar Ackerman, Pilar Ackerman, Pilar Ackerman...}                     
  567 Ken Myer                  {Ken Myer, Ken Myer, Ken Myer, Ken Myer...}                                                                     
------                                                                                                                  
The winner is Pilar Ackerman with 50.17 % of the vote                                                                   
                                                                                                                        
                                                                                                                        
PoSH>                                                                                    
Published Saturday, February 23, 2008 2:36 PM by MoW

Comments

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

Anonymous comments are disabled