Skip to content

Instantly share code, notes, and snippets.

@leonid-shevtsov
Created March 15, 2011 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonid-shevtsov/870924 to your computer and use it in GitHub Desktop.
Save leonid-shevtsov/870924 to your computer and use it in GitHub Desktop.
task :default_cities => :environment do
require 'fastercsv'
city_map = City.all.inject({}) do |cities, city|
cities[city.name] ||= []
cities[city.name] << city
cities
end
population = {}
FasterCSV.open('population.csv','r').each do |r|
name = r[0].strip
title = name + ', '+r[1].strip
population[title] = r[2].gsub(',','').to_i
end
defaults = []
city_map.reject{|name, c| c.length==1}.each do |name, cities|
max = 0
next_max = 0
max_city = nil
cities.each do |city|
p = population[city.title]
if !p.nil? && p>max
next_max = max
max = p
max_city = city
end
end
if max_city && (max>40000) && (next_max==0 || (max/next_max>9))
defaults << max_city
end
end
puts defaults.map(&:title).sort.join("\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment