
VBScript solution to Event 1 in the 2008 Winter Scripting Games.
Solutions are also available for Windows PowerShell and Perl.
To solve this first event of the 2008 Scripting Games you needed to know (or learn) a little bit about arrays. The first thing to note in this event is that we were looking for pairs, meaning the suit (heart, space, diamond, or club) was irrelevant. With that in mind, here’s the script we came up with:
total = 0
arrPairs = Array(6, 5, 6, 6, "K")
For i = 0 to UBound(arrPairs)
For j = (i + 1) to UBound(arrPairs)
If arrPairs(i) = arrPairs(j) Then
total = total + 1
End If
Next
Next
Wscript.Echo "Total: " & total
We need to keep track of the total number of pairs, so we started the script by setting a counter (which we named total) to 0. We then created an array that held the cards we were dealt, as per the event description:
arrPairs = Array(6, 5, 6, 6, "K")
Again, because we’re looking only for matching pairs, the suit doesn’t matter; therefore, we didn’t bother keeping track of that. (Yes, that was sneaky of us to try to confuse you with irrelevant information, wasn’t it?) The array consists of the five cards: three 6s, a 5, and a king.
Now we need to loop through the cards counting pairs. We start with a for loop that loops from 0, the first element in the array, to UBound(arrPairs):
For i = 0 to UBound(arrPairs)
Why UBound(arrPairs)? Well, the UBound function gives us the index number of the last element in the array, in this case element 4 (remember, we start counting at 0). That means we’re going to loop from 0, the first element in the array, to 4, the last element.
Now comes the really tricky part. Within our for loop, we create another for loop:
For j = (i + 1) to UBound(arrPairs) - 1
This is called a nested for loop: we have a for loop nested within another for loop. In order to compare the elements in the array, we need to compare every element to every other element. Let’s walk through this. The first time through the outer for loop, i is equal to 0, the first element in the array. We need to compare that element to array elements 1 through 4. We do that by starting another for loop to loop through those elements. As you can see, our inner for loop starts at i + 1, or element 1. This array also goes through UBound(arrPairs), the last element in the array. So the first time through, we have index numbers 0 (i) and 1 (j).
Now that we have the index numbers for the first two elements in the array, we compare them to see if they match:
If arrPairs(i) = arrPairs(j) Then
If they don’t match we simply ignore them and continue on. If they do match, we increment our counter by 1 because we found a pair:
total = total + 1
At this point we’re still in the inner for loop. We loop around again, and this time j will be equal to 2, the third element in the array. We’ll compare element 2 to element 0 and if they match we’ll increment our counter. This continues until j is equal to 4, the last element.
At this point we’ve completed the inner loop. So now we return to the outer loop, where i gets incremented to element 1. We now start the inner loop over again, starting at element i + 1 (2), and compare the second element to the third element.
And so on.
By the time we’re done, we’ll have compared every array element to every other array element, and we’ll echo back the count of the number of matches we found:
Wscript.Echo "Total: " & total
Which, in this case, just happens to be 3:
Total: 3