mozy contest: problem 1 PHP solution

Nicholas Leippe nick at leippe.com
Mon Nov 6 11:04:22 MST 2006


> > The first problem was to evaluate a line full of parenthesis. If they
> > were not balanced on the line output a 0.  If they were balanced, output
> > the maximum nesting depth.
> >

My code fails to catch bogus nestings such as ")(". It needs another test:

> > <?
> > while ($line = fgets(STDIN)) {
> >         $mx = 0;
> >         $cur_depth = 0;
> >         for ($i = 0; $i < strlen($line); $i++) {
> >                 $p = $line{$i};
> >                 if ($p == '(') {
> >                         $cur_depth++;
> >                 } else if ($p == ')') {

			if (!$cur_depth) {
				echo "0\n";
				continue;
			}

> >                         $mx = max($mx, $cur_depth);
> >                         $cur_depth--;
> >                 }
> >         }
> >         if ($cur_depth == 0) {
> >                 echo "$mx\n";
> >         } else {
> >                 echo "0\n";
> >         }
> > }



More information about the PLUG mailing list