Skip to content

Instantly share code, notes, and snippets.

@gregeng
Created October 1, 2013 12:03
Show Gist options
  • Save gregeng/6777431 to your computer and use it in GitHub Desktop.
Save gregeng/6777431 to your computer and use it in GitHub Desktop.
Lab 2: Apple Picker Holidays
# Apple Picker
# Skills: Collect and Select
def apple_picker_collect(fruits=["apple", "orange", "apple"])
only_apples = fruits.collect do |x|
x if x == "apple"
end
only_apples.compact!
end
apple_picker_collect
def apple_picker_select(fruits=["apple", "orange", "apple"])
fruits.select do |x|
x if x == "apple"
end
end
apple_picker_select
#With the collection method, you will get nil values whereas select gets rid of them for you.
######
# Holiday Suppliers
# Skills: Hashes, Iteration, Collect
holiday_supplies = {
:winter => {
:christmas => ["Lights", "Wreath"],
:new_years => ["Party Hats"]
},
:summer => {
:forth_of_july => ["Fireworks", "BBQ"]
},
:fall => {
:thanksgiving => ["Turkey"]
},
:spring => {
:memorial_day => ["BBQ"]
}
}
#1 How would you access the second supply for the forth_of_july? ex:
holiday_supplies[:summer][:forth_of_july][1]
#2 Add a supply to a Winter holiday.
holiday_supplies[:winter][:new_years] << "Noise Makers"
#3 Add a supply to memorial day.
holiday_supplies[:spring][:memorial_day] << "Slip N' Slide"
#4 Add a new holiday to any season with supplies.
holiday_supplies[:fall][:thanksgiving] << "Turducken"
#5 Write a method to collect all Winter supplies from all the winter holidays.
def winter_suppliers(holiday_supplies)
list = holiday_supplies[:winter].collect do |holiday, supply_array|
supply_array.collect do |supply|
supply
end
end
list
end
winter_suppliers(holiday_supplies)
#6 Write a loop to list out all the supplies you have for each holiday and the season.
holiday_supplies.each do |season, holidays|
season = season.capitalize
puts "#{season}:"
holidays.each do |holiday, supplies|
holiday = holiday.capitalize
supplies = supplies.join(", ")
puts " #{holiday}: #{supplies}"
end
end
#7 Write a method to collect all holidays with BBQ
def holidays_with_bbq(holiday_supplies)
bbq_holidays = []
holiday_supplies.each do |season, holidays|
holidays.each do |holiday, supplies|
if supplies.include?("BBQ")
bbq_holidays << holiday
end
end
end
bbq_holidays
end
holidays_with_bbq(holiday_supplies)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment