Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Balancing brackets
#perl 5.22 my @results; for (0..10){ my $str=""; for my $length (0..(rand()*10+4)){ $str.=("(",")")[rand()*2]; } printf ("'%-20s' ", $str); print balance( $str),"\n"; } sub balance{ my $level=0; my $pointer =0; foreach (split "",shift){ $level+=($_ eq "(")?1:-1; last if $level<0; $pointer++; } return ("balanced brackets...woohooo!", "unclosed brackets", "bracket closed before open near position $pointer", )[$level <=> 0]; }
run
|
edit
|
history
|
help
0
Perl Lang
size_of_array
Password Validation
Perl logo
Reference
Quotes
Anchal 101712
test hello world perl n texs
Quote code
Challenge 41 Task 1