new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
13
Task/Balanced-brackets/Perl/balanced-brackets-1.pl
Normal file
13
Task/Balanced-brackets/Perl/balanced-brackets-1.pl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
use 5.10.0; # for given ... when construct
|
||||
sub balanced {
|
||||
my $depth = 0;
|
||||
for (split //, shift) {
|
||||
when('[') { ++$depth }
|
||||
when(']') { return if --$depth < 0 }
|
||||
}
|
||||
return !$depth
|
||||
}
|
||||
|
||||
for (']', '[', '[[]', '][]', '[[]]', '[[]]]][][]]', 'x[ y [ [] z ]][ 1 ][]abcd') {
|
||||
print balanced($_) ? "" : "not ", "balanced:\t'$_'\n";
|
||||
}
|
||||
10
Task/Balanced-brackets/Perl/balanced-brackets-2.pl
Normal file
10
Task/Balanced-brackets/Perl/balanced-brackets-2.pl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
use 5.10.0; # for '++' non-backtrack behavior
|
||||
sub balanced {
|
||||
my $_ = shift;
|
||||
s/(\[(?:[^\[\]]++|(?1))*\])//g;
|
||||
! /[\[\]]/;
|
||||
}
|
||||
|
||||
for (']', '[', '[[]', '][]', '[[]]', '[[]]]][][]]', 'x[ y [ [] z ]][ 1 ][]abcd') {
|
||||
print balanced($_) ? "" : "not ", "balanced:\t'$_'\n";
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue