Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

we worked with lists of Dish objects. Let's call a list of Dishes a Menu and let's redefine a Restaurant to have a menu instead

we worked with lists of Dish objects. Let's call a list of Dishes a Menu and let's redefine a Restaurant to have a menu instead of just one best dish and price; we'll also define a couple of examples: Restaurant = namedtuple('Restaurant', 'name cuisine phone menu') r1 = Restaurant('Thai Dishes', 'Thai', '334-4433', [Dish('Mee Krob', 12.50, 500), Dish('Larb Gai', 11.00, 450)]) r2 = Restaurant('Taillevent', 'French', '01-44-95-15-01', [Dish('Homard Bleu', 45.00, 750), Dish('Tournedos Rossini', 65.00, 950), Dish("Selle d'Agneau", 60.00, 850)])

(e.1) Write a Python expression that defines r3 as a Restaurant object for the French restaurant Pascal whose phone number is 940-752-0107; they serve escargots for $12.95 (250 cal.), poached salmon for $18.50 (550 cal.), rack of lamb for $24.00 (850 cal.), and marjolaine cake for $8.50 (950 cal.).

(e.2) Write the function Restaurant_first_dish_name that takes a Restaurant as its argument and returns the name of the first dish on the restaurant's menu. Remember to write the examples and expected results (as assert statements) before you write the function; do this for every function, whether we remind you or not. You should include code to check whether the menu has zero dishes and return the empty string if so.

(e.3) Write a function called Restaurant_is_cheap that takes two arguments, a Restaurant and a number, and returns True if the average price of the Restaurant's menu is less than or equal to the number. [Hint: Some of the functions you wrote above can be used here.] As you work on this, you'll find it essential to keep track of what part of the data you're working with: Is it a Restaurant object, a string, a number, a list, a Dish, ...?

(e.4) In fact, counting the whole collection of Restaurants, there are four "layers" to the data in this example: Collection, a list of Restaurant objects. Operations on Collections include collection_new, collection_str, collection_search_by_name, and others from the Restaurants program, plus all the predefined operations on lists (like len, sort, and indexing). Restaurant, a namedtuple with four fields (strings for the name, cuisine, and phone number, and a list for the menu). Operations on Restaurants include ones we defined like restaurant_str, the automatically defined constructor function Restaurant, and predefined ones like _replace. Menu (which we called Dishlist above), a list of Dish objects. We could rename the operations we defined above (menu_display, menu_change_prices, menu_average, and so on); we also have the predefined list operations. Dish, a namedtuple with three fields. We defined some operations on Dish objects above. It helps keep everything straight if you write a separate function for each layer. If we want, for example, to write the function collection_raise_prices that takes a Collection and returns the Collection with the price of every dish at every restaurant raised by $2.50, it will be easiest if collection_raise_prices calls, for each Restaurant, a function restaurant_raise_prices (that takes a restaurant and returns that restaurant with all its prices raised by $2.50). restaurant_raise_prices in turn would call menu_raise_prices, which takes a Menu, applies a function like dish_raise_price to each Dish on the Menu, and returns the modified menu. Create a Collection from the three Restaurants defined above (plus more, if you like). Write the function collection_raise_prices as described above. Simply raise each price by $2.50. Then, write the function collection_change_price that works as described above but takes a second parameter, a percentage by which to change each price (as we did above). Note that you'll have to keep passing the percentage along to each successive function, since it's not until you get to the bottom (the price of an individual Dish) that you'll actually use that number.

(e.5) Write the function collection_select_cheap that takes a Collection and a number and returns a list of all the Restaurants in the collection whose average price is less than or equal to that number. Use the functions described above where appropriate.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions