Skip to content

Instantly share code, notes, and snippets.

@sferik
Last active December 15, 2015 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sferik/5340078 to your computer and use it in GitHub Desktop.
Save sferik/5340078 to your computer and use it in GitHub Desktop.

WDI Homework - April 8, 2013

Array and Hash access

Given the following data structure:
a = ["Anil", "Erik", "Jonathan"]
  • How would you return the string "Erik"?
  • How would you add your name to the array?
Given the following data structure:
h = {0 => "Zero", 1 => "One", :two => "Two", "two" => 2}
  • How would you return the string "One"?
  • How would you return the string "Two"?
  • How would you return the number 2?
  • How would you add {3 => "Three"} to the hash?
  • How would you add {:four => 4} to the hash?
Given the following data structure:
is = {true => "It's true!", false => "It's false"}
  • What is the return value of is[2 + 2 == 4]?
  • What is the return value of is["Erik" == "Jonathan"]?
  • What is the return value of is[9 > 10]?
  • What is the return value of is[0]?
  • What is the return value of is["Erik"]?
Given the following data structure:
users = {
  "Jonathan" => {
    :twitter => "tronathan",
    :favorite_numbers => [12, 42, 75],
  },
  "Erik" => {
    :twitter => "sferik",
    :favorite_numbers => [8, 12, 24],
  },
  "Anil" => {
    :twitter => "bridgpal",
    :favorite_numbers => [12, 14, 85],
  },
}
  • How would you access Jonathan's Twitter handle (i.e. the string "tronathan")?
  • How would you add the number 7 to Erik's favorite numbers?
  • How would you add yourself to the users hash?
  • How would you return the array of Erik's favorite numbers?
  • How would you return the smallest of Erik's favorite numbers?
  • How would you return an array of Anil's favorite numbers that are also even?
  • How would you return an array of the favorite numbers common to all users?
  • How would you return an array containing all users' favorite numbers, sorted, and excluding duplicates?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment