Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Lab 5 ICS 31 Section 7
- #Edward Kim 21732671
- #Elena Adame 76808246
- #Part C
- print ("------Part C-------")
- '''
- def Dish(n:str, p:int, c:int):
- '''
- #Part C1
- from collections import namedtuple
- Dish = namedtuple("Dish", "name price calories")
- d1 = Dish("pizza", 10, 300)
- d2 = Dish("cake", 1, 10)
- d3 = Dish("sushi", 3, 50)
- #Part C2
- def Dish_str(d:Dish):
- return d.name+'($'+str(d.price)+ '):'+ str(d.calories)+ 'cal'
- #Part C3
- def Dish_same(d1:Dish, d2:Dish):
- if d1.calories == d2.calories:
- if d1.name == d2.name:
- return True
- else:
- return False
- #Part C4
- def Dish_change_price(d:Dish, n:int):
- d = d._replace(price = d.price + (d.price*(n/100)))
- return d
- #Part C5
- def Dish_is_cheap(d:Dish, n:int):
- return d.price < n
- #Part C6
- DL = [
- Dish("Pasta", 15, 450),
- Dish("Tacos", 2, 100),
- Dish("Pie", 3, 250),
- Dish("Hamburger", 5, 500),
- Dish("Shrimp", 10, 300)]
- DL2 = [
- Dish("Sandwich", 4, 150),
- Dish("Quesadilla", 2, 375),
- Dish("Bacon", 25, 700),
- Dish("Dumplings", 7, 178)]
- DL.extend(DL2)
- def Dishlist_display(l:list):
- result = ''
- for d in l:
- result+=(Dish_str(d)+ '\n')
- return result
- #Part C7
- def Dishlist_all_cheap(l:list,n:int):
- for d in l:
- if not Dish_is_cheap(d, n):
- return False
- return True
- #Part C8
- def Dishlist_change_price(l:list, n:int):
- result = []
- for d in l:
- d = Dish_change_price(d, n)
- result.append(d)
- return result
- #Part C9
- def Dishlist_prices(l:list):
- result = []
- for d in l:
- result.append(d.price)
- return result
- #Part C10
- def Dishlist_average(l:list):
- total = 0
- for d in l:
- total += d.price
- return total/len(l)
- #Part C11
- def Dishlist_keep_cheap(l:list, n:int):
- result = []
- for d in l:
- if Dish_is_cheap(d, n):
- result.append(d)
- return result
- #Part C12
- D25 = [Dish('Chicken', 3, 400),
- Dish('Enchilidas', 10, 400),
- Dish('Steak', 30, 600),
- Dish('Sandwich', 10, 300),
- Dish('Pizza', 8, 700),
- Dish('Shrimp', 30, 200),
- Dish('Salad', 10, 50),
- Dish('Potstickers', 10, 200),
- Dish('Pancakes', 8, 250),
- Dish('Waffles', 100, 100),
- Dish('Sushi', 18, 150),
- Dish('Cake', 15, 1000),
- Dish('Donuts', 1, 150),
- Dish('Corn', 2, 100),
- Dish('Hot Dog', 6, 400),
- Dish('French Toast', 10, 300),
- Dish('Tofu', 5, 100),
- Dish('Chicken n Waffles', 40, 500),
- Dish("Chocolate", 4, 150),
- Dish("Quesadilla", 2, 375),
- Dish("Bacon", 25, 700),
- Dish("Dumplings", 7, 178),
- Dish("Fish 'n Chips", 12, 476),
- Dish("Chicken Cobob", 7, 300),
- Dish("Fruit Salad", 5, 76)]
- def before_and_after():
- n = int(input("Input a number to represent a percentage change in prices:"))
- print (Dishlist_display(D25))
- d = Dishlist_change_price(D25, n)
- print ("")
- print (Dishlist_display(d))
- #before_and_after()
- #Part D
- print ("-----Part D------")
- '''
- __author__ = 'dgk'
- # RESTAURANT COLLECTION PROGRAM
- # ICS 31, UCI, David G. Kay, Fall 2012
- # Implement Restaurant as a namedtuple, collection as a list
- ##### MAIN PROGRAM (CONTROLLER)
- def restaurants(): # nothing -> interaction
- """ Main program
- """
- print("Welcome to the restaurants program!")
- our_rests = Collection_new()
- our_rests = handle_commands(our_rests)
- print("\nThank you. Good-bye.")
- MENU = """
- Restaurant Collection Program --- Choose one
- n: Add a new restaurant to the collection
- r: Remove a restaurant from the collection
- s: Search the collection for selected restaurants
- p: Print all the restaurants
- e: Remove (erase) all the restaurants from the collection
- c: Change prices for the dishes served
- q: Quit
- """
- def handle_commands(C: list) -> list:
- """ Display menu, accept and process commands.
- """
- while True:
- response = input(MENU)
- if response=="q":
- return C
- elif response=='n':
- r = Restaurant_get_info()
- C = Collection_add(C, r)
- elif response=='r':
- n = input("Please enter the name of the restaurant to remove: ")
- C = Collection_remove_by_name(C, n)
- elif response=='p':
- print(Collection_str(C))
- elif response=='s':
- n = input("Please enter the name of the restaurant to search for: ")
- for r in Collection_search_by_name(C, n):
- print(Restaurant_str(r))
- elif response =='e':
- C = result = []
- elif response =='c':
- n = int(input("Please enter the percentage amount to change the prices: "))
- C = Collection_change_prices(C, n)
- else:
- invalid_command(response)
- def invalid_command(response): # string -> interaction
- """ Print message for invalid menu command.
- """
- print("Sorry; '" + response + "' isn't a valid command. Please try again.")
- ##### Restaurant
- from collections import namedtuple
- Restaurant = namedtuple('Restaurant', 'name cuisine phone dish price')
- # Constructor: r1 = Restaurant('Taillevent', 'French', '01-11-22-33-44', 'Escargots', 23.50)
- def Restaurant_str(self: Restaurant) -> str:
- return (
- "Name: " + self.name + "\n" +
- "Cuisine: " + self.cuisine + "\n" +
- "Phone: " + self.phone + "\n" +
- "Dish: " + self.dish + "\n" +
- "Price: ${:2.2f}".format(self.price) + "\n\n")
- def Restaurant_get_info() -> Restaurant:
- """ Prompt user for fields of Restaurant; create and return.
- """
- return Restaurant(
- input("Please enter the restaurant's name: "),
- input("Please enter the kind of food served: "),
- input("Please enter the phone number: "),
- input("Please enter the name of the best dish: "),
- float(input("Please enter the price of that dish: ")))
- #### COLLECTION
- # A collection is a list of restaurants
- def Collection_new() -> list:
- #Return a new, empty collection
- return [ ]
- def Collection_str(C: list) -> str:
- #Return a string representing the collection
- s = ""
- for r in C:
- s = s + Restaurant_str(r)
- return s
- def Collection_search_by_name(C: list, name: str) -> list:
- """ Return list of Restaurants in input list whose name matches input string.
- """
- result = [ ]
- for r in C:
- if r.name == name:
- result.append(r)
- return result
- # alternative (using a list comprehension):
- # return [r for r in C if r.name == name]
- def Collection_add(C: list, R: Restaurant) -> list:
- """ Return list of Restaurants with input Restaurant added at end.
- """
- C.append(R)
- return C
- def Collection_remove_by_name(C: list, name: str) -> list:
- """ Given name, remove all Restaurants with that name from collection.
- """
- result = [ ]
- for r in C:
- if r.name != name:
- result.append(r)
- return result
- # Alternative:
- # return [r for r in self.rests if r.name != name]
- def Restaurant_change_price(restaurant:Restaurant, change:int)->list:
- restaurant = restaurant._replace(price = restaurant.price + (restaurant.price*(change/100)))
- return restaurant
- def Collection_change_prices(C:list, change:int)->list:
- result = []
- for r in C:
- result.append(Restaurant_change_price(r, change))
- return result
- restaurants()
- '''
- #Part E
- print ('------Part E-------')
- 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)])
- #Part E1
- r3 = Restaurant('Pascal', 'French', '940-752-0107', [Dish('Escargots', 12.95, 250),
- Dish('Poached Salmon', 18.50, 550),
- Dish('Rack of Lamb', 24.00, 850),
- Dish('Marjolaine Cake', 8.50, 950)])
- #Part E2
- def Restaurant_first_dish(r:Restaurant)->str:
- return r.menu[0].name
- #Part E3
- def Restaurant_is_cheap(r:Restaurant, n:int):
- return Dishlist_average(r.menu) < n
- #Part E4
- Collection = [r1, r2, r3]
- def Collection_change_price(c:Collection, change:int):
- result = []
- for r in c:
- for d in r.menu:
- Dish_change_price(d, change)
- result.append(r)
- return result
- print (Collection,"\n")
- print (Collection_change_price(Collection, 100))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement