Skip to content

Instantly share code, notes, and snippets.

@gregeng
Created October 3, 2013 02:51
Show Gist options
  • Save gregeng/6804039 to your computer and use it in GitHub Desktop.
Save gregeng/6804039 to your computer and use it in GitHub Desktop.
NYC Pigeon Organizer
########################
# NYC PIGEON ORGANIZER #
########################
# Start with the following collected data on NYC pigeons.
require 'pry'
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"]
}
}
# the pigeon names
def pigeon_names(pigeon_data)
all_pigeon_names = pigeon_data.collect do |attribute, attribute_hash|
attribute_hash.collect do |spec_attribute, names|
names.each do |name|
name
end
end
end
all_pigeon_names = all_pigeon_names.flatten.uniq
end
def pigeons_hash_by_name(pigeon_data)
pigeon_list = {}
all_pigeon_names = pigeon_names(pigeon_data)
all_pigeon_names.each do |name|
pigeon_list[name] = {}
end
all_pigeon_names.each do |pigeon|
pigeon_data.each do |attribute, spec_attribute|
spec_attribute.each do |data, names|
if attribute == :color
if names.include?(pigeon)
if pigeon_list[pigeon][attribute].nil?
pigeon_list[pigeon][attribute]=[data.to_s]
else
pigeon_list[pigeon][attribute]<<data.to_s
end
end
else
if names.include?(pigeon)
if pigeon_list[pigeon][attribute].nil?
pigeon_list[pigeon][attribute] = data.to_s
end
end
end
end
end
end
pigeon_list
end
pigeons_hash_by_name(pigeon_data)
# first done by each attribute, then combined into one method
# all_pigeon_names.each do |pigeon|
# pigeon_data[:color].each do |color, names|
# if names.include?(pigeon)
# if pigeon_list[pigeon][:color].nil?
# pigeon_list[pigeon][:color]=[color.to_s]
# else
# pigeon_list[pigeon][:color]<<color.to_s
# end
# end
# end
# end
# all_pigeon_names.each do |pigeon|
# pigeon_data[:gender].each do |gender, names|
# if names.include?(pigeon)
# if pigeon_list[pigeon][:gender].nil?
# pigeon_list[pigeon][:gender]= gender.to_s
# end
# end
# end
# end
# all_pigeon_names.each do |pigeon|
# pigeon_data[:lives].each do |location, names|
# if names.include?(pigeon)
# if pigeon_list[pigeon][:lives].nil?
# pigeon_list[pigeon][:lives]=location.to_s
# end
# end
# end
# end
# 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 => "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