Re: Price Ranges (#164)
by Matthew Rudy Jacobs other posts by this author
Jun 7 2008 11:48AM messages near this date
Re: [QUIZ] Price Ranges (#164)
|
Re: [QUIZ] Price Ranges (#164)
I assumed that input had to be taken in the way outlined in the
challenge.
So most of my code is about parsing that to create the required ranges.
=========================
class String
def better_to_f
str = match(/\D*([0-9,.]+)/)[1]
str.gsub!(',','_')
str.to_f
end
end
$vendors = <<-STRING
Company A: $1,000 - $3,000
Company B: $6,000 - $10,000
Company C: $500 - $2,500
STRING
vendor_regexp = /(\w.*\w):\s*\$([0-9,.]+) - \$([0-9,.]+)/
$shopper = <<-STRING
Low: $3,500
High: $7,000
STRING
require 'yaml'
parsed = YAML.parse($shopper)
low = parsed['Low'] && parsed['Low'].value.better_to_f
high = parsed['High'] && parsed['High'].value.better_to_f
vendors = $vendors.scan(vendor_regexp)
result = []
vendors.each do |name, min, max|
min = min.better_to_f
max = max.better_to_f
if low && low > max
next
elsif high && high < min
next
else
result << name
end
end
--
Posted via http://www.ruby-forum.com/.
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
|