Guest User

Untitled

a guest
May 29th, 2017
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.84 KB | None | 0 0
  1. require 'csv'
  2.  
  3. def extract_key_price(row)
  4.   brand, item, price_str, *_blah_blah_blah = row # FIXME: Reflect the actual csv structure here
  5.   key = "#{brand} #{item}"
  6.   price = price_str.to_f
  7.  
  8.   [key, price]
  9. end
  10.  
  11. # Building the dictionary of minimum prices for "<brand> <item>" keys
  12. min_price = Hash.new(2**32) # Any number that is greater than any item price goes here
  13.  
  14. CSV.foreach("<source_filename>") do |row| # FIXME: Put source filename here
  15.   key, price = extract_key_price(row)
  16.   min_price[key] = price if min_price[key] > price
  17. end
  18.  
  19. # Main processing
  20. File.open('<target_filename>', 'w') do |result| # FIXME: Put result filename here
  21.   CSV.foreach("<source_filename>") do |row|     # FIXME: Put source filename here
  22.     key, price = extract_key_price(row)
  23.     result.puts(CSV.generate_line(row)) if min_price[key] == price
  24.   end
  25. end
Add Comment
Please, Sign In to add comment