Skip to content

Instantly share code, notes, and snippets.

@loganhasson
Last active December 24, 2015 12:59
Show Gist options
  • Save loganhasson/6801242 to your computer and use it in GitHub Desktop.
Save loganhasson/6801242 to your computer and use it in GitHub Desktop.
require 'pry'
require 'awesome_print'
########################
# 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"]
}
}
def sort_pigeons(pigeon_hash)
sorted_pigeons = {}
pigeon_hash.each do |attribute_hash, options|
options.each do |option, names_array|
names_array.each do |bird|
sorted_pigeons[bird] ||= {}
if attribute_hash == :color
sorted_pigeons[bird][:color] ||= []
sorted_pigeons[bird][:color] << option
else
sorted_pigeons[bird][attribute_hash] = option
end
end
end
end
sorted_pigeons
end
sorted_birds = sort_pigeons(pigeon_data)
ap sorted_birds
# Iterate over the hash above collecting each pigeon by name by and rebuild it into the hash below
# that displays the individual attributes of each bird.
# 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 => "Central Park"
# },
# "Ms .K" => {
# :color => ["grey", "white"],
# :gender => "female",
# :lives => "Central Park"
# },
# "Queenie" => {
# :color => ["white", "brown"],
# :gender => "female",
# :lives => "Subway"
# },
# "Andrew" => {
# :color => ["white"],
# :gender => "male",
# :lives => "City Hall"
# },
# "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