Skip to content

Instantly share code, notes, and snippets.

@ahimmelstoss
Created October 11, 2013 18:50
Show Gist options
  • Save ahimmelstoss/6940040 to your computer and use it in GitHub Desktop.
Save ahimmelstoss/6940040 to your computer and use it in GitHub Desktop.
currently refactoring
# 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"]
}
}
#property
#value
#birds
#1. collect all names
#2. put these names in hash as the keys, with everything else as nested hash values
#3. get attribute keys from original hash
organized_pigeon_data = {}
pigeon_data.each do |property, value_hash| #declare two variables; one puts it into an array
value_hash.each do |value, bird_names| #break it down through iteration and make it mirror the structre of hash
bird_names.each do |name| #at the bird_names level, iterate over each...
# pigeon_list[bird_names] ||= {}
# pigeon_list[bird_names][property] ||= []
# pigeon_list[bird_names][property] << value.to_s
if organized_pigeon_data[name]
if organized_pigeon_data[name][property] #(can use property from above)
organized_pigeon_data[name][property] << value #add more values (account for all colors)
else
organized_pigeon_data[name][property] = [value] #add a new value if not there already
end
else
organized_pigeon_data[name] = {property => [value]}
end
end
end
end
raise organized_pigeon_data.inspect
#variable ||= "test" will assign if it doesn't exist yet
#pull out colors
#pull out genders
#pull out locations
# def find_names(pigeon_data)
# pigeon_names = []
# pigeon_data[:gender].each do |gender, names| #gender has only one instance of a name
# pigeon_names << names
# end
# pigeon_names.flatten! #flatten to same level
# end
# def find_colors(pigeon_data, name)
# pigeon_colors = []
# pigeon_data[:colors].each do |color, names|
# names.each do |pigeon_name|
# pigeon_colors << color if pigeon_name == name
# end
# end
# pigeon_colors
# end
# def find_location(pigeon_data, name)
# pigeon_location = []
# pigeon_data[:location].each do |location, names|
# names.each do |pigeon_name|
# pigeon_location << lives if pigeon_name == name
# end
# end
# pigeon_location
# end
# def find_gender(pigeon_data, name)
# pigeon_gender = []
# pigeon_data[:gender].each do |gender, names|
# names.each do |pigeon_name|
# pigeon_gender << gender.to_s if pigeon_name == name
# end
# end
# pigeon_gender
# end
# def make_pigeon_list
# pigeon_list = {}
# find_names(pigeon_data).each do |pigeon_name| #over the new pigeon_names array, add to each pigeon as a key...
# pigeon_list[pigeon_name] = {}
# 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