Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: Boolean algebra by chromatic
in thread Boolean algebra by le

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-21 08:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found