
Windows PowerShell solution to Event 1 in the 2008 Winter Scripting Games.
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 = 6,5,6,6,"K"
for ($i = 0; $i -lt $arrPairs.length; $i++)
{
for ($j = $i + 1; $j -lt $arrPairs.length; $j++)
{
if ($arrPairs[$i] -eq $arrPairs[$j])
{
$total = $total + 1
}
}
}
"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, per to the event description:
$arrPairs = 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 this for loop:
for ($i = 0; $i -lt $arrPairs.length; $i++)
There are three parts to this type of for loop:
| • | $i = 0 This statement tells our loop where to start. In this case we’re starting at 0 because we want to start at the first element in the array, which has an index number of 0. |
| • | $i -lt $arrPairs.length This statement tells our loop when to stop. We want to stop when we reach the last element in the array. How do we find the last element in the array? That’s easy: the Length property of an array tells us how many elements are in the array. So we continue our loop as long as $i is less than the length of the array. |
| • | $i++ This statement sets the increment for our loop. We want to add 1 to i each time through the loop, and $i++ is PowerShell shorthand for “add 1 to $i”. |
Now comes the really tricky part. Within our for loop, we create another for loop:
for ($j = $i + 1; $j -lt $arrPairs.length; $j++)
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 continues while $j is less than the length of 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] -eq $arrPairs[$j])
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
And yes, we could have used the PowerShell shorthand here and incremented our counter like this:
$total++
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:
"Total: " + $total
Which, in this case, just happens to be 3:
Total: 3