Skip to content

Instantly share code, notes, and snippets.

@mattmarcello
Created October 3, 2014 14:51
Show Gist options
  • Save mattmarcello/b4749ada5fb09a48bf14 to your computer and use it in GitHub Desktop.
Save mattmarcello/b4749ada5fb09a48bf14 to your computer and use it in GitHub Desktop.

What do you know about Iteration / Enumeration?

#####(1) Given the following array, use each to enumerate over it, using puts to print this sentence to the console for each element: [THE_NAME] is Mario Batali's fancy [THE_CUISINE] joint.

arr = [
  {name: 'Babbo', cuisine: 'high-priced italian'},
  {name: 'Del Posto', cuisine: 'higher-priced italian'},
  {name: 'Eataly', cuisine: 'disney world of an italian market'},
  {name: 'Otto', cuisine: 'pizza'}
]

# your code here

#####(2) Given the following array, store the sum of its elements in a variable.

arr = [1,1, 2, 3, 5, 8, 13, 21]

#####(3) Given the following array of strings, create a new array that contains the reverse of each string in the original array.

strings = [
  "tahw", "sekam", "uoy", "kniht", "er'uoy", "eht", "eno", "ohw",
  "nac", "hgual", "tuohtiw", "niyrc"
]

# your code here

#####(4) Given the following hash, print each key-value pair as follows.
[BAND_MEMBER] plays the [INSTRUMENT] on Rumours

fleetwood_mac = {
  'Lindsey Buckingham' => 'guitar',
  'Stevie Nicks' => 'vox',
  'Christine McVie' => 'keys',
  'John McVie' => 'bass',
  'Mick Fleetwood' => 'drums'
}

# your code here

#####(5) Create a hash with keys and values that reflect to the count of the characters in the following sentence. For example, the corresponding hash for the phrase "hello, world", would be:

{"h"=>1, "e"=>1, "l"=>3, "o"=>2, ","=>1, " "=>1, "w"=>1, "r"=>1, "d"=>1}
sentence = "Mr. Powers, my job is to acclimatize you to the nineties. You know, a lot's changed since 1967."

# your code here

#####(6) Return a hash that is identical to the following...with the exception that its keys and values are capitalized.

hash = {
  l: 'lemur',
  s: 'sloth',
  z: 'zebra'
}

# your code here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment