2008窗口脚本游戏-高级PowerShell事件7

Olympic_flame

In a few days the 2008 Scripting Games will come to an end. This is a yearly event that the Script Center does. There's a beginner and an advanced division and a bunch of deceptively hard problems. I was selected to be on of the "Guest Commentators (list here)" which really means they wanted me to solve one of the problems and provide the solution as an example. I'm not sure my solution is the best way, but it did solve the problem they assigned me.

再过几天, 2008脚本游戏将告一段落。 这是脚本中心每年进行一次的活动。 有一个初学者和一个高级部门,以及许多看似困难的难题。 我被选为“访客评论员(在此列出)”的意思,这实际上意味着他们要我解决其中一个问题并提供解决方案作为示例。 我不确定我的解决方案是最好的方法,但确实可以解决他们分配给我的问题。

My problem was Event 7: Play Ball! and I was to write a script that schedules all the games for a round-robin baseball tournament. The complete scenario is here, but in a nutshell:

我的问题是事件7:打球! 我当时写了一个脚本,为循环棒球比赛安排所有比赛。 完整的场景在这里,但总而言之:

"In a round-robin baseball tournament (or any kind of round-robin tournament, for that matter), every team plays each of the other teams in the tournament one time and one time only. For example, suppose we have a tournament with three teams (teams A, B, and C). In that case, the tournament would consist of the following set of games:

“在循环棒球锦标赛(或任何形式的循环锦标赛)中,每支球队仅一次又一次参加该锦标赛中的其他球队。例如,假设我们有一个三支球队(A,B和C队)。在这种情况下,比赛将由以下几组游戏组成:

  • A vs. B

    A对B

  • A vs. C

    A对C

  • B vs. C

    B对C

See how that works? Team A plays Team B and Team C; Team B plays Team A and Team C; and Team C plays Teams A and B."

看看如何运作? A队打B队和C队; 乙队扮演甲队和丙队; 而C队则参加A队和B队。”

A few other wrinkles thrown in are that the games must be randomized, otherwise Team A will play too many in a row and you need to schedule six teams, A through F. Of course, to be clear, every team must pay every other team once and only once. Here's my solution, hacked together quickly.

还有一些其他问题,那就是必须随机分配比赛,否则A队将连续打太多比赛,并且您需要安排A到F的六个队。当然,要明确,每个队都必须向其他队支付一次也只有一次。 这是我的解决方案,很快就被黑客入侵了。

#this only works with an even number of teams
cls
[array]$global:games = $nul
function rotateArray($a)
{
 $first, $rest = $a
 $a = $rest + $first
 return $a
}
function makeGames($a)
{
 $i = 0;
 while($i -lt $a.Length/2)
 {
  $global:games = $global:games + ($a[$i].ToString() + " vs. " + $a[$a.Length-1-$i].ToString())
  $i++
 }  
}
$a = "A","B","C","D","E","F"
$z = 0
while($z -lt $a.Length-1)
{
 makeGames($a)
 # hold on to the first one
 
 $first, $rest = $a
 #rotate the rest
 $rest = rotateArray($rest)
 $a = [array]$first + $rest
 $z++
}
#randomize games
$a = [collections.arraylist]$global:games
$r = $a.count..1 |% {$R = new-object random}{$R.next(0,$a.count) |%{$a[$_];$a.removeat($_)}}
$r

Doing this in PowerShell took my brain a while to get. Note the RotateArray method's use of multi-variable assignment to chop up he array into first and rest. That wasn't obvious to me as a C-guy for the last 15 years, but it made my solution simpler when I refactored and introduced it.

在PowerShell中执行此操作花了我一段时间。 请注意,RotateArray方法使用多变量赋值将数组分成第一个和其余部分。 在过去的15年中,作为一名C-guy,这对于我来说并不明显,但是当我重构和介绍它时,这使我的解决方案变得更加简单。

The solution (remember, it's randomized) will look something like this:

解决方案(请记住,它是随机的)如下所示:

B vs. D
B vs. C
A vs. D
B vs. F
C vs. D
A vs. F
A vs. B
C vs. F
E vs. F
A vs. E
D vs. F
B vs. E
D vs. E
A vs. C
C vs. E

Enjoy. Here's the same solution in Perl from Jan Dubois and again in VBScript. Who wants to do the F# version and the Ruby version? What about just LINQ to Objects?

请享用。 这是Jan Dubois的PerlVBScript相同解决方案谁想要做F#版本和Ruby版本? LINQ to Objects呢?

翻译自: https://www.hanselman.com/blog/2008-window-scripting-games-advanced-powershell-event-7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值