Skip to content

Instantly share code, notes, and snippets.

@rosiehoyem
Created October 14, 2013 15:28
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 rosiehoyem/6977486 to your computer and use it in GitHub Desktop.
Save rosiehoyem/6977486 to your computer and use it in GitHub Desktop.
Pigeon Organizer
########################
# NYC PIGEON ORGANIZER #
########################
pigeon_data = {
:color => {
:purple => ["Theo", "Peter Jr.", "Lucky"],
:grey => ["Theo", "Peter Jr.", "Ms .K"],
:white => ["Queenie", "Andrew", "Ms .K", "Alex"],
:brown => ["Queenie", "Alex"]
},
:gender => {
:male => ["Alex", "Theo", "Peter Jr.", "Andrew", "Lucky"],
:female => ["Queenie", "Ms .K"]
},
:lives => {
"Subway" => ["Theo", "Queenie"],
"Central Park" => ["Alex", "Ms .K", "Lucky"],
"Library" => ["Peter Jr."],
"City Hall" => ["Andrew"]
}
}
#deconstruct data
#property
#value
#birds
# organized_pigeon_data = {}
# pigeon_data.each do |property, value_hash|
# value_hash.each do |value, birds|
# birds.each do |name|
# if organized_pigeon_data[name]
# if organized_pigeon_data[name][property]
# organized_pigeon_data[name][property] << value
# else
# organized_pigeon_data[name][property] = [value]
# end
# else
# organized_pigeon_data[name] = {property => value}
# end
# end
# end
# end
# raise organized_pigeon_data.inspect
## Avi's Solution ##
organized_pigeon_data = {}
pigeon_data.each do |property, value_hash|
value_hash.each do |value, birds|
birds.each do |name|
if organized_pigeon_data[name]
if organized_pigeon_data[name][property]
organized_pigeon_data[name][property] << value
else
organized_pigeon_data[name][property] = [value]
end
else
organized_pigeon_data[name] = {property => value}
end
end
end
end
raise organized_pigeon_data.inspect
# ## David's Solution ##
# pigeon_data.each_pair do |attribute_name, attribute_hash|
# attribute_hash.each_pair do |attribute, name_list|
# name_list.each do |name|
# p attribute_name #why?
# p attribute
# p name
# pigeon_list[name] ||= {}
# pigeon_list[name][attribute_name] ||= []
# pigeon_list[name][attribute_name] << attribute.to_s
# end
# end
# end
## My Solution ##
# # collect_names(pigeon_data)
# names = []
# pigeon_data.each do | attribute, attrib_array |
# attrib_array.each do | property, name|
# names << name
# end
# end
# names.flatten.uniq!
# # create_list(names, pigeon_data)
# pigeon_list = {}
# names.each do |name|
# pigeon_list[name] = { :color => [], :gender => "", :lives => ""}
# pigeon_data.each do | attribute, attrib_array |
# attrib_array.each do | property, pigeons |
# pigeon_list[name][attribute] << property if pigeons.include?(name)
# end
# end
# pigeon_list
# end
# puts "#{pigeon_list}"
# Iterate over the hash above collecting each pigeon by name and insert it
# as the key of a new hash where each name holds the attributes for that bird.
# Your output should match the hash below:
# pigeon_list = {
# "Theo" => {
# :color => ["purple", "grey"],
# :gender => "male",
# :lives => "Subway"
# },
# "Peter Jr." => {
# :color => ["purple", "grey"],
# :gender => "male",
# :lives => "Library"
# },
# "Lucky" => {
# :color => ["purple"],
# :gender => "male",
# :lives => "City Hall"
# },
# "Ms .K" => {
# :color => ["grey", "white"],
# :gender => "female",
# :lives => "Central Park"
# },
# "Queenie" => {
# :color => ["white", "brown"],
# :gender => "female",
# :lives => "Subway"
# },
# "Andrew" => {
# :color => ["white"],
# :gender => "male",
# :lives => "Central Park"
# },
# "Alex" => {
# :color => ["white", "brown"],
# :gender => "male",
# :lives => "Central Park"
# }
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment