Created
October 3, 2013 03:16
-
-
Save ivanbrennan/6804295 to your computer and use it in GitHub Desktop.
NYC Pigeon Oganizer 2
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 # | |
| ######################## | |
| # Start with the following collected data on NYC pigs. | |
| 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"] | |
| } | |
| } | |
| pigs = {} | |
| pigeon_data.each do |attr, vals| | |
| vals.each do |val, names| | |
| names.each do |name| | |
| pigs[name] ||= {} | |
| # If a pigeon can have multiple values for this attribute | |
| attr_all_names = pigeon_data[attr].values.flatten | |
| if attr_all_names != attr_all_names.uniq | |
| # Push value to existing array OR store value in an array | |
| (pigs[name][attr] && pigs[name][attr] << val.to_s) || | |
| pigs[name][attr] = [val.to_s] | |
| else | |
| pigs[name][attr] = val.to_s | |
| end | |
| end | |
| end | |
| end | |
| # 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 attrs for that bird. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment