Skip to content

learn-co-students/ruby-enumerables-hash-practice-nyc-pigeon-organizer-lab-sf-web-091619

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NYC Pigeon Organizer

Learning Goals

  • Iterate over nested hashes
  • Sort and manipulate hashes

Introduction

Being able to navigate data is a critical programming skill. Sometimes, data we need isn't in an ideal structure and we need to reorganize it. In this lab, we'll be doing just that - taking a hash, reorganizing the data, and returning that newly structured data as a new hash.

Instructions

NYC Pigeon

You are helping a research group study pigeons in New York City. You are given the following collected data on New York City pigeons in the form of a hash:

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 data above is organized by attributes - :color, :gender, and :lives, each a hash. Within these hashes are arrays of pigeon names.

We want this data, but in a different structure. Rather than sorting by attributes, your task is to create a hash where pigeon names are the keys, with each name pointing to a hash of their attributes.

Iterate over the hash above, collecting each pigeon by name and insert it as the key of a new hash. Each of these hashes should have :color, :gender, and :lives keys assigned to arrays of info about that particular pigeon. Your output should look something like 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 => ["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"]
  }
}

Note: Comparing the original hash with the expected output above, you will see that some of the original keys are symbols, like :male and :female, but in the expected output, these have been converted into strings.

Write your code in the provided nyc_pigeon_organizer method. The tests in this lab will use this method, passing in the original pigeon_data hash. Use learn and the test results to guide your progress.

Alternate Data Set

The goal here is to implement a process for converting any hash data that is in the original structure. The final tests in this lab use a alternate set of data to specifically test for this:

pigeon_data = {
  :color => {
    :brown => ["Luca"],
    :black => ["Lola"],
  },
  :gender => {
    :male => ["Luca"],
    :female => ["Lola"]
  },
  :lives => {
    "Central Park" => ["Lola"],
    "Library" => ["Luca"]
  }
}

Even though the names are different, the hash follows the same format as our original pigeon_data, so passing this hash into the nyc_pigeon_organizer should result in a new hash with "Luca" and "Lola" as the top level keys, pointing to hashes with their particular attributes.

Video Walkthrough

Take some time to try to solve this challenge on your own. However, if you find yourself stuck, check out the video below. As mentioned in the video, there are many possible solutions to this lab. Use it to help you get unstuck - while watching, try to identify the specific steps you were struggling with. If you find the answer you were looking for before the end, keep going with your own solution!

<iframe width="560" height="315" src="https://www.youtube.com/embed/xhBMsclze68" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

Conclusion

Building programs requires data. Whether it is a set of numbers, user input, account information, or info scraped from websites, we need data to make our programs useful. Data, however, is not always organized in the way we want it. Being able to reorganize our data to make it even more useful is a skill we you will frequently use as you build more and more complex applications.

View NYC Pigeon Organizer on Learn.co and start learning to code for free.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages