PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Fwd: Ruby Quiz #148
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Fwd: Ruby Quiz #148

Réponse
 
LinkBack Outils de la discussion
Vieux 03/12/2007, 02h47   #1
Dave Pederson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Fwd: Ruby Quiz #148

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

Solution to quiz #148.
Thanks,

-Dave
---------- Forwarded message ----------
From: Dave Pederson <dave.pederson.ruby@gmail.com>
Date: Nov 30, 2007 7:26 PM
Subject: Ruby Quiz #148
To: dave.pederson.ruby@gmail.com


#!/usr/bin/env ruby

op = %w{ + - / * }
pm = %w{ + - }

postfixes = ["2 3 5 + *",
"1 56 35 + 16 9 - / +",
"56 34 213.7 + * 678 -",
"5 9 * 8 7 4 6 + * 2 1 3 * + * + *"]

puts
postfixes.each do |postfix|
stack = []
postfix.split.each do |c|
if op.include?(c)
second, first = stack.pop, stack.pop
if pm.include?(c)
stack.push "(#{first} #{c} #{second})"
else
stack.push "#{first} #{c} #{second}"
end
else
stack.push(c)
end
end
infix = stack.pop
if (infix[0] == 40 && infix[infix.size-1] == 41)
infix = infix[1, infix.size-2]
end
puts "postfix = #{postfix}"
puts "infix = #{infix}"
puts
end

  Réponse avec citation
Vieux 03/12/2007, 03h17   #2
Michael Boutros
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Fwd: Ruby Quiz #148

I've looked at a lot of solutions for this quiz today and I have to say
I like yours the most. Good work
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 03/12/2007, 04h09   #3
James Edward Gray II
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby Quiz #148

On Dec 2, 2007, at 9:17 PM, Michael Boutros wrote:

> I've looked at a lot of solutions for this quiz today and I have to
> say
> I like yours the most. Good work


Since there was no name specified, I think all solvers should assume
he was talking to you.

James Edward Gray II

  Réponse avec citation
Vieux 03/12/2007, 04h38   #4
Ken Bloom
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Fwd: Ruby Quiz #148

On Sun, 02 Dec 2007 21:47:29 -0500, Dave Pederson wrote:

> Note: parts of this message were removed by the gateway to make it a
> legal Usenet post.
>
> Solution to quiz #148.
> Thanks,
>
> -Dave
> ---------- Forwarded message ---------- From: Dave Pederson
> <dave.pederson.ruby@gmail.com> Date: Nov 30, 2007 7:26 PM
> Subject: Ruby Quiz #148
> To: dave.pederson.ruby@gmail.com
>
>
> #!/usr/bin/env ruby
>
> op = %w{ + - / * }
> pm = %w{ + - }
>
> postfixes = ["2 3 5 + *",
> "1 56 35 + 16 9 - / +",
> "56 34 213.7 + * 678 -",
> "5 9 * 8 7 4 6 + * 2 1 3 * + * + *"]
>
> puts
> postfixes.each do |postfix|
> stack = []
> postfix.split.each do |c|
> if op.include?(c)
> second, first = stack.pop, stack.pop
> if pm.include?(c)
> stack.push "(#{first} #{c} #{second})"
> else
> stack.push "#{first} #{c} #{second}"
> end
> else
> stack.push(c)
> end
> end
> infix = stack.pop
> if (infix[0] == 40 && infix[infix.size-1] == 41)
> infix = infix[1, infix.size-2]
> end
> puts "postfix = #{postfix}"
> puts "infix = #{infix}"
> puts
> end


This is incorrect:

postfix = 3 5 * 5 8 * /
infix = 3 * 5 / 5 * 8

It should be
infix = 3 * 5 / (5 * 8)

--Ken

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
  Réponse avec citation
Vieux 03/12/2007, 07h46   #5
Dave Pederson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Fwd: Ruby Quiz #148

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

Thanks for pointing that out. I was trying to minimize parentheses...looks
like i kinda hurried though it. Anyways, here was my first solution (minus
the bonus points):

#!/usr/bin/env ruby

op = %w{ + - * / }
postfixes = ["2 3 5 + *",
"1 56 35 + 16 9 - / +",
"56 34 213.7 + * 678 -",
"5 9 * 8 7 4 6 + * 2 1 3 * + * + *",
"3 5 * 5 8 * /"]

postfixes.each do |postfix|
stack = []
postfix.split.each do |c|
unless op.include? c
stack.push(c)
else
second, first = stack.pop, stack.pop
stack.push "( #{first} #{c} #{second} )"
end
end
puts "postfix = #{postfix}"
puts "infix = #{stack.pop}"
puts
end



On Dec 2, 2007 8:40 PM, Ken Bloom <kbloom@gmail.com> wrote:

> On Sun, 02 Dec 2007 21:47:29 -0500, Dave Pederson wrote:
>
> > Note: parts of this message were removed by the gateway to make it a
> > legal Usenet post.
> >
> > Solution to quiz #148.
> > Thanks,
> >
> > -Dave
> > ---------- Forwarded message ---------- From: Dave Pederson
> > <dave.pederson.ruby@gmail.com> Date: Nov 30, 2007 7:26 PM
> > Subject: Ruby Quiz #148
> > To: dave.pederson.ruby@gmail.com
> >
> >
> > #!/usr/bin/env ruby
> >
> > op = %w{ + - / * }
> > pm = %w{ + - }
> >
> > postfixes = ["2 3 5 + *",
> > "1 56 35 + 16 9 - / +",
> > "56 34 213.7 + * 678 -",
> > "5 9 * 8 7 4 6 + * 2 1 3 * + * + *"]
> >
> > puts
> > postfixes.each do |postfix|
> > stack = []
> > postfix.split.each do |c|
> > if op.include?(c)
> > second, first = stack.pop, stack.pop
> > if pm.include?(c)
> > stack.push "(#{first} #{c} #{second})"
> > else
> > stack.push "#{first} #{c} #{second}"
> > end
> > else
> > stack.push(c)
> > end
> > end
> > infix = stack.pop
> > if (infix[0] == 40 && infix[infix.size-1] == 41)
> > infix = infix[1, infix.size-2]
> > end
> > puts "postfix = #{postfix}"
> > puts "infix = #{infix}"
> > puts
> > end

>
> This is incorrect:
>
> postfix = 3 5 * 5 8 * /
> infix = 3 * 5 / 5 * 8
>
> It should be
> infix = 3 * 5 / (5 * 8)
>
> --Ken
>
> --
> Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
> Department of Computer Science. Illinois Institute of Technology.
> http://www.iit.edu/~kbloom1/ <http://www.iit.edu/%7Ekbloom1/>
>
>


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h23.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13141 seconds with 13 queries