ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
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

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved