-
-
Save JohnFord/923754 to your computer and use it in GitHub Desktop.
An atlrug member asked for a code-review so I made a couple of changes and suggestions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
class Store | |
attr_accessor :name, :address, :city, :state, :zip, :phone_number | |
end | |
def list_of_stores | |
files = Dir.glob('*.yml') | |
stores = [] | |
files.each do |file| | |
File.open(file, 'r') {|f| stores += YAML.load(f)} | |
end | |
return stores | |
end | |
def delete_duplicates(stores) | |
seen = [] | |
marker = [] | |
counter = 0 | |
stores.each do |store| | |
attr_array = [store.name[0..5], store.zip, store.phone_number, store.address[0..3]] | |
if seen.include?(attr_array) | |
marker << store | |
counter += 1 | |
else | |
seen << attr_array | |
end | |
end | |
marker.each {|m| stores.delete(m)} | |
return counter | |
end | |
def print_multi_stores(stores) | |
seen = Hash.new { |h,k| h[k] = Hash.new(0) } | |
stores.each do |store| | |
seen[store.name][store.state] += 1 | |
end | |
seen = seen.sort_by {|k,v| v[0]} | |
seen.reverse! | |
puts "" | |
puts "multi stores" | |
puts "-------------" | |
seen.each do |store_name,state_counts| | |
total_stores = state_counts.values.reduce(0) { |sum, val| sum += val } | |
next if total_stores < 2 | |
puts "#{store_name}: #{total_stores} stores" | |
state_counts.each do |state,count| | |
puts "\t#{state} -- #{count}" | |
end | |
end | |
puts "-------------" | |
puts "" | |
end | |
stores = list_of_stores | |
stores.sort_by! {|s| s.zip} | |
puts stores.length | |
puts "deleted " + delete_duplicates(stores).to_s + " duplicate stores" | |
print_multi_stores(stores) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment