Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'csv'
- def extract_key_price(row)
- brand, item, price_str, *_blah_blah_blah = row # FIXME: Reflect the actual csv structure here
- key = "#{brand} #{item}"
- price = price_str.to_f
- [key, price]
- end
- # Building the dictionary of minimum prices for "<brand> <item>" keys
- min_price = Hash.new(2**32) # Any number that is greater than any item price goes here
- CSV.foreach("<source_filename>") do |row| # FIXME: Put source filename here
- key, price = extract_key_price(row)
- min_price[key] = price if min_price[key] > price
- end
- # Main processing
- File.open('<target_filename>', 'w') do |result| # FIXME: Put result filename here
- CSV.foreach("<source_filename>") do |row| # FIXME: Put source filename here
- key, price = extract_key_price(row)
- result.puts(CSV.generate_line(row)) if min_price[key] == price
- end
- end
Add Comment
Please, Sign In to add comment