Danny has decided to treat himself to a nice three-course dinner at a local restaurant. As he looks through the menu, he assigns every option a yumminess rating so he can get his favorites. However, when he looks in his wallet, he realizes that he won't be able to afford his favorite selection in all three of the courses (appetizer, main course, and dessert) because he only has $32.
What is the maximum sum of yumminess rating that Danny can select, and still afford to pay for dinner?
Details and assumptions
Danny will eat one selection from each course, so he needs an appetizer, a main course, and a desert.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
if its knapsack problem answer should be 47
The description of this problem did not state what dish belongs to what course. If it is possible to order the same dish three times, then it is possible to get 39 (by ordering the sampler three times). - Actually this problem is a good application of integer linear programming.
Log in to reply
Right! And you have to just guess the course of the dish!
Log in to reply
There's a link to a document that separates the menu items into different categories.
what about this case: from appetizer 1 Quesadilla (6,9) , from main course 1 beans and rice (15,8) and from desert 2 Ice Cream(3,5) and 1 Fried Ice Cream (5,8). the total cost in this case in 32 and total yumminess points is 35. Isn't the answer should be 35?
Log in to reply
The question points out that he eats "1 from each course" therefore he can't have too deserts.
This is a form of the knapsack problem; because there were only a few items, I decided just to bash it:
(How do you enter source code? Mine gets really messed up at any for loop)
It's cool to see these problems that are examples of real problems in computer science (knapsack, traveling salesman, ...)
I don't think this problem is valid. I tried bashing it, but there was no distinction drawn between which foods were appetizers, main course, and desert.
Log in to reply
Open the file named 'Menu and Danny's Ratings'.
Sean, this is how I used for loops. There is probably a more efficient way to do this though :)
app = [[7,10],[9,11],[10,13],[6,9],[4,3]]
main = [[20,13],[25,23],[21,14],[15,8],[18,12]]
des = [[3,5],[4,6],[5,8],[7,9],[8,11]]
def yum_score():
answer_list = []
for i in app:
for x in main:
for z in des:
yum=[]
mon_sum = i[0]+x[0]+z[0] // adds the cost of 1 dessert, 1 appetizer and 1 main course together
yum_sum = i[1]+x[1]+z[1]// adds the yumminess points of 1 dessert, 1 appetizer and 1 main course together
if mon_sum <= 32: // if total cost is less than or equal to $32, the total cost and the total yumminess points are added to the list 'yum'.
yum.append(yum_sum)
yum.append(mon_sum)
answer_list.append(yum)// the list 'yum' is added to another list. This list contains the cost and rating points of all combinations that Danny can afford.
answer_list.sort(reverse = True) // sorting the final list in the descending order of rating points
return answer_list [0] // returns the combination with the highest rating points.
print (yum_score())
The answer is 32 rating points.
Log in to reply
As our main objective is to find the highest satisfactory points possible with 32 bucks in hand, perhaps we don't need array yum. We can set max = 0 before the loop begins, then whereever mon sum <=32 and corresponding yum sum is higher than max, new max will be the current yum_sum.
i got only 31=10+13+8. What are your individual scores ???
Log in to reply
I didn't think the question was very clear on what it was asking (must we have three from each course? Can we have more than one of one course? etc.). 33 is achievable from steak fajitas + nachos. BUt I guess since the answer is 32, that means you must have one from each course?
Now that I know that, I figure that from Appetizer we must hvae at least Quesadilla. That means we can't have Steak Fajitas, so downgrade to Fish Tacos (its 3 dollars less than chicken fajitas but only 2 yumminess less). We have 8 dollars left so let's indulge in a cheese cake.
Log in to reply
This question's almost not clear. "he realizes that he won't be able to afford his favourite selection in all three of the courses" means he want each of them at a max rating, but he can't.
I tried bashing and bashing I get 32 = 9+12+11. I'm not sure if there's better combination.
Log in to reply
@Samuraiwarm Tsunayoshi – "Details and assumptions
Danny will eat one selection from each course, so he needs an appetizer, a main course, and a desert."
Sorry folks, no multiple starters or mains here.
32 is achievable with Quesadilla $6 (9) Fish Tacos $18 (12), and Cheesecake $8 (11)
(It occurs to me now that I probably should have tried writing some code for this rather than just taking 5 minutes to work it out? Bah.)
Log in to reply
appetizers={ ('Nachos',7,10),
('Taquitos',9,11),
('Sampler',10,13),
('Quesadilla',6,9),
('Chips and Salsa',4,3),
('Chimichanga',20,13)}
maincourse={ ('Steak Fajitas',25,23),
('Chicken Fajitas',21,14),
('Beans and Rice',15,8),
('Fish Tacos',18,12)}
desserts={ ('Ice Cream',3,5),
('Milkshake',4,6),
('Fried Ice Cream',5,8),
('Chocolate Cake',7,9),
('Cheesecake', 8,11)}
maxi=0
stuff=''
for a,b,c in appetizers:
for d,e,f in maincourse:
for g,h,i in desserts:
if b+e+h <= 32:
if c+f+i>maxi:
maxi=c+f+i
stuff=", ".join([a,d,g])
print maxi
print stuff
you missed a lot there are another ways that give us 31 but an only way to get 32 it is 9+12+11
34=23+6+5.but even that one is incorrect. cant he have sampler for all the three courses because it is not mentioned that he cant repeat the items.that gives us 39=13+13+13 plus $2 in pocket.
I'm a DBA, so I used SQL to solve this problem ;) If you're not familiar with SQL, I'd advise you to be. It's a really good language and useful for a lot of things...
First, I created three tables, to store the data of Main Courses, Appetizers and Deserts: CREATE TABLE appetizers (name VARCHAR2(30), price NUMBER, yumminess NUMBER); CREATE TABLE main_courses (name VARCHAR2(30), price NUMBER, yumminess NUMBER); CREATE TABLE deserts (name VARCHAR2(30), price NUMBER, yumminess NUMBER);
Then, I inserted the values on each table: --Appetizers: INSERT INTO appetizers VALUES ('Nachos', 7, 10); INSERT INTO appetizers VALUES ('Taquitos', 9, 11); INSERT INTO appetizers VALUES ('Sampler', 10, 13); INSERT INTO appetizers VALUES ('Quesadilla', 6, 9); INSERT INTO appetizers VALUES ('Chips and Salsa', 4, 3);
--Main Courses INSERT INTO main courses VALUES ('Chimichanga', 20, 13); INSERT INTO main courses VALUES ('Steak Fajitas', 25, 23); INSERT INTO main courses VALUES ('Chicken Fajitas', 21, 14); INSERT INTO main courses VALUES ('Beans and Rice', 15, 8); INSERT INTO main_courses VALUES ('Fish Tacos', 18, 12);
--Deserts INSERT INTO deserts VALUES ('Ice Cream', 3, 5); INSERT INTO deserts VALUES ('Milkshake', 4, 6); INSERT INTO deserts VALUES ('Fried Ice Cream', 5, 8); INSERT INTO deserts VALUES ('Chocolate Cake', 7, 9); INSERT INTO deserts VALUES ('Cheescake', 8, 11);
(Don't forget to Commit) COMMIT;
Then, using this SQL query: SELECT * FROM ( SELECT a.name||', '||b.name||', '||c.name foods, a.price + b.price + c.price price, a.yumminess + b.yumminess + c.yumminess yumminess FROM appetizers a, main_courses b, deserts c WHERE a.price + b.price + c.price <= 32 ORDER BY 3 DESC, 2 DESC ) WHERE rownum = 1;
I was able to determine all the sums of all possible combinations of each food, filter only those with price less then or equal to 32, order them decreasingly based on yumminess and price (respectively) and finally just select the first row!
Simple as that ;)
Keep in mind that this setup seems complicated, but is really simple. And it works perfectly for much bigger datasets...
//Here is a solution in C# //
ArrayList ar = new ArrayList();
double sm = 0;
Hashtable ht1 = new Hashtable();
Hashtable ht2 = new Hashtable();
Hashtable ht3 = new Hashtable();
int[] appetizer = { 7, 9, 10, 6, 4 };
ht1["7"] = 10;
ht1["9"] = 11;
ht1["10"] = 13;
ht1["6"] = 9;
ht1["4"] = 3;
int[] maincourse = { 20, 25, 21, 15, 18 };
ht2["20"] = 13;
ht2["25"] = 23;
ht2["21"] = 14;
ht2["15"] = 8;
ht2["18"] = 12;
int[] desert = { 3, 4, 5, 7, 8 };
ht3["3"] = 5;
ht3["4"] = 6;
ht3["5"] = 8;
ht3["7"] = 9;
ht3["8"] = 11;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
for (int k = 0; k < 5; k++)
{
sm = appetizer[i] + maincourse[j] + desert[k];
if (sm > 32)
{
continue;
}
else
{
int aa = int.Parse(ht1[appetizer[i].ToString()].ToString()) + int.Parse(ht2[maincourse[j].ToString()].ToString()) + int.Parse(ht3[desert[k].ToString()].ToString());
if (aa == 32)
{
ar.Add(aa);
}
}
}
}
}
MessageBox.Show(FindMax(ar).ToString());
i = [7,9,10,6,4,10,11,13,9,3]
j = [20,25,21,15,18,13,23,14,8,12]
k = [3,4,5,7,8,5,6,8,9,11]
x = y = z = 0
while x < 5:
y = 0
while y < 5:
z = 0
while z < 5:
A = i[x] + j[y] + k[z]
if A <= 32:
print("yumminess" = i[x+5]+j[y+5]+k[z+5])
z = z + 1
y = y + 1
x = x + 1
Python Solution:
class food(object):
def __init__(self, name, price, yumminess):
self.name = name
self.price = price
self.yumminess = yumminess
appetizers = [food('Nachos', 7, 10),
food('Taquitos', 9, 11),
food('Sampler', 10, 13),
food('Quesadilla', 6, 9),
food('Chips and Salsa', 4, 3)]
main_courses = [food('Chimichanga', 20, 13),
food('Steak Fajitas', 25, 23),
food('Chicken Fajitas', 21, 14),
food('Beans and Rice', 15, 8),
food('Fish Tacos', 18, 12)]
deserts = [food('Ice Cream', 3, 5),
food('Milkshake', 4, 6),
food('Fried Ice Cream', 5, 8),
food('Chocolate Cake', 7, 9),
food('Cheescake', 8, 11)]
bestYumminess = 0
for appetizer in appetizers:
for main_course in main_courses:
for desert in deserts:
if appetizer.price + main_course.price + desert.price <= 32:
totalYumminess = appetizer.yumminess + main_course.yumminess + desert.yumminess
if bestYumminess < totalYumminess:
bestYumminess = totalYumminess
print bestYumminess
>>> 32
Assign each selection an index that equals (yumminess score - price). For example, the steak fajitas have an index of -1 because 24 - $25 = -1. The total yumminess score (if Danny spends $32) is equal to 32 plus the sum of all of the (yumminess score - price) indexes for each selection that Danny picks. The main courses are the most expensive, so we choose one of them and then try to maximize the sum of the (yumminess score - price) indexes for Danny's dessert and appetizer choices while spending $32.
For example, if Danny orders the $20 chimichangas, he has $12 left to spend on appetizer and dessert. The appetizers with the highest (yumminess score - price) index are the $7 nachos, the $10 sampler, and the $6 quesadilla, which all have an index of +3. Similarly, the desserts with the highest (yumminess score - price) index are the $5 fried ice cream and the $8 cheesecake, which are tied at an index of +3. Ideally, we'd like to spend exactly $12 on the dessert and appetizer while choosing from those with the highest index to maximize the yumminess score. We can achieve this by buying the $7 nachos and the $5 fried ice cream, which have a combined index of +6. The chimichangas have an index of -7, so the maximum yumminess score for an order with chimichangas is 32 + 6 - 7 = 31.
Similarly, we can show that the maximum yumminess scores for orders with the steak fajitas, the chicken fajitas, the beans and rice, and the fish tacos are 31, 31, 30, and 32, respectively. Therefore, the maximum yumminess score is 32 , achieved by ordering the fish tacos with the quesadilla to start and a cheesecake for dessert. Yummy!
here is piece of code which finds the best option
App = ([10, 7],[11, 9],[13, 10],[9,6], [3,4])
mc = ([13,20],[23,25],[14,21],[8,15],[12,18])
de = ([5,3],[6,4],[8,5],[9,7],[11,8])
totVal = 0 totYummy = 0 for i in range(0,5): a = App[i] for j in range(0,5): m = mc[j] for k in range(0,5): d = de[k] totYum = a[0]+m[0]+d[0] totVal = a[1]+m[1]+d[1] if totVal <=32 and totYummy < totYum: totYummy = totYum print 'App = ', i, 'mc = ', j, 'de = ', k,'totYum = ', totYum, 'totVal = ', totVal
By appetizers , i took it to mean Nachos, Sampler, Fish Tacos, Chimichanga, Chips and Salsa and taquitos . Dessert of course is pretty clear and the rest are the main dish. So , i arrived at a quotient of yumminess per dollar spent and then sorted appetizers , main course and dessert separately and chose the ones with the highest yumminess quotient. So we get Nachos, Chicken Fajitas and Ice cream but we get total cost = 16 so we can actually have 2 units of each dish giving a yumminess of 24 * 2 = 48
Log in to reply
Sorry, it should be Nachos, Quesidilla and Icecream with a total cost of 7 + 6 + 3 = 16 and so we can have 2 units of each giving a total yumminess of (10 + 9 + 5)*2 = 48. Alternatively, we could also have used a more generalized constrained optimization algorithm such as assignment problem , integer programming problem etc
Problem Loading...
Note Loading...
Set Loading...
python
that prints the answer 3 2