Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi,

it's been a long time since I posted my last question here, but I've started my studies of CS and don't find a lot of time anymore.

Well, one of our first courses at the university is called "Introduction to programming" where we do stuff in Modula-2 (yeah). I always felt like "Hey how easy could this be done in Perl", but I resisted to annotate my thoughts in a comment in the source code :-)

One of the first examples we had to solve was the simplification of a boolean expression. The input looks like this
(abdc)(adc-b)
which aliases two boolean expressions. The terms inside the expressions are ANDed together, and the two expressions are ORed together (a negation is expressed with "-"), which means that the above string would be
(a AND b AND d AND c) OR (a AND d AND c AND NOT b)

Now boolean algebra says that one can simplify this expression if and only if the two partial expressions differ in just one negated variable, so the solution for the above example would be:
(adc)
The specification for this example was rather strict:
  • Each variable inside an expression may only occur once (the allowed characters were a..z, "(", ")" and "-")
  • If the expression could be simplified do so, else print something like "CANNOT BE SIMPLIFIED"
  • If the input is wrong, print something like "WRONG INPUT"
  • If the expression can be simplified, the solution was the first partial expression minus the differing variable.
  • The order of variables in the output string had to be the same as the input string.
  • If the two partial expressions are the same, the result is the first partial expression.
Ok, I finished this example (and it was correct :-) ), but then I thought, just for fun, I do it in Perl, but now I just can't find a really cool "perlish" solution for this.

So I want to know if you guys (and ladies of course) can give a quick hack for this. (My Modula-2 programm was 202 lines long, pretty-formatted.)

In reply to Boolean algebra by le

Title:
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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-03-29 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found