[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ] Price Ranges (#164

Matthew Moss

5/31/2008 12:27:00 AM

[Note: parts of this message were removed to make it a legal post.]

Apologies for the late quiz... been rather busy today. Here's another simple
one, but practical. I didn't get specific about input
formats/parameters/etc, I leave that to you this week.


-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Quiz 2:

1. Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have passed from the time on this message.

2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A
permanent, new website is in the works for Ruby Quiz 2. Until then,
please visit the temporary website at

<http://splatbang.com/rubyquiz/<http://matthew.moss.googlepages.com/ho....


3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby Talk follow the discussion. Please reply to
the original quiz message, if you can.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

## Price Ranges

_Quiz description by James Edward Gray II_

You have a list of price ranges from different vendors like:

Company A: $1,000 - $3,000
Company B: $6,000 - $10,000
Company C: $500 - $2,500

Given such a list and the desired price range a shopper wishes to pay,
return the companies the shopper should examine. For example, if the
shopper's price range was:

Low: $2,500
High: $5,000

the companies returned would be:

Company A
Company C

The shopper should also be allowed to provide just a low or just a high
value instead of both, should they prefer to do so.

15 Answers

Matthew Moss

5/31/2008 12:38:00 AM

0

> 2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A
> permanent, new website is in the works for Ruby Quiz 2. Until then,
> please visit the temporary website at

Why does typing a simple email have to be so difficult? *sheesh* The
correct, unmanged web address is:

<http://splatbang.com/rub...

Matthew Moss

5/31/2008 1:50:00 AM

0

> Why does typing a simple email have to be so difficult? *sheesh* The
> correct, unmanged web address is:

Unmanged?

*smack head*

I swear, my I.Q. must be dropping 10 points _daily_.


Robert Dober

5/31/2008 8:24:00 AM

0

On Sat, May 31, 2008 at 3:50 AM, Matthew Moss <matthew.moss@gmail.com> wrote:
>> Why does typing a simple email have to be so difficult? *sheesh* The
>> correct, unmanged web address is:
>
> Unmanged?
>
> *smack head*
>
> I swear, my I.Q. must be dropping 10 points _daily_.
>
>
Cheer up Matt I am trying to put my gmail into TWI*M mode for years
now and I cannot figure out how. Hugh.
R.
>



--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Chris Shea

6/2/2008 4:24:00 AM

0

On May 30, 6:27 pm, Matthew Moss <matthew.m...@gmail.com> wrote:
> [Note: parts of this message were removed to make it a legal post.]
>
> Apologies for the latequiz... been rather busy today. Here's another simple
> one, but practical. I didn't get specific about input
> formats/parameters/etc, I leave that to you this week.
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> The three rules of RubyQuiz2:
>
> 1. Please do not post any solutions or spoiler discussion for thisquizuntil 48 hours have passed from the time on this message.
>
> 2. Support RubyQuiz2 by submitting ideas as often as you can! (A
> permanent, new website is in the works for RubyQuiz2. Until then,
> please visit the temporary website at
>
> <http://splatbang.com/rubyquiz/<http://matthew.moss.googlepages.com/ho....
>
> 3. Enjoy!
>
> Suggestion: A [QUIZ] in the subject of emails about the problem
> helps everyone on Ruby Talk follow the discussion. Please reply to
> the originalquizmessage, if you can.
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> ## Price Ranges
>
> _Quiz description by James Edward Gray II_
>
> You have a list of price ranges from different vendors like:
>
> Company A: $1,000 - $3,000
> Company B: $6,000 - $10,000
> Company C: $500 - $2,500
>
> Given such a list and the desired price range a shopper wishes to pay,
> return the companies the shopper should examine. For example, if the
> shopper's price range was:
>
> Low: $2,500
> High: $5,000
>
> the companies returned would be:
>
> Company A
> Company C
>
> The shopper should also be allowed to provide just a low or just a high
> value instead of both, should they prefer to do so.

You weren't looking for a user interface, were you? ;)

http://pastie.textmate....

Chris

Donald Ball

6/2/2008 5:34:00 AM

0

My simple solution:

http://pastie.caboo...

- donald

Andrea Fazzi

6/2/2008 8:12:00 AM

0

Matthew Moss ha scritto:
> Apologies for the late quiz... been rather busy today. Here's another simple
> one, but practical. I didn't get specific about input
> formats/parameters/etc, I leave that to you this week.
>
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> The three rules of Ruby Quiz 2:
>
> 1. Please do not post any solutions or spoiler discussion for this
> quiz until 48 hours have passed from the time on this message.
>
> 2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A
> permanent, new website is in the works for Ruby Quiz 2. Until then,
> please visit the temporary website at
>
> <http://splatbang.com/rubyquiz/<http://matthew.moss.googlepages.com/ho....
>
>
> 3. Enjoy!
>
> Suggestion: A [QUIZ] in the subject of emails about the problem
> helps everyone on Ruby Talk follow the discussion. Please reply to
> the original quiz message, if you can.
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> ## Price Ranges
>
> _Quiz description by James Edward Gray II_
>
> You have a list of price ranges from different vendors like:
>
> Company A: $1,000 - $3,000
> Company B: $6,000 - $10,000
> Company C: $500 - $2,500
>
> Given such a list and the desired price range a shopper wishes to pay,
> return the companies the shopper should examine. For example, if the
> shopper's price range was:
>
> Low: $2,500
> High: $5,000
>
> the companies returned would be:
>
> Company A
> Company C
>
> The shopper should also be allowed to provide just a low or just a high
> value instead of both, should they prefer to do so.
>
>

Here my solution (with specs):

http://pastie.caboo...
http://pastie.caboo...

Bye.
Andrea


vc

6/2/2008 8:40:00 AM

0

Matthew Moss wrote:

> ## Price Ranges
>

Here it is:
http://pastebin....

--
http://s2.d...

Steven Hahn

6/2/2008 7:56:00 PM

0


I had not created a SAX listener before in Ruby. So, my solution reads the=
vendor list in from XML. I noticed others were using this pastie thingy..=
I don't know what that is. Besides, pasties always seemed to be kind of =
pointless to me. They leave precious little to the imagination. So, below=
is the ruby file followed by the xml file:

---------------------------------------------------------------------------=
------
#!/usr/local/bin/ruby -w
require 'rexml/parsers/sax2parser'
require 'rexml/sax2listener'

class VendorListener
include REXML::SAX2Listener

def initialize(low, high)
begin
@low =3D Float(low)
rescue
@low =3D 0
end
=20
begin
@high =3D Float(high)
rescue
#if someone can spend more than this then
#they can afford a better program
@high =3D 10**15
end
end
=20
def start_element(uri, localname, tag_name, attrs)
if tag_name =3D=3D 'Vendor'
@vendorName =3D attrs['name']
end
end

def end_element(uri, localname, tag_name)
if tag_name =3D=3D 'Vendor'
if in_range?
puts @vendorName
end
elsif tag_name =3D=3D 'LowPrice'
@lowPrice =3D Float(@data)
elsif tag_name =3D=3D 'HighPrice'
@highPrice =3D Float(@data)
end
end

def characters(value)
@data =3D value
end

def in_range?
(@low >=3D @lowPrice and @low <=3D @highPrice) or
(@high >=3D @lowPrice and @high <=3D @highPrice) or
(@low < @lowPrice and @high > @highPrice)
end
end

puts "Enter a price range."
print "Enter low value: "
low =3D gets
print "Enter high value: "
high =3D gets

parser =3D REXML::Parsers::SAX2Parser.new(File.new('vendors.xml' ))
parser.listen(VendorListener.new(low, high))
parser.parse

puts "Program terminated."
---------------------------------------------------------------------------=
------
<?xml version=3D"1.0" encoding=3D"UTF-8"?>
<Vendors>
<Vendor name=3D"Company A">
<PriceRange>
<LowPrice>1000</LowPrice>
<HighPrice>3000</HighPrice>
</PriceRange>
</Vendor>
<Vendor name=3D"Company B">
<PriceRange>
<LowPrice>6000</LowPrice>
<HighPrice>10000</HighPrice>
</PriceRange>
</Vendor>
<Vendor name=3D"Company C">
<PriceRange>
<LowPrice>500</LowPrice>
<HighPrice>2500</HighPrice>
</PriceRange>
</Vendor>
</Vendors>


_________________________________________________________________
Make every e-mail and IM count. Join the i=92m Initiative from Microsoft.
http://im.live.com/Messenger/IM/Join/Default.aspx?source... MakeCoun=
t=

Jesús Gabriel y Galán

6/3/2008 6:14:00 AM

0

On Sat, May 31, 2008 at 2:27 AM, Matthew Moss <matthew.moss@gmail.com> wrote:
> ## Price Ranges
>
> _Quiz description by James Edward Gray II_
>
> You have a list of price ranges from different vendors like:
>
> Company A: $1,000 - $3,000
> Company B: $6,000 - $10,000
> Company C: $500 - $2,500
>
> Given such a list and the desired price range a shopper wishes to pay,
> return the companies the shopper should examine. For example, if the
> shopper's price range was:
>
> Low: $2,500
> High: $5,000
>
> the companies returned would be:
>
> Company A
> Company C
>
> The shopper should also be allowed to provide just a low or just a high
> value instead of both, should they prefer to do so.

Here's my solution. I use codeforpeople's main, so the program accepts
a --help that explains the params. I expected a file in CSV format for
the company data:

company1, low, high
company2, low, high

and the shopper's low and high are passed as options to the program:

require 'main'
require 'fastercsv'

Main {
argument('file')

option('low', 'l') {
argument_required
cast :int
default 0
}

option('high', 'g') {
argument_required
cast :int
}

def run
file = params[:file].value
low = params[:low].value
have_high = params[:high].given?
high = params[:high].value if have_high
values = []
FasterCSV.foreach file do |line|
cmp_values = [line[0]]
cmp_values << line[1].to_i
cmp_values << line[2].to_i
values << cmp_values
end

unless have_high
companies = values.select {|x| x[2] >= low}
else
companies = values.reject {|x| (x[1] > high) || (x[2] < low)}
end
companies.map! {|x| x[0]}
puts companies
end
}

For example for this company_data.csv file:

company1, 1000, 3000
company2, 6000, 10000
company3, 500, 2500

these are some runs:

$ ruby quiz164.rb company_data.csv
company1
company2
company3

$ ruby quiz164.rb company_data.csv -l 4000
company2

$ ruby quiz164.rb company_data.csv -l 4000 -g 5000

$ ruby quiz164.rb company_data.csv -g 1500
company1
company3

$ ruby quiz164.rb company_data.csv -g 750
company3

$ ruby quiz164.rb --help
NAME
quiz164.rb

SYNOPSIS
quiz164.rb file [options]+

PARAMETERS
file (1 -> file)
--low=low, -l (0 ~> int(low=0))
--high=high, -g (0 ~> int(high))
--help, -h


Thanks for the quiz.

Jesus.

Toby O'Rourke

6/3/2008 12:00:00 PM

0

A simple solution to my first Ruby Quiz

class SupplierFinder
attr_accessor :companies
def initialize(suppliers='')
@companies = Hash.new
File.open(suppliers).each do |line|
range = line.scan(/\$[\d,]+/)
companies.store( Range::new(parse(range[0]),parse(range[1])),
line.match(/^[\w\s]*/))
end
end
def search(switch=nil, low=0, high=0)
my_range = Range::new(low, high) if switch == '-r'
companies.each_pair do |range, name|
puts name if ( !my_range.nil? and (my_range.include? range.first
or my_range.include? range.last or range.include? low or
range.include? high )) or (switch == '-h' and range.first <= low) or
(switch == '-l' and range.last >= low)
end
end
private
def parse(price)
price.gsub( '$', '').gsub(',','').to_i
end
end
SupplierFinder.new(ARGV[0]).search(ARGV[1], ARGV[2].to_i,
ARGV[3].to_i) if __FILE__ == $0

Cheers,

Toby.

On May 31, 1:27 am, "Matthew Moss" <matthew.m...@gmail.com> wrote:
> Apologies for the late quiz... been rather busy today. Here's another simple
> one, but practical. I didn't get specific about input
> formats/parameters/etc, I leave that to you this week.
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> The three rules of Ruby Quiz 2:
>
> 1. Please do not post any solutions or spoiler discussion for this
> quiz until 48 hours have passed from the time on this message.
>
> 2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A
> permanent, new website is in the works for Ruby Quiz 2. Until then,
> please visit the temporary website at
>
> <http://splatbang.com/rubyquiz/<http://matthew.moss.googlepages.com/ho....
>
> 3. Enjoy!
>
> Suggestion: A [QUIZ] in the subject of emails about the problem
> helps everyone on Ruby Talk follow the discussion. Please reply to
> the original quiz message, if you can.
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> ## Price Ranges
>
> _Quiz description by James Edward Gray II_
>
> You have a list of price ranges from different vendors like:
>
> Company A: $1,000 - $3,000
> Company B: $6,000 - $10,000
> Company C: $500 - $2,500
>
> Given such a list and the desired price range a shopper wishes to pay,
> return the companies the shopper should examine. For example, if the
> shopper's price range was:
>
> Low: $2,500
> High: $5,000
>
> the companies returned would be:
>
> Company A
> Company C
>
> The shopper should also be allowed to provide just a low or just a high
> value instead of both, should they prefer to do so.