scripting games 2007 events 3 4 5 and 6
Event 3: Time For a Change
I had to write this type of program way back in High School Programming I. It was an exercise in using the modulo operator. It surprised me that none of the answers I've seen went with that solution.
[decimal]$amount = read-host "Enter Amount"
$amount = 50 - $amount
""
"Change returned: $amount"
# 10
$tens = [int]($amount / 10)
$amount = $amount % 10
"Tens: $tens"
# 5
$fives = [int]($amount / 5)
$amount = $amount % 5
"Fives: $fives"
# 1
$ones = [int]($amount / 1)
$amount = $amount % 1
"Ones: $ones"
# .25
$quarters = [int]($amount / .25)
$amount = $amount % .25
"Quarters: $quarters"
# .10
$dimes = [int]($amount / .1)
$amount = $amount % .1
"Dimes: $dimes"
# .05
$nickels = [int]($amount / .05)
$amount = $amount % .05
"Nickels: $nickels"
# .01
$pennies = [int]($amount / .01)
$amount = $amount % .01
"Pennies: $pennies"
""
Event 4: Happy Chinese New Year
I was using the Wikipedia entry as reference. It messed me up a lot due to the double column, but that was about the only trouble I had with this one.
$date = read-host "Enter a year between 1900 and 2100"
$animals = @{
1 = "Rat";
2 = "Ox";
3 = "Tiger";
4 = "Rabbit";
5 = "Dragon";
6 = "Snake";
7 = "Horse";
8 = "Sheep";
9 = "Monkey";
10 = "Rooster";
11 = "Dog";
12 = "Pig";
}
$date = $($date - 1900) + 1
$date %= 12
if ($date -eq 0) {
$animals[12]
} else {
$animals[$date]
}
Event 5: When I Say Mean, I Mean Average
This one was a little more challenging with the ADODB support and a little math.
One of the biggest problems I ran into was actually opening the mdb file. The rules say that the file had to be in the same directory as the script. Simply specifying "scores.mdb" did not work for me. I recently read that this has to do with how PowerShell and .NET interpret the current directory. Basically, PowerShell thought I meant c:\Documents & Settings\joe\scores.mdb when I really wanted c:\scripts\scores.mdb. My solution is in the code.
I think my solution is right... (Note, I'm typing this the night before the answers are released but not publishing until afterwards.)
$adOpenStatic =3
$adLockOptimistic = 3
$scores = @()
$mode = @{}
$dbFile = get-childitem scores.mdb
$objConnection = new -com ADODB.Connection
$objRecordset = new -com ADODB.Recordset
$objConnection.Open("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = $dbFile")
$objRecordset.Open("select * from Results", $objConnection, $adOpenStatic, $adLockOptimistic)
$objRecordset.MoveFirst()
while ($objRecordset.EOF -ne $True) {
$score = $objRecordset.Fields.Item("Score").Value
$scores += $score
$total += $score
$mode[$score]++
$objRecordset.MoveNext()
}
""
# get the average
$average = $total / $scores.length
"Mean: " + [int]$average
# get the mode
$highest = 0
foreach ($x in $mode.keys) {
if ($mode[$highest] -lt $mode[$x]) {
$highest = $x
}
}
"Mode: " + $highest
# get the median
$sorted = $scores | sort
[int]$median = $scores.length / 2
"Median: " + $sorted[$median - 1]
# get the highest score
"Highest: " + $sorted[-1]
# get the lowest score
"Lowest: " + $sorted[0]
""
$objRecordset.Close()
$objConnection.Close()
Event 6: Give Me a (75-column) Break
This one snagged me for a while, but once I started going in small steps, I eventually figured out the answer.
I'm still confused over the alice2.txt file that came with the Competitors Pack. Is this supposed to be the solution? The rules say that there can be no more than 75 characters on a line. The alice2.txt file has some lines where more words could have fit on a line, but, instead, they were placed on the next line. I quadruple checked my answer, saw that it followed each of the Event's rules, and turned it in.
$contents = get-content "alice.txt"
$line = ""
$i = 1
for ($x = 0; $x -lt $contents.length; $x++) {
$c = $contents[$x]
if ($i -eq 75) {
if ($c -ne " ") {
$remove = 0
while ($contents[$x] -ne " ") {
$x--
$remove++
}
$line = $line.remove($x, $remove)
}
$line += "`n"
$i = 0
} else {
$line += $c
}
$i++
}
""
$line
""
