September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
42
Task/Range-expansion/PowerShell/range-expansion-2.psh
Normal file
42
Task/Range-expansion/PowerShell/range-expansion-2.psh
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
function Expand-Range
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory=$true,
|
||||
Position=0)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[ValidatePattern('^[0-9,-]*$')]
|
||||
[string]
|
||||
$Range
|
||||
)
|
||||
|
||||
try
|
||||
{
|
||||
if ($Range -match '-,') # I'm not good enough to weed this case out with Regex
|
||||
{
|
||||
throw "Input string was not in a correct format."
|
||||
}
|
||||
|
||||
[int[]]$output = $Range -split ',' | ForEach-Object {
|
||||
|
||||
[int[]]$array = $_ -split '(?<=\d)-'
|
||||
|
||||
if ($array.Count -gt 1) # $array contains one or two elements
|
||||
{
|
||||
$array[0]..$array[1] # two elements = start and end of range
|
||||
}
|
||||
else
|
||||
{
|
||||
$array # one element = an integer
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw "Input string was not in a correct format."
|
||||
}
|
||||
|
||||
$output
|
||||
}
|
||||
1
Task/Range-expansion/PowerShell/range-expansion-3.psh
Normal file
1
Task/Range-expansion/PowerShell/range-expansion-3.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
(Expand-Range "-6,-3--1,3-5,7-11,14,15,17-20") -join ", "
|
||||
Loading…
Add table
Add a link
Reference in a new issue