Perl, 32 = 31 + 1 or 73 = 72 + 1 (minimized parentheses)
32 = 31 + 1: with extra unnecessary parentheses
Edits:
- Fix, parentheses now counted with
y///
.
- Unnecessary variable
$a
removed.
$_="("x y/)//.s|$|")"x y/(//|er
It is used with the run-time switch -p
(+1 byte).
Test file input.txt
:
This line has no parentheses
alert(Math.max(1, 2
1+1)*2).toString()
function() { alert('Hello, World!'); })(
(foo))(bar
)))(((
((
))
Command line:
perl -p script.pl <input.txt
or
perl -pe '$_="("x y/)//.s|$|")"x y/(//|er' <input.txt
Result:
This line has no parentheses
alert(Math.max(1, 2))
(((1+1)*2).toString())
(((function() { alert('Hello, World!'); })()))
(((foo))(bar))
((()))((()))
(())
(())
Ungolfed:
The algorithm is simple, just add the counterpart for each found parentheses.
$_ = # $_ 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: adding minimum number of parentheses
This script only adds the minimum number of parentheses to get a balanced output.
$a=y/()//cdr;1while$a=~s/\(\)//g;$_=$a=~y/)(/(/dr.$_;s|$|$a=~y/()/)/dr|e
It is used with the run-time switch -p
(+1 byte).
perl -pe "$a=y/()//cdr;1while$a=~s/\(\)//g;$_=$a=~y/)(/(/dr.$_;s|$|$a=~y/()/)/dr|e" <input.txt
Result:
This line has no parentheses
alert(Math.max(1, 2))
((1+1)*2).toString()
(function() { alert('Hello, World!'); })()
((foo))(bar)
((()))((()))
(())
(())
Ungolfed:
$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: adding minimum number of parentheses
This is an older method to add the minimum number of parentheses for a balanced output.
my($l,$r);s/[()]/($&eq")"&&($r&&$r--||++$l))||$r++/ger;$_="("x$l.$_;s/$/")"x$r/e
It uses Perl 5.14 (because of the non-destructive substitution modifier) and the run-time switch -p
(+1 byte).
perl -p script.pl <input.txt
Result:
This line has no parentheses
alert(Math.max(1, 2))
((1+1)*2).toString()
(function() { alert('Hello, World!'); })()
((foo))(bar)
((()))((()))
(())
(())
Ungolfed:
# 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";
}
()
parens, or do other brackets{}
,[]
,<>
, etc need to be considered as well? \$\endgroup\$(
and)
. \$\endgroup\$