Skip to content

Instantly share code, notes, and snippets.

@jamesjtong
Created October 1, 2013 02:45
Show Gist options
  • Save jamesjtong/6773225 to your computer and use it in GitHub Desktop.
Save jamesjtong/6773225 to your computer and use it in GitHub Desktop.
all labs besides hasquetball
movie = {
action: ["Die Hard", "Fight Club"],
fantasy: ["Harry Potter", "Lord of the Rings", "Pokemon"] ,
comedy: ["Scary Movie", "Scary Movie 2", "Grown Ups"] ,
thriller: ["Freddy Kreuger", "Scream", "Texas Chainsaw Massacrew"],
romance: ["The Notebook"]
}
recipes = {
chicken_marsala: ["mushrooms","chicken","white wine", "marsala wine"],
fried_chicken: ["chicken wings", "breading", "flour", "oil", "corn starch"],
strawberry_soy_smoothie: ["strawberries", "soymilk", "soy yogurt", "soy icecream"]
}
user_profiles = {
james: {fav_colors: ["white","blue"], essays: ["essay_1","essay_2","essay_3"]},
john: {fav_colors: ["blue", "black"], essays: ["essay_1", "essay_2", "essay_3"]}
}
#Write a method that takes a sentence and returns it with each word reversed in place.
def reverse_each_word(sentence)
sentence.split.map {|word| word.reverse}.join(" ")
end
=========================================================more labs====================================
#Apple picker
# 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.
def apple_picker(arr)
arr.select {|fruit| fruit == "apple"}
end
def apple_picker(arr)
arr.collect {|fruit| fruit if fruit == "apple"}.compact
end
#select returns the element if the element passes ur block of code
#collect returns the result of everything passed. this is why it will return ["apple",nil,"apple"] without compact
# apple_picker(["apple", "orange", "apple"]) #=> ["apple", "apple"]
# 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
# 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] << "Flags"
# Add a new holiday to any season with supplies.
holiday_supplies[:fall][:halloween] = ["pumpkins", "costumes", "candy"]
# 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}.flatten
end
# Write a loop to list out all the supplies you have for each holiday and the season.
holiday_supplies.each do |season, holidays|
puts "#{season.capitalize}:"
holidays.each do |holiday, supplies|
puts "\t#{holiday.capitalize} : #{supplies}"
end
end
# Output:
# Winter:
# Christmas: Lights and Wreath
# New Years: Party Hats
# Write a method to collect all holidays with BBQ.
def holidays_with_bbqs(holiday_supplies)
bbq_holiday = []
holiday_supplies.each do |season,holidays|
holidays.each do |holiday, supplies|
bbq_holiday << holiday if supplies.include?("BBQ")
end
end
bbq_holiday
end
# holidays_with_bbqs(holiday_supplies) #=> [:fourth_of_july, :memorial_day]
puts winter_suppliers(holiday_supplies)
=======================================================================even more labs ====================
# Deli Counter - Take a Number
# You need to program the "Take a Number" feature for a deli. At the start, the deli is empty and is represented by an empty array.
# Build a method that a new customer will use when entering our deli. The method, take_a_number, should accept the current line of people, along with the new person's name, and return their position in line (and no 0 index, these are normal people, if they are 7th in line, tell them that, not 6).
# Build a method now_serving. This method should call out (via puts) the next person in line and remove them from the line.
# Build a method line that shows people their current place in line.
james_deli = %w(bob joe lisa mona eric james patrick)
def take_a_number(place,name)
place.index(name)+1
end
def now_serving(place)
puts "Currently serving #{place[0].capitalize}"
place.shift
end
def line(place)
place.each_with_index { |person, index| print "#{index+1}. #{person.capitalize} "}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment