Skip to content

Instantly share code, notes, and snippets.

@TrevMcKendrick
Created October 3, 2013 05:52
Show Gist options
  • Save TrevMcKendrick/6805576 to your computer and use it in GitHub Desktop.
Save TrevMcKendrick/6805576 to your computer and use it in GitHub Desktop.
########################
# NYC PIGEON ORGANIZER #
########################
# Start with the following collected data on NYC pigeons.
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"]
}
}
pigeon_list = {}
def add_attribute(list,name, category,attribute)
if list[name][category].nil?
list[name][category] = [attribute]
else
list[name][category] << attribute
end
end
pigeon_data.each do |category_key,attribute_value|
attribute_value.each do |attribute_key, names_array_value|
names_array_value.each do |name|
pigeon_list[name] = {} if pigeon_list[name].nil?
add_attribute(pigeon_list,name, category_key,attribute_key)
end
end
end
puts pigeon_list
# pigeon_list = {}
# def set_new_attribute(list, name, category, attribute)
# if list[name][category].nil?
# list[name][category] = [attribute]
# else
# list[name][category] << attribute
# end
# end
# pigeon_data.each do |category, attr_hash|
# attr_hash.each do |attribute, pigeons_with_attr|
# pigeons_with_attr.each do |name|
# pigeon_list[name] = {} if pigeon_list[name].nil?
# set_new_attribute(pigeon_list, name, category, attribute)
# end
# end
# 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