147k views
20 votes
Write a Python program to solve the problem described above. Define a function satisfactory_meal(Meal) which takes a single parameter, Meal, and returns True or False. Meal is a list of lists of the form [Dish_number, Organic, Has_dairy, Has_meat, Locally_sourced] where the Dish_number is a positive integer and the other list items are 1 or 0. The function should return True if the meal is satisfactory, and should return False otherwise. You may assume that the Meal list will be in the correct format when the function is called; you do not have to error-check for a non-list or an incorrectly formed list. The dishes in list Meal are not necessarily sorted by dish numbers or any other order. In your program, you may write and call any additional functions that are helpful in the computation. Examples: satisfactory_meal([ [8, 0, 0, 0, 1], [9, 1, 1, 0, 1], [23, 1, 0, 0, 1], [2, 1, 0, 1, 0], [6, 0, 0, 1, 1] ]) should return True, satisfactory_meal([ [4, 1, 0, 0, 0], [7, 0, 1, 0, 1], [90, 0, 0, 0, 0], [3, 0, 0, 1, 1] ]) should return False, and satisfactory_meal([ ]) should return False.

User Oded Harth
by
3.0k points

1 Answer

10 votes

def dx(fn, x, delta=0.001):

return (fn(x+delta) - fn(x))/delta

def solve(fn, value, x=0.5, maxtries=1000, maxerr=0.00001):

for tries in xrange(maxtries):

err = fn(x) - value

if abs(err) < maxerr:

return x

slope = dx(fn, x)

x -= err/slope

raise ValueError('no solution found')

User Penguino
by
3.3k points