winter scripting games: events 5 and 6

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 5 can be done rather easily in vbscript. I needed to convert a string into a hexadecimal array and then back into a string. I was able to make the first conversion, but couldn’t work out how to go backwards. I actually couldn’t get from hex to ASCII code, but I could easily get the rest of the way back to a real string of readable characters. Oh well.

$r = “It was the best of times…you know the rest.”
$a = $r.ToCharArray()
$h = @()
$v = @()

for ($i=0;$i -le $a.length;$i++){
$x = [int][char]$a[$i]
$h += “{0:X}” -f $x
}
$h

for ($i=0;$i -le $h.length;$i++){

# $y = [byte]$h[$i]
# $y = “{0:D}” -f $h[$i]
# $y = [Convert]::ToString($h[$i],16)
#this is the last part $y = [char][int]$h[$i]
$y

}

Beginner Event 6 just wanted some key words to be filled into an incomplete script found at the link above. I think my answers were correct…and if not, the program did run as expected anyway.

1. -eq
2. }
3. foreach
4. continue (although this can just be left blank too)
5. While
6. Switch

Advanced Event 5 wanted an Access database opened, then some math computations made, namely the min, max, mode, median, and mean values. Now, this can be very easy in other languages, but for some reason either PowerShell does not have these helpers built in yet, or I wasn’t able to find how to do it properly. Either way, here it is. If you really delve into my code, you can see that by the time I did the median, I was using better techniques than I had been using earlier. If I wanted to, I could rewrite the max and min sections much smaller now, I think.

$adOpenStatic = 3
$adLockOptimistic = 3
$objConnection = New-Object -comobject ADODB.Connection
$objRecordset = New-Object -comobject ADODB.Recordset
$objConnection.Open(“Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =
/scores.mdb”)
$objRecordset.Open(“Select * from Results”,
$objConnection,$adOpenStatic,$adLockOptimistic)

####### START MEAN #######
$objRecordset.MoveFirst()
$i,$avg = 0

do {
$avg += $objRecordset.Fields.Item(“Score”).Value
$i++;$objRecordset.MoveNext()}
until ($objRecordset.EOF -eq $True)

$avg = [math]::truncate($avg / $i)

####### START MIN #######
$objRecordset.MoveFirst()
$max = 0

do {
if ($objRecordset.Fields.Item(“Score”).Value -gt $max)
{ $max = $objRecordset.Fields.Item(“Score”).Value}
else { }
$objRecordset.MoveNext()}
until ($objRecordset.EOF -eq $True)

####### START MAX #######
$objRecordset.MoveFirst()
$min = $max

do {
if ($objRecordset.Fields.Item(“Score”).Value -lt $min)
{ $min = $objRecordset.Fields.Item(“Score”).Value}
else { }
$objRecordset.MoveNext()}
until ($objRecordset.EOF -eq $True)

####### START MODE #######
[int[]]$modearray = @()

for ($n=0;$n -le $max;$n++)
{$modearray += 0
}
$objRecordset.MoveFirst()

do {
$n = $objRecordset.Fields.Item(“Score”).Value
$modearray[$n] = $modearray[$n] + 1
$objRecordset.MoveNext()}
until ($objRecordset.EOF -eq $True)

$modemax = 0

for ($n=0;$n -le $modearray.length;$n++)
{
if ($modearray[$n] -gt $modemax)
{ $mode = $n; $modemax = $modearray[$n]}
else { }
}

####### START MEDIAN #######
[int[]]$medianarray = @()

for ($n=0;$n -lt $i;$n++)
{$medianarray += 0}

$n = 0
$objRecordset.MoveFirst()

do {
$medianarray[$n] = $objRecordset.Fields.Item(“Score”).Value
$n++;$objRecordset.MoveNext()}
until ($objRecordset.EOF -eq $True)

$medianarray = $medianarray | sort
$median = $medianarray[$medianarray.length/2]

####### START OUTPUT #######
Write-host “Mean: $avg”
Write-host “Mode: $mode”
Write-host “Median: $median”
Write-Host “Highest score: $max”
Write-Host “Lowest score: $min”

$objRecordset.Close()
$objConnection.Close()

Advanced Event 6 wanted a nicely formatted 75-column block of text. I really didn’t know what to do here.

One thought on “winter scripting games: events 5 and 6

  1. Just a comment to self, wow, I can’t believe how close I got to converting hex to characters in Beginner (?!) Event 5. All I really needed was to step back and somehow know to prepend every value with “0x” so that PowerShell would know it was a hex value. That way when I make them all integers by casting them as int, PowerShell would take it upon itself to convert from hex to ASCII. How I needed to know that, I don’t know. 🙂
    Given another day, I think I would have worked out Advanced Event 6 as it really was not as difficult as I think I made it out to be. I had a minor thought to read in the text delimited by spaces, but I was tired all weekend, heh. That’s my excuse, but now that I see what could have been done, I definitely learned a lot!

Comments are closed.