Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,15 @@
sub generate {
my $n = shift;
my $str = '[' x $n;
substr($str, rand($n + $_), 0) = ']' for 1..$n;
return $str;
}
sub balanced {
shift =~ /^ (\[ (?1)* \])* $/x;
}
for (0..8) {
my $input = generate($_);
print balanced($input) ? " ok:" : "bad:", " '$input'\n";
}

View file

@ -0,0 +1,3 @@
sub balanced {
shift =~ /^ ( [^\[\]]++ | \[ (?1)* \] )* $/x;
}

View file

@ -0,0 +1,5 @@
use Regexp::Common 'balanced';
my $re = qr/^$RE{balanced}{-parens=>'[]'}$/;
sub balanced {
return shift =~ $re;
}

View file

@ -0,0 +1,8 @@
sub balanced {
my $depth = 0;
for (split //, shift) {
if ($_ eq '[') { ++$depth }
elsif ($_ eq ']') { return if --$depth < 0 }
}
return !$depth
}