
Solution to Event 7 in the 2008 Winter Scripting Games Sudden Death Challenge.
If you’ve never had the opportunity to play with SAPI (Speech Application Programming Interface) or Microsoft Agent we hope you were able to figure out how to complete Event 7. We also hope you had a little fun in doing so; it’s always cool to have your scripts talk to you. (Usually the Scripting Guys talk to their scripts, although not always in a particularly pleasant voice.) At any rate, we enjoyed this event, which means you might be seeing more talking script stuff in the future.
Note. Does that mean that you might be seeing more talking script stuff in future Scripting Games? Well, maybe. But the Scripting Guys also have a couple other tricks up their sleeves. But that’s another story for another day. |
Other than getting the script to talk this wasn’t a particularly difficult event; you just had to make sure you followed the directions. If you did, and if you used VBScript (though this event could also have been solved using Perl or Windows PowerShell) you likely came up with a script similar to this one:
x = 100
x = x * 2
x = x / 30
x = Int(x)
x = x^5
x = x * 4
x = Sqr(x)
x = x / 45
x = FormatNumber(x, 2)
Set objVoice = CreateObject("SAPI.SpVoice")
objVoice.Speak x
As you’ll see in a few minutes, this script is actually a bit longer than it needs to be; however, we thought we’d show you the elongated version first; that should help you get a better understanding of what we’re doing, and why.
To begin with, we assign the value 100 to a variable named x. Once that assignment has been made we multiply x by 2, which makes x equal to 200. We then divide x by 30; once we’ve done that x will be equal to 6.66666666666667.
And yes, that is a crazy number; maybe that’s why we decided to strip all the decimal places off the number and leave us with just the integer portion: 6. To do that, we simply call the Int function, like so:
x = Int(x)
After we’ve done that, x will be equal to 6.
So are we done yet? No, not even close. Next we need to raise x to the fifth power; in VBScript, the exponentiation operator is the ^ (caret) symbol. At that point x will be equal to 7776, but not for long; that’s because we immediately multiply this value by 4, leaving x equal to 31104.
Fun, huh?
Next we use the Sqr function to calculate the square root of 31104; that’s going to yield 176.363261480389. (Coincidentally enough, that’s Scripting Guy Jean Ross’ lucky number.) Finally, we divide that value by 45, giving us an almost-but-not-quite final answer of this:
3.91918358845308
In order to get our final, official answer we need to round this value to 2 decimal places. That’s what this line of code, and the FormatNumber function, does for us:
x = FormatNumber(x, 2)
At this point we would usually echo back the value of x (3.92). However, we decided that echoing back the value of x was too boring; we wanted the script to tell us the value of x. That’s why we added the following two lines of code:
Set objVoice = CreateObject("SAPI.SpVoice")
objVoice.Speak x
In line 1 we’re creating an instance of the SAPI.SpVoice object, a Microsoft COM object that can turn written text into speech. In line 2 we simply ask this object to report back the value of x. If all goes well, we should hear a somewhat robotic-sounding voice recite the following:
Three point nine two
Cool!
As we hinted at earlier, by the judicious use of parentheses we could have carried out this operation using far fewer lines of code. The following script is a bit difficult to decipher, but this one also returns the value 3.92:
x = FormatNumber(((Sqr(((Int((100 * 2) / 30)^5) * 4))) / 45), 2)
Set objVoice = CreateObject("SAPI.SpVoice")
objVoice.Speak x
To figure out how this script (or at least the first line in the script) works you need to start by finding the innermost set of parentheses; in that case, that would be this:
(100 * 2)
The way VBScript – and other scripting/programming languages – tend to work is to start by carrying out the task found in the innermost set of parentheses. In this script, that means multiplying 100 by 2. The script then takes that value – 200 – and looks for the next set of parentheses. Substituting 200 for 100 * 2 that would be this:
(200 / 30)
From there the script takes that value – 6.66666666666667 – and then looks for the next set of parentheses. We’ll let you figure out the rest for yourself.