Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Postfix to Infix (#148)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Eric Mahurin  
View profile  
 More options Dec 3 2007, 9:42 am
Newsgroups: comp.lang.ruby
From: Eric Mahurin <eric.mahu...@gmail.com>
Date: Mon, 3 Dec 2007 09:42:10 -0500
Local: Mon, Dec 3 2007 9:42 am
Subject: Re: [QUIZ] Postfix to Infix (#148)
Note:  parts of this message were removed by the gateway to make it a legal Usenet post.

On Dec 3, 2007 8:05 AM, Ken Bloom <kbl...@gmail.com> wrote:

> On Sun, 02 Dec 2007 22:23:23 -0500, Eric Mahurin wrote:

> > Note:  parts of this message were removed by the gateway to make it a
> > legal Usenet post.

> > On Nov 30, 2007 7:28 AM, Ruby Quiz <ja...@grayproductions.net> wrote:

> >> For an added bonus, try to keep the parentheses added to infix
> >> expressions to
> >> the minimum of what is needed.

> > My solution does the above, plus a few more things:

> > * maintains an OO data structure (to do everything below) * further
> > reduce parentheses (and post-fix stack depth) by using some
> > associativity
> > * evaluates the result
> > * gives stack-reduced post-fix form

> > The basic idea of the solution is to have an object for each expression
> > (possibly with sub-expressions as operands) and have methods for
> > applying another operation in either direction (i.e. have both #add and
> > #radd - reverse add).  This allows independent decisions on what each
> > type of expression should do when it is in either operand of another
> > operation.

> > Here are a few examples (result shows internal postfix, infix, and
> > result):

> >>ruby quiz148.rb "2 3 5 + *"
> > 2 3 5 + * => 2*(3 + 5) =>  16
> >>ruby quiz148.rb "56 34 213.7 + * 678 -"
> > 56 34 213.7 + * 678 - => 56*(34 + 213.7) - 678 =>  13193.2
> >>ruby quiz148.rb "1 56 35 + 16 9 - / +"
> > 1 56 35 + 16 9 - / + => 1 + (56 + 35)*(16 - 9) =>  14
> >>ruby quiz148.rb "1 2 3 4 5 + + + +"
> > 1 2 + 3 + 4 + 5 + => 1 + 2 + 3 + 4 + 5 =>  15
> >>ruby quiz148.rb "1 2 3 4 5 - - - -"
> > 1 2 - 3 + 4 - 5 + => 1 - 2 + 3 - 4 + 5 =>  3

> This doesn't look right.
> 3 5 * 5 8 * / => 3*5*(5*8) =>  0

> Thanks Ken,

I obviously didn't test divide.  My previously solution has a stupid typo in
Quotient#to_s.  Should be:

class Quotient < Product
  def to_s
    "#{@left}/#{@right}"  # had a * operator here before, whoops!
  end
  ...

Here's a couple more tests:

>ruby quiz148.rb "3 5 / 5 8 / /"

3 5 / 5 / 8 * => 3/5/5*8 =>  0
>ruby quiz148.rb "3 5 5 8 / / /"

3 5 5 / 8 * / => 3/(5/5*8) =>  0

All the results are zero because it is using ruby's Fixnum#/.  The last form
isn't quite optimal because it preferring to minimize divisions over
minimizing groupings/stack-depth.  If you use the commented code in
Product#rdiv like this:

  def rdiv(other)
    # could do this to reduce grouping and stack depth
    # but this will increase expensive divisions
    @left.rdiv(other).div(@right) # might have more divisions now
    #other.div(Group.new(self))
  end

you'll get this:

>ruby quiz148.rb "3 5 5 8 / / /"

3 5 / 5 * 8 / => 3/5*5/8 =>  0

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.