Created
October 11, 2013 15:13
-
-
Save TrevMcKendrick/6936427 to your computer and use it in GitHub Desktop.
This file contains 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
# Create Some Hashes | |
## Create Hashes for the following use-cases. | |
# - A movie collection that organizes by genres | |
# - Recipes with ingredients | |
# - User profiles where each user has a list of favorite colors along with 3 personal essays, essay_1, essay_2, essay_3 | |
# Just be creative, create a bunch of fake data just for the practice of how you would store this data in a structured hash. Feel free to create a single file, hashes.rb. There are really no wrong answers - there are just more logical ways of storing this sort of data. | |
movies = { | |
:drama => ["Good Will Hunting", "Saving Private Ryan", "Shawshenk Redemption"], | |
:comedy => ["The Sting", "Dumb and Dumber", "Bandits"], | |
:musical => ["The King and I", "Brigadoon", "Damn Yankees", "Anything Goes"] | |
} | |
recipes = { | |
:pancakes => ["Syrup", "Flour", "Butter", "Powdered Sugar", "Water","Milk"], | |
:cereal => ["Milk","Bowl","Cereal"] | |
} | |
users = { | |
:name => | |
{ :favorite_color => "Black", :essay => ["essay_1", "essay_2", "essay_3"] } | |
} | |
# # # Reverse Each Word: | |
# # *From [Rubeque](http://rubeque.com/problems/reverse-each-word)* | |
def reverse_words(sentence) | |
sentence_array = sentence.split(" ") | |
reversed_sentence = sentence_array.collect do |word| | |
word.reverse | |
end | |
reversed_sentence.join(" ") | |
end | |
# Write a method that takes a sentence and returns it with each word reversed in place. | |
# *Note: This is not the vanilla reverse method* | |
# ## Skills | |
# collect/map | |
# ```ruby | |
# def reverse_each_word(sentence) | |
# # do your magic here | |
# end | |
# # reverse_each_word("Hello there, and how are you?") | |
# #=> "olleH ,ereht dna woh era ?uoy" | |
#--------------------------------------------------------------------- | |
# Apple Picker | |
## Skills: Collect and Select | |
## Instructions | |
# Create a method, apple_picker, that will pick all the apples out of an | |
# array. Implement it with collect and then implement it with select. | |
# Write a sentence about how select differs from collect. | |
# ```ruby apple_picker(["apple", "orange", "apple"]) #=> ["apple", | |
# "apple"] ``` | |
def apple_picker(arr) | |
boolean_array = arr.collect { |fruit| fruit == "apple" } | |
boolean_array.delete_if { |value| value == false } | |
boolean_array.collect { "apple" } | |
end | |
def apple_picker2(arr) | |
arr.select { |fruit| fruit == "apple"} | |
end | |
# Collect returns a new array whose elements are the result of whatever was performed by the block on the other array's elements | |
# Select returns a new array whose elements match what was given in the block | |
# Maybe Select is a more specific version of Collect? | |
# # Holiday Suppliers | |
# ## Skills: Hashes, Iteration, Collect | |
# You have a bunch of decorations for various holidays organized by | |
# season. | |
holiday_supplies = { | |
:winter => { | |
:christmas => ["Lights", "Wreath"], | |
:new_years => ["Party Hats"] | |
}, | |
:summer => { | |
:forth_of_july => ["Fireworks", "BBQ"] | |
}, | |
:fall => { | |
:thanksgiving => ["Turkey"] | |
}, | |
:spring => { | |
:memorial_day => ["BBQ"] | |
} | |
} | |
## Questions | |
# 1. How would you access the second supply for the forth_of_july? ex: | |
# `holiday_supplies[:spring][:memorial_day][0]` | |
holiday_supplies[:summer][:forth_of_july][1] | |
# 2. Add a supply to a Winter holiday. | |
holiday_supplies[:winter][:christmas] << "Christmas Tree" | |
# 3. Add a supply to memorial day. | |
holiday_supplies[:spring][:memorial_day] << "US Flag" | |
# 4. Add a new holiday to any season with supplies. | |
holiday_supplies[:fall][:festivus] = ["Large Pole"] | |
holiday_supplies[:fall][:festivus] | |
holiday_supplies | |
# 5. Write a method to collect all Winter supplies from all the winter | |
# holidays. ex: `winter_suppliers(holiday_supplies) #=> ["Lights", | |
# "Wreath", etc]` | |
# def winter_supplies | |
# each_holiday = holiday_supplies[:winter].each do |holiday| | |
# holiday == true | |
# end | |
# puts each_holiday | |
# end | |
# winter_supplies | |
# 6. Write a loop to list out all the supplies you have for each holiday | |
# and the season. | |
holiday_supplies.each do |season, holiday| | |
puts "The season is: #{season}" | |
holiday.each do |holiday, supplies| | |
puts "The holiday is: #{holiday}" | |
supplies.each do |supplies| | |
puts "Supplies are: #{supplies}" | |
end | |
end | |
end | |
# Output: ``` Winter: Christmas: Lights and Wreath New Years: Party | |
# Hats ``` | |
# 7. Write a method to collect all holidays with BBQ. | |
# `holidays_with_bbqs(holiday_supplies) #=> [:fourth_of_july, | |
# :memorial_day]` # ``` | |
def BBQ_baby(hash) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment