Last active
December 24, 2015 13:19
-
-
Save rayning0/6803670 to your computer and use it in GitHub Desktop.
NYC Pigeon Organizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ######################## | |
| # 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"] | |
| } | |
| } | |
| pigeon_list = {} | |
| pkeys = pigeon_data.keys | |
| results = [] | |
| (0..pkeys.size - 1).each do |i| | |
| pigeon_data[pkeys[i]].each do |key2, namearray| | |
| namearray.each do |name| | |
| results << [name, pkeys[i], pigeon_data[pkeys[i]].key(namearray)] | |
| end | |
| end | |
| end | |
| p results | |
| puts | |
| # Used same hash making algorithm as the Artist-Album-Song algorithm | |
| # in Lecture 3: Part 7, Second Program Refactor | |
| # https://github.com/flatiron-school/003-ruby-lectures/blob/master/lecture-3/part7.second_program_refactor.rb | |
| results.each do |person| | |
| if pigeon_list[person[0]] && pigeon_list[person[0]][person[1]] | |
| pigeon_list[person[0]][person[1]] << person[2] | |
| elsif pigeon_list[person[0]] | |
| pigeon_list[person[0]][person[1]] = person[2] | |
| else | |
| pigeon_list[person[0]] = {person[1] => [person[2]]} | |
| end | |
| end | |
| puts pigeon_list | |
| =begin ---------------OUTPUT-------------- | |
| [["Theo", :color, :purple], ["Peter Jr.", :color, :purple], ["Lucky", :color, :purple], ["Theo", :color, :grey], | |
| ["Peter Jr.", :color, :grey], ["Ms .K", :color, :grey], ["Queenie", :color, :white], ["Andrew", :color, :white], | |
| ["Ms .K", :color, :white], ["Alex", :color, :white], ["Queenie", :color, :brown], ["Alex", :color, :brown], | |
| ["Alex", :gender, :male], ["Theo", :gender, :male], ["Peter Jr.", :gender, :male], ["Andrew", :gender, :male], | |
| ["Lucky", :gender, :male], ["Queenie", :gender, :female], ["Ms .K", :gender, :female], ["Theo", :lives, "Subway"], | |
| ["Queenie", :lives, "Subway"], ["Alex", :lives, "Central Park"], ["Ms .K", :lives, "Central Park"], | |
| ["Lucky", :lives, "Central Park"], ["Peter Jr.", :lives, "Library"], ["Andrew", :lives, "City Hall"]] | |
| {"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"}} | |
| --------------------------------------------- | |
| # 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" | |
| # } | |
| # } | |
| =end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment