OK, short version: something about invoke-command is passing only the first element of the array into the script block. Not sure why, but the & operator does work. Here's my script block:
$isSorted = {
param ([Int32[]]$a)
#$a
for ($i = 0; $i -lt ($a.length - 2); $i++)
{
if ($a[$i] -gt $a[$i+1])
{
return (1 -eq 0)
}
}
return (1 -eq 1)
}
Since I can't develop without tests, here's my test cases:
$sorted01 = 1,2,3,4,5 #Expected True
$sorted01 = 1,1,1,1,1 #Expected True
$unsorted01 = 1,2,3,4,3 #Expected False
$unsorted02 = 3,2,3,4,5 #Expected False
$unsorted03 = 3,2,3,4,3 #Expected False
I can put these together in an array and use a foreach loop to run all the tests:
$trueTestCases = $sorted01,$sorted02
$falseTestCases = $unsorted01,$unsorted02,$unsorted03
foreach ($a in $trueTestCases)
{
if (!(&$isSorted $a)) {"FAIL: List is sorted: $a"}
}
foreach ($a in $falseTestCases)
{
if (&$isSorted $a) {"FAIL: List is not sorted: $a"}
}
Result:
FAIL: List is not sorted: 1 2 3 4 3
Um, that's bad. The only one that fails is the case where the last two digits are unordered. My for-loop isn't working right.
I don't know where I got the idea that it needed to be ($a.length – 2), but ($a.length – 1) is what it should be.
This script block takes arbitrarily large arrays, but what about empty arrays or single elements?
PowerShell: &$isSorted $aa
True
PowerShell: &$isSorted 1,2
True
PowerShell: &$isSorted 2,1
False
That works – and I don't know why.
If the length of the array is 0 or 1, the for-loop is skipped and the value defaults to true.
An array with length 2 goes through the array once, checking just once.
This is awfully clever. At this point, I'd like to pretend I designed it that way intentionally.
Some cleaning up:
I wrote help for this. As before, it doesn't display as written in a script block. I'm not sure whether I'm doing it wrong or whether it isn't supposed to work. I can, though, turn it into a function and get a display of it that way:
PowerShell: get-help x
NAME
x
SYNOPSIS
Checks wether an array is sorted, such that the second of two elements is greater than
or equal to the first.
SYNTAX
x [[-candidate] (LT)Object(GT)] [(LT)commonparameters(gt)]< p="">
DESCRIPTION
Takes an array of any length and checks for being sorted. An array with 0 or 1 members is
sorted by definition.
The function tests each pair in sequence from left to right (0 to highest index) and returns
$false at the first fail. In principle, it will take longer to check a long, sorted array.
RELATED LINKS
REMARKS
To see the examples, type: "get-help x -examples".
For more information, type: "get-help x -detailed".
For technical information, type: "get-help x -full".
I wrote script comments in the body of the script block.
I tried to make it as easy as possible to add new test cases to the list.
Here's the final script block and test code:
$isSorted = {
(LT)#
.SYNOPSIS
Checks wether an array is sorted, such that the second of two elements is greater than
or equal to the first.
.DESCRIPTION
Takes an array of any length and checks for being sorted. An array with 0 or 1 members is
sorted by definition.
The function tests each pair in sequence from left to right (0 to highest index) and returns
$false at the first fail. In principle, it will take longer to check a long, sorted array.
.PARAMETER $candidate
The array to be tested.
#(GT)
param ($candidate) # Takes any array - not tested for mixed arrays (strings and ints, for instance
for ($i = 0; $i -lt ($candidate.length - 1); $i++) # checks each position against the position to its right
{
if ($candidate[$i] -gt $candidate[$i+1]) # uses Greater Than.
{
return $false # One failure of Greater Than gives a $false result.
}
}
return $true # if none fail, then true
}
(LT)#
&$isSorted $sorted
&$isSorted $unsorted
#(GT)
# Add test cases as needed.
$sorted = 1,2
$sorted[0] = 1,2,3,4,5 #Expected True
$sorted[1] = 1,1,1,1,1 #Expected True
$unsorted = 1,2,3
$unsorted[0] = 1,2,3,4,3 #Expected False
$unsorted[1] = 3,2,3,4,5 #Expected False
$unsorted[2] = 3,2,3,4,3 #Expected False
foreach ($a in $sorted)
{
if (!(&$isSorted $a)) {"FAIL: List is sorted: $a"}
}
foreach ($a in $unsorted)
{
if (&$isSorted $a) {"FAIL: List is not sorted: $a"}
}}
No comments:
Post a Comment