Help for this page

Select Code to Download


  1. or download this
    sub is_balanced {
       my $string = shift;
    ...
       return 0 if $@;
       return 1;
    }
    
  2. or download this
    my $balanced   = "(1 + 2 * (3 + (5/4) + 8) + 6)";
    my $unbalanced1 = "(((1 + 2 * (3 + (5/4) + 8) + 6))";
    ...
    printf("Is $balanced balanced? %s\n", balanced_parens($balanced) ? "ye
    +s" : "no");
    printf("Is $unbalanced1 balanced? %s\n", balanced_parens($unbalanced1)
    + ? "yes" : "no");
    printf("Is $unbalanced2 balanced? %s\n", balanced_parens($unbalanced2)
    + ? "yes" : "no");
    
  3. or download this
    Is (1 + 2 * (3 + (5/4) + 8) + 6) balanced? yes
    Is (((1 + 2 * (3 + (5/4) + 8) + 6)) balanced? no
    Is (((1 + 2 * (3 + (5/4) + 8) + 6)))) balanced? no