Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@samtalks
Created October 1, 2013 13: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 samtalks/6778439 to your computer and use it in GitHub Desktop.
Save samtalks/6778439 to your computer and use it in GitHub Desktop.
# Holiday suppliers
# 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"]
}
}
# How would you access the second supply for the forth_of_july? ex: holiday_supplies[:spring][:memorial_day][0]
puts holiday_supplies[:summer][:forth_of_july][1]
# Add a supply to a Winter holiday.
holiday_supplies[:winter][:christmas] << "Tree"
puts holiday_supplies[:winter]
# Add a supply to memorial day.
holiday_supplies[:spring][:memorial_day] << "Soda"
puts holiday_supplies[:spring]
# Add a new holiday to any season with supplies.
holiday_supplies[:spring][:easter] = ["Eggs"]
puts holiday_supplies[:spring]
# Write a method to collect all Winter supplies from all the winter holidays. ex: winter_suppliers(holiday_supplies) #=> ["Lights", "Wreath", etc]
def winter_suppliers(holiday_supplies)
holiday_supplies[:winter].map {|holiday, supplies| supplies}
end
puts winter_suppliers(holiday_supplies)
# Write a loop to list out all the supplies you have for each holiday and the season.
# Output:
# Winter:
# Christmas: Lights and Wreath
# New Years: Party Hats
holiday_supplies.each do |season, holiday_and_supplies|
puts season.to_s.capitalize + ":"
holiday_and_supplies.each do |holiday, supplies|
new_str = ""
new_str << (" " + holiday.to_s.capitalize + ": ")
i = 0
supplies.each do |supply|
if i == 0
new_str << supply
else
new_str << (" and " + supply)
end
i += 1
end
puts new_str
end
end
# Write a method to collect all holidays with BBQ.
def holidays_with_bbqs(holiday_supplies)
arr = []
holiday_supplies.each do |season, holiday_and_supplies|
holiday_and_supplies.each do |holiday, supplies|
arr << holiday if supplies.include?("BBQ")
end
end
p arr
end
holidays_with_bbqs(holiday_supplies) #=> [:fourth_of_july, :memorial_day]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment