Re: Price Ranges (#164)
by Toby O'Rourke other posts by this author
Jun 3 2008 5:01AM messages near this date
Re: [QUIZ] Price Ranges (#164)
|
Re: [QUIZ] Price Ranges (#164)
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...@[...].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/home>>.
>
> 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.
Thread:
Matthew Moss
Sharon and Dave
Matthew Rudy Jacobs
Sharon and Dave
Steven Hahn
Harry Kakueki
Toby O'Rourke
jgabrielygalan
Steven Hahn
Andrea Fazzi
S2
Matthew Moss
Matthew Moss
Robert Dober
|