RosettaCodeData/Task/Run-length-encoding/PowerShell/run-length-encoding.psh

19 lines
387 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
function Compress-RLE ($s) {
$re = [regex] '(.)\1*'
$ret = ""
foreach ($m in $re.Matches($s)) {
$ret += $m.Length
$ret += $m.Value[0]
}
return $ret
}
function Expand-RLE ($s) {
$re = [regex] '(\d+)(.)'
$ret = ""
foreach ($m in $re.Matches($s)) {
$ret += [string] $m.Groups[2] * [int] [string] $m.Groups[1]
}
return $ret
}