Perl,32 = 31 +1或73 = 72 +1(最小括号)
32 = 31 + 1:带有多余的括号
编辑:
- 修复,括号现在用
y///
。
$a
删除不必要的变量。
$_="("x y/)//.s|$|")"x y/(//|er
它与运行时开关-p
(+1字节)一起使用。
测试文件input.txt
:
This line has no parentheses
alert(Math.max(1, 2
1+1)*2).toString()
function() { alert('Hello, World!'); })(
(foo))(bar
)))(((
((
))
命令行:
perl -p script.pl <input.txt
要么
perl -pe '$_="("x y/)//.s|$|")"x y/(//|er' <input.txt
结果:
This line has no parentheses
alert(Math.max(1, 2))
(((1+1)*2).toString())
(((function() { alert('Hello, World!'); })()))
(((foo))(bar))
((()))((()))
(())
(())
取消高尔夫:
该算法很简单,只需为每个找到的括号添加对应的括号。
$_ = # $_ is provided as input by switch `-p` and
# it is printed afterwards as output.
# y/X// is used to count the character 'X' in $_
'(' x y/)// # add opening parentheses for each closing parentheses
. s|$|')' x y/(//|er # go right before the end of line and insert
# closing parentheses for each opening parentheses
# in the original string
73 = 72 + 1:添加最小值括号
该脚本仅添加最小括号即可获得平衡的输出。
$a=y/()//cdr;1while$a=~s/\(\)//g;$_=$a=~y/)(/(/dr.$_;s|$|$a=~y/()/)/dr|e
它与运行时开关-p
(+1字节)一起使用。
perl -pe "$a=y/()//cdr;1while$a=~s/\(\)//g;$_=$a=~y/)(/(/dr.$_;s|$|$a=~y/()/)/dr|e" <input.txt
结果:
This line has no parentheses
alert(Math.max(1, 2))
((1+1)*2).toString()
(function() { alert('Hello, World!'); })()
((foo))(bar)
((()))((()))
(())
(())
取消高尔夫:
$a = y/()//cdr; # filter parentheses and store in $a
1 while $a =~ s/\(\)//g; # remove matching parentheses
$_ = $a =~ y/)(/(/dr . $_; # add missing opening parentheses at start of string
s|$|$a=~y/()/)/dr|e # insert missing closing parentheses at end of string
81 = 80 + 1:添加最小括号
这是为平衡的输出添加最小括号数的较旧方法。
my($l,$r);s/[()]/($&eq")"&&($r&&$r--||++$l))||$r++/ger;$_="("x$l.$_;s/$/")"x$r/e
它使用Perl 5.14(由于使用了非破坏性替换修饰符)和运行时开关-p
(+1字节)。
perl -p script.pl <input.txt
结果:
This line has no parentheses
alert(Math.max(1, 2))
((1+1)*2).toString()
(function() { alert('Hello, World!'); })()
((foo))(bar)
((()))((()))
(())
(())
取消高尔夫:
# The while loop is added by option "-p".
LINE:
while (<>) {
# $_ contains the current line
my ($l, $r); # initializes $l and $r (to undef/kind of indirect 0)
# Modifiers for the following substitution of $_:
# /g: process all parentheses
# /e: evaluate code
# /r: does not change the original input string $_ (Perl 5.14)
s/[()]/
# $& contains the matched parentheses
# $r is a balance level counter; at the end $r contains
# the number of needed closing parentheses
# $l is the number of needed opening parentheses;
# if $r would go negative, then an opening parentheses
# is missing and $l is increases and $r remains zero.
(
$& eq ")" && # case ")"
($r && $r-- # close a parentheses group and update balance counter
|| ++$l) # or update $l if an opening parentheses is needed
)
|| $r++ # case "(": increase balance counter
/ger;
$_ = "(" x $l . $_; # add opening parentheses at the begin of line
s/$/")" x $r/e # add closing parentheses before the line end
# the remainder is added by run-time switch "-p"
} continue {
print or die "-p destination: $!\n";
}
()
括号,或者做其他括号{}
,[]
,<>
,等需要考虑呢?