Created
April 14, 2015 03:22
-
-
Save tgoldenberg/ecd83ff77c0b4466c9de 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
def bakery_num(num_of_people, fav_food) | |
#Release 2 - Initializing the hash and the variables of the amount of food. | |
my_list = {"pie" => 8, "cake" => 6, "cookie" => 1} | |
pie_qty = 0 | |
cake_qty = 0 | |
cookie_qty = 0 | |
has_fave = false | |
# Release 3 | |
# This block of code is pointless. Not a good way to set a variable when it already is set. | |
# Release 4 | |
raise ArgumentError.new("You can't make that food") unless my_list.has_key?(fav_food) | |
# Release 5 | |
# Finding the fav_food in the array and setting the variable fav_food_qty to it. | |
fav_food_qty = my_list[fav_food] | |
# Release 6 | |
if num_of_people % fav_food_qty == 0 | |
num_of_food = num_of_people / fav_food_qty | |
return "You need to make #{num_of_food} #{fav_food}(s)." | |
end | |
# Release 7 | |
# The while loop is to first give out pies, and if there is a remainder, give a cake, and for those left over, cookies | |
# Release 8 | |
case fav_food | |
when 'cake' then cake_qty += (num_of_people / my_list["cake"]) | |
num_of_people %= my_list["cake"] | |
when 'pie' then pie_qty += (num_of_people / my_list["pie"]) | |
num_of_people %= my_list["pie"] | |
when 'cookie' then cookie_qty += (num_of_people / my_list["cookie"]) | |
num_of_people %= my_list["cookie"] | |
end | |
while num_of_people >= 8 | |
pie_qty += 1 | |
num_of_people -= 8 | |
end | |
while num_of_people >= 6 | |
cake_qty += 1 | |
num_of_people -= 6 | |
end | |
while num_of_people >= 1 | |
cookie_qty += 1 | |
num_of_people -= 1 | |
end | |
return "You need to make #{pie_qty} pie(s), #{cake_qty} cake(s), and #{cookie_qty} cookie(s)." | |
end | |
#----------------------------------------------------------------------------------------------------- | |
# Release 1: DRIVER TEST CODE | |
# DO NOT MODIFY ANYTHING BELOW THIS LINE (except in the section at the bottom) | |
# These are the tests to ensure it's working. | |
# These should all print true if the method is working properly (except for the last one). | |
p bakery_num(24, "cake") == "You need to make 4 cake(s)." | |
p bakery_num(41, "pie") == "You need to make 5 pie(s), 0 cake(s), and 1 cookie(s)." | |
p bakery_num(24, "cookie") == "You need to make 24 cookie(s)." | |
p bakery_num(4, "pie") == "You need to make 0 pie(s), 0 cake(s), and 4 cookie(s)." | |
p bakery_num(130, "pie") == "You need to make 16 pie(s), 0 cake(s), and 2 cookie(s)." | |
# p bakery_num(3, "apples") # this will raise an ArgumentError | |
# You SHOULD change this driver test code. Why? Because it doesn't make sense. | |
p bakery_num(41, "cake") == "You need to make 0 pie(s), 6 cake(s), and 5 cookie(s)." # WHAAAAAT? I thought I said I wanted cake! | |
# Reflection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment