Created
October 3, 2013 01:08
-
-
Save ivanbrennan/6803045 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 # | |
| ######################## | |
| # 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 attr == :color | |
| (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