Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Boolean algebra

by chromatic (Archbishop)
on Nov 13, 2000 at 09:33 UTC ( #41263=note: print w/replies, xml ) Need Help??

in reply to Boolean algebra

Here's the guts of the reduction. It's not particularly brilliant, but it seems to work in my testing. If it were not Sunday night, I might get elaborate with error checking or bit vectors, which would really be the way to go with this.

There is room for someone to come along and to reduce my code's verbosity further. Welcommen.

#!/usr/bin/perl -w use strict; my $input = "(abdc)(adc-b)"; my @expr; while ($input =~ s!\(([-a-z,]+)\)!!) { my @items = split '', $1; my (@and, @or) = (); while (@items) { my $elem = shift @items; if ($elem eq '-') { push @or, shift @items; } else { push @and, $elem; } } push @expr, [ \@and, \@or ]; } my $first = shift @expr; my @output = @{ @$first[0] }; while (my $next = shift @expr) { my (%keep, %drop); @keep{@{ @$next[0] }} = (); @drop{@{ @$next[1] }} = (); foreach (@output) { next if (exists $keep{$_}); $_ = undef if (exists $drop{$_}); } } my $output = join('', grep{ defined($_) } @output); print "Result is; $output\n";

Update: I realized early this morning that this only handles the OR case in the second and subsequent input units. Back to the drawing board.

Replies are listed 'Best First'.
RE: Re: Boolean algebra
by le (Friar) on Nov 13, 2000 at 17:57 UTC
    Sorry, but your code is not correct (it works for this input but not for generic input).

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://41263]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2023-03-25 19:42 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (63 votes). Check out past polls.