winter scripting games: events 3 and 4

This post is just about my time with PowerShell for the 2007 Winter Scripting Games. If you have no interest, I certainly won’t be upset if you skip this post. I’m posting only because this will mark my first exposure to PowerShell.

Beginner Event 3 was a pretty easy exercise in picking out what item in a series is not like the others. I’m sure this can be done in less code, but this made the quickest sense to me with the least effort.

$a1 = “monday”, “MONDAY”, “monday”
$a2 = “TUESDAY”, “tuesday”, “tuesday”
$a3 = “WEDNESDAY”, “wednesday”, “wednesday”
$a4 = “thursday”, “thursday”, “THURSDAY”
$a5 = “friday”, “FRIDAY”, “friday”

$x1a = [String]::Compare($a1[0],$a1[1],$False)
$x1b = [String]::Compare($a1[1],$a1[2],$False)
$x1c = [String]::Compare($a1[0],$a1[2],$False)
if ($x1a -eq 0){“a1: third”}
elseif ($x2b -eq 0){“a1: first”}
else{“a1: second”}

$x2a = [String]::Compare($a2[0],$a2[1],$False)
$x2b = [String]::Compare($a2[1],$a2[2],$False)
$x2c = [String]::Compare($a2[0],$a2[2],$False)
if ($x2a -eq 0){“a2: third”}
elseif ($x2b -eq 0){“a2: first”}
else{“a2: second”}

$x3a = [String]::Compare($a3[0],$a3[1],$False)
$x3b = [String]::Compare($a3[1],$a3[2],$False)
$x3c = [String]::Compare($a3[0],$a3[2],$False)
if ($x3a -eq 0){“a3: third”}
elseif ($x3b -eq 0){“a3: first”}
else{“a3: second”}

$x4a = [String]::Compare($a4[0],$a4[1],$False)
$x4b = [String]::Compare($a4[1],$a4[2],$False)
$x4c = [String]::Compare($a4[0],$a4[2],$False)
if ($x4a -eq 0){“a4: third”}
elseif ($x4b -eq 0){“a4: first”}
else{“a4: second”}

$x5a = [String]::Compare($a5[0],$a5[1],$False)
$x5b = [String]::Compare($a5[1],$a5[2],$False)
$x5c = [String]::Compare($a5[0],$a5[2],$False)
if ($x5a -eq 0){“a5: third”}
elseif ($x5b -eq 0){“a5: first”}
else{“a5: second”}

Beginner Event 4 just wanted a nicely formatted list of running services.

get-service | where-object {$_.status-eq “running”} | format-table
-property DisplayName, Status -auto

Advanced Event 3 involved a program to make change in various demoninations. Not too bad, and I was pretty happy with my initial formating of the input.

$a = Read-Host “Enter your dollars”
$a = $a -replace(“\$”,””)
$a = $a -replace(“\.”,””)
$a = 5000 – $a
$change = $a / 100
$change = “{0:N2}” -f $change

$tens = $a / 1000
$tens = [math]::truncate($tens)
$a = $a – $tens * 1000
$fives = $a / 500
$fives = [math]::truncate($fives)
$a = $a – $fives * 500
$ones = $a / 100
$ones = [math]::truncate($ones)
$a = $a – $ones * 100
$quarters = $a / 25
$quarters = [math]::truncate($quarters)
$a = $a – $quarters * 25
$dimes = $a / 10
$dimes = [math]::truncate($dimes)
$a = $a – $dimes * 10
$nickels = $a / 5
$nickels = [math]::truncate($nickels)
$a = $a – $nickels * 5
$pennies = $a / 1
$pennies = [math]::truncate($pennies)

Write-Host “Change returned: $change”
Write-Host “Tens: $tens”
Write-Host “Fives: $fives”
Write-Host “Ones: $ones”
Write-Host “Quarters: $quarters”
Write-Host “Dimes: $dimes”
Write-Host “Nickels: $nickels”
Write-Host “Pennies: $pennies”

Advanced Event 4 was also fairly easy and fun in attempting to map out the various chinese new year animals. I did it a slightly harder way than they gave in their answer.

$a = Read-Host “Enter your year”
$a = $a -1900
$b = $a / 12
$b = [math]::truncate($b)
$b = $b * 12
$a = $a – $b

switch($a)
{
“0”{$answer=”Rat”}
“1”{$answer=”Ox”}
“2”{$answer=”Tiger”}
“3”{$answer=”Rabbit”}
“4”{$answer=”Dragon”}
“5”{$answer=”Snake”}
“6”{$answer=”Horse”}
“7”{$answer=”Goat”}
“8”{$answer=”Monkey”}
“9”{$answer=”Rooster”}
“10”{$answer=”Dog”}
“11”{$answer=”Pig”}
}
Write-Host $answer