Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/Range-expansion/PowerShell/range-expansion-1.psh
Normal file
22
Task/Range-expansion/PowerShell/range-expansion-1.psh
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
function range-expansion($array) {
|
||||
function expansion($arr) {
|
||||
if($arr) {
|
||||
$arr = $arr.Split(',')
|
||||
$arr | foreach{
|
||||
$a = $_
|
||||
$b, $c, $d, $e = $a.Split('-')
|
||||
switch($a) {
|
||||
$b {return $a}
|
||||
"-$c" {return $a}
|
||||
"$b-$c" {return "$(([Int]$b)..([Int]$c))"}
|
||||
"-$c-$d" {return "$(([Int]$("-$c"))..([Int]$d))"}
|
||||
"-$c--$e" {return "$(([Int]$("-$c"))..([Int]$("-$e")))"}
|
||||
}
|
||||
}
|
||||
} else {""}
|
||||
}
|
||||
$OFS = ", "
|
||||
"$(expansion $array)"
|
||||
$OFS = " "
|
||||
}
|
||||
range-expansion "-6,-3--1,3-5,7-11,14,15,17-20"
|
||||
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