Skip to content

Instantly share code, notes, and snippets.

@rosiehoyem
Last active December 24, 2015 09:29
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 rosiehoyem/6777832 to your computer and use it in GitHub Desktop.
Save rosiehoyem/6777832 to your computer and use it in GitHub Desktop.
All homework from Sept 31st.
# 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
movie_collection = {
:comedy => ["Dumb and Dumber", "Billy Madison", "American Pie"],
:action_adventure => ["Indiana Jones", "Die Hard", "Terminator"],
:documentary => ["Man on a Wire", "Exit Through the Gift Shop"]
}
recipes = {
:lasagna => ["pasta", "ricotta cheese", "tomato sauce"],
:tacos => ["hard shells", "meat", "cheese", "salsa"];
:breakfast_of_champions => ["cereal", "milk"]
}
user_profiles = {
:Joe => {
:color => ["blue", "green"],
:essays => ["essay_1", "essay_2", "essay_3"]
},
:Jane => {
:color => ["yellow", "gray", "magenta"],
:essays => ["essay_1", "essay_2", "essay_3"]
},
:Jack => {
:color => ["black"],
:essays => ["essay_1", "essay_2", "essay_3"]
}
}
#Reverse Each World
sentence = "Reverse this sentence."
def reverse_each_word(sentence)
sen_array = sentence.split(" ")
sen_array_rev = sen_array.collect { |x| x.reverse! }
sen_array_rev.join(" ")
end
reverse_each_word(sentence)
#Apple Picker
# 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.
#not working
apple_array = ["apple", "orange", "apple"]
def apple_picker(apple_array)
apple_array.collect { |fruit| fruit if fruit == "apple" }
end.compact
apple_picker(apple_array)
apple_array = ["apple", "orange", "apple"]
def apple_picker(apple_array)
apple_array.select { |fruit| fruit == "apple" }
end
apple_picker(apple_array)
#Holiday Supplies
holiday_supplies = {
:winter => {
:christmas => ["Lights", "Wreath"],
:new_years => ["Party Hats"]
},
:summer => {
:forth_of_july => ["Fireworks", "BBQ"]
},
:fall => {
:thanksgiving => ["Turkey"]
},
:spring => {
:memorial_day => ["BBQ"]
}
}
# 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]
# Add a supply to a Winter holiday.
holiday_supplies[:winter][:christmas] << "Ornaments"
# Add a supply to memorial day.
holiday_supplies[:spring][:memorial_day] << "Lawn Chair"
# Add a new holiday to any season with supplies.
holiday_supplies[:spring][:easter] = ["Easter Eggs", "Chocolate Bunnies"]
# Write a method to collect all Winter supplies from all the winter holidays. ex: winter_suppliers(holiday_supplies) #=> ["Lights", "Wreath", etc]
def winter_supplies(holiday_supplies)
holiday_supplies[:winter].map do |holiday, supplies|
supplies
end.flatten
end
winter_supplies(holiday_supplies)
# Write a loop to list out all the supplies you have for each holiday and the season.
def all_supplies(holiday_supplies)
holiday_supplies.each do |seasons, holidays|
holidays.each do |holiday, supplies|
puts " #{holiday}:#{supplies}"
end.flatten
end
end
all_supplies(holiday_supplies)
# Write a method to collect all holidays with BBQ.
#holidays_with_bbqs(holiday_supplies) #=> [:fourth_of_july, :memorial_day]
def holidays_with_bbqs(holiday_supplies)
matching_holidays = []
holiday_supplies.each do |season, holiday_hash|
holiday_hash.each do |holiday, supply_array|
matching_holidays << holiday if supply_array.include?("BBQ")
end
end
matching_holidays
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment