scripting games 2007 events 1 and 2
Advanced PowerShell Events 1 and 2 of the 2007 Scripting Games have ended and the answers are posted.
My answers were accepted, however, they were very different than the official answers given. This is just another great example about how There's More Than One Way To Do It in PowerShell.
Like Mow, here are the entries I submitted:
Event 1: Do as the Romans Do
$num = read-host "Please enter a Roman numeral"
$numerals = @{
I = 1;
V = 5;
X = 10;
L = 50;
C = 100;
D = 500;
M = 1000;
}
$total = 0
foreach ($i in $num.toCharArray()) {
$i = $i.tostring()
if ($prev) {
if ($numerals[$prev] -lt $numerals[$i]) {
$total -= $numerals[$prev]
$total += $numerals[$i] - $numerals[$prev]
} else {
$total += $numerals[$i]
}
} else {
$total += $numerals[$i]
}
$prev = $i
}
$total
Event 2: Three by Fours
foreach ($i in 1..148148) {
$answer = $i * 3
if ($answer.tostring() -notmatch "[^4]+") {
$i
break
}
}
I've submitted my answers for Events 3 and 4, but they haven't been marked yet. I'll post them once those events are over.
