Advertisement
Guest User

Untitled

a guest
Oct 28th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.89 KB | None | 0 0
  1. #Lab 5 ICS 31 Section 7
  2. #Edward Kim 21732671
  3. #Elena Adame 76808246
  4.  
  5. #Part C
  6. print ("------Part C-------")
  7. '''
  8. def Dish(n:str, p:int, c:int):
  9. '''
  10. #Part C1
  11. from collections import namedtuple
  12.  
  13. Dish = namedtuple("Dish", "name price calories")
  14.  
  15.  
  16. d1 = Dish("pizza", 10, 300)
  17. d2 = Dish("cake", 1, 10)
  18. d3 = Dish("sushi", 3, 50)
  19.  
  20. #Part C2
  21.  
  22. def Dish_str(d:Dish):
  23. return d.name+'($'+str(d.price)+ '):'+ str(d.calories)+ 'cal'
  24.  
  25. #Part C3
  26. def Dish_same(d1:Dish, d2:Dish):
  27. if d1.calories == d2.calories:
  28. if d1.name == d2.name:
  29. return True
  30. else:
  31. return False
  32.  
  33. #Part C4
  34. def Dish_change_price(d:Dish, n:int):
  35. d = d._replace(price = d.price + (d.price*(n/100)))
  36. return d
  37.  
  38. #Part C5
  39. def Dish_is_cheap(d:Dish, n:int):
  40. return d.price < n
  41.  
  42. #Part C6
  43. DL = [
  44. Dish("Pasta", 15, 450),
  45. Dish("Tacos", 2, 100),
  46. Dish("Pie", 3, 250),
  47. Dish("Hamburger", 5, 500),
  48. Dish("Shrimp", 10, 300)]
  49. DL2 = [
  50. Dish("Sandwich", 4, 150),
  51. Dish("Quesadilla", 2, 375),
  52. Dish("Bacon", 25, 700),
  53. Dish("Dumplings", 7, 178)]
  54.  
  55. DL.extend(DL2)
  56.  
  57. def Dishlist_display(l:list):
  58. result = ''
  59. for d in l:
  60. result+=(Dish_str(d)+ '\n')
  61. return result
  62.  
  63.  
  64.  
  65. #Part C7
  66. def Dishlist_all_cheap(l:list,n:int):
  67. for d in l:
  68. if not Dish_is_cheap(d, n):
  69. return False
  70. return True
  71.  
  72. #Part C8
  73. def Dishlist_change_price(l:list, n:int):
  74. result = []
  75. for d in l:
  76. d = Dish_change_price(d, n)
  77. result.append(d)
  78. return result
  79.  
  80. #Part C9
  81. def Dishlist_prices(l:list):
  82. result = []
  83. for d in l:
  84. result.append(d.price)
  85. return result
  86.  
  87. #Part C10
  88. def Dishlist_average(l:list):
  89. total = 0
  90. for d in l:
  91. total += d.price
  92. return total/len(l)
  93.  
  94. #Part C11
  95. def Dishlist_keep_cheap(l:list, n:int):
  96. result = []
  97. for d in l:
  98. if Dish_is_cheap(d, n):
  99. result.append(d)
  100. return result
  101.  
  102. #Part C12
  103. D25 = [Dish('Chicken', 3, 400),
  104. Dish('Enchilidas', 10, 400),
  105. Dish('Steak', 30, 600),
  106. Dish('Sandwich', 10, 300),
  107. Dish('Pizza', 8, 700),
  108. Dish('Shrimp', 30, 200),
  109. Dish('Salad', 10, 50),
  110. Dish('Potstickers', 10, 200),
  111. Dish('Pancakes', 8, 250),
  112. Dish('Waffles', 100, 100),
  113. Dish('Sushi', 18, 150),
  114. Dish('Cake', 15, 1000),
  115. Dish('Donuts', 1, 150),
  116. Dish('Corn', 2, 100),
  117. Dish('Hot Dog', 6, 400),
  118. Dish('French Toast', 10, 300),
  119. Dish('Tofu', 5, 100),
  120. Dish('Chicken n Waffles', 40, 500),
  121. Dish("Chocolate", 4, 150),
  122. Dish("Quesadilla", 2, 375),
  123. Dish("Bacon", 25, 700),
  124. Dish("Dumplings", 7, 178),
  125. Dish("Fish 'n Chips", 12, 476),
  126. Dish("Chicken Cobob", 7, 300),
  127. Dish("Fruit Salad", 5, 76)]
  128.  
  129. def before_and_after():
  130. n = int(input("Input a number to represent a percentage change in prices:"))
  131. print (Dishlist_display(D25))
  132. d = Dishlist_change_price(D25, n)
  133. print ("")
  134. print (Dishlist_display(d))
  135.  
  136. #before_and_after()
  137.  
  138. #Part D
  139. print ("-----Part D------")
  140. '''
  141. __author__ = 'dgk'
  142.  
  143. # RESTAURANT COLLECTION PROGRAM
  144. # ICS 31, UCI, David G. Kay, Fall 2012
  145.  
  146. # Implement Restaurant as a namedtuple, collection as a list
  147.  
  148. ##### MAIN PROGRAM (CONTROLLER)
  149.  
  150. def restaurants(): # nothing -> interaction
  151. """ Main program
  152. """
  153. print("Welcome to the restaurants program!")
  154. our_rests = Collection_new()
  155. our_rests = handle_commands(our_rests)
  156. print("\nThank you. Good-bye.")
  157.  
  158. MENU = """
  159. Restaurant Collection Program --- Choose one
  160. n: Add a new restaurant to the collection
  161. r: Remove a restaurant from the collection
  162. s: Search the collection for selected restaurants
  163. p: Print all the restaurants
  164. e: Remove (erase) all the restaurants from the collection
  165. c: Change prices for the dishes served
  166. q: Quit
  167. """
  168.  
  169. def handle_commands(C: list) -> list:
  170. """ Display menu, accept and process commands.
  171. """
  172. while True:
  173. response = input(MENU)
  174. if response=="q":
  175. return C
  176. elif response=='n':
  177. r = Restaurant_get_info()
  178. C = Collection_add(C, r)
  179. elif response=='r':
  180. n = input("Please enter the name of the restaurant to remove: ")
  181. C = Collection_remove_by_name(C, n)
  182. elif response=='p':
  183. print(Collection_str(C))
  184. elif response=='s':
  185. n = input("Please enter the name of the restaurant to search for: ")
  186. for r in Collection_search_by_name(C, n):
  187. print(Restaurant_str(r))
  188. elif response =='e':
  189. C = result = []
  190. elif response =='c':
  191. n = int(input("Please enter the percentage amount to change the prices: "))
  192. C = Collection_change_prices(C, n)
  193. else:
  194. invalid_command(response)
  195.  
  196. def invalid_command(response): # string -> interaction
  197. """ Print message for invalid menu command.
  198. """
  199. print("Sorry; '" + response + "' isn't a valid command. Please try again.")
  200.  
  201.  
  202.  
  203.  
  204. ##### Restaurant
  205. from collections import namedtuple
  206. Restaurant = namedtuple('Restaurant', 'name cuisine phone dish price')
  207. # Constructor: r1 = Restaurant('Taillevent', 'French', '01-11-22-33-44', 'Escargots', 23.50)
  208.  
  209. def Restaurant_str(self: Restaurant) -> str:
  210. return (
  211. "Name: " + self.name + "\n" +
  212. "Cuisine: " + self.cuisine + "\n" +
  213. "Phone: " + self.phone + "\n" +
  214. "Dish: " + self.dish + "\n" +
  215. "Price: ${:2.2f}".format(self.price) + "\n\n")
  216.  
  217. def Restaurant_get_info() -> Restaurant:
  218. """ Prompt user for fields of Restaurant; create and return.
  219. """
  220. return Restaurant(
  221. input("Please enter the restaurant's name: "),
  222. input("Please enter the kind of food served: "),
  223. input("Please enter the phone number: "),
  224. input("Please enter the name of the best dish: "),
  225. float(input("Please enter the price of that dish: ")))
  226.  
  227.  
  228. #### COLLECTION
  229. # A collection is a list of restaurants
  230.  
  231. def Collection_new() -> list:
  232. #Return a new, empty collection
  233.  
  234. return [ ]
  235.  
  236. def Collection_str(C: list) -> str:
  237. #Return a string representing the collection
  238.  
  239. s = ""
  240. for r in C:
  241. s = s + Restaurant_str(r)
  242. return s
  243.  
  244. def Collection_search_by_name(C: list, name: str) -> list:
  245. """ Return list of Restaurants in input list whose name matches input string.
  246. """
  247. result = [ ]
  248. for r in C:
  249. if r.name == name:
  250. result.append(r)
  251. return result
  252. # alternative (using a list comprehension):
  253. # return [r for r in C if r.name == name]
  254.  
  255. def Collection_add(C: list, R: Restaurant) -> list:
  256. """ Return list of Restaurants with input Restaurant added at end.
  257. """
  258. C.append(R)
  259. return C
  260.  
  261. def Collection_remove_by_name(C: list, name: str) -> list:
  262. """ Given name, remove all Restaurants with that name from collection.
  263. """
  264. result = [ ]
  265. for r in C:
  266. if r.name != name:
  267. result.append(r)
  268. return result
  269. # Alternative:
  270. # return [r for r in self.rests if r.name != name]
  271.  
  272. def Restaurant_change_price(restaurant:Restaurant, change:int)->list:
  273. restaurant = restaurant._replace(price = restaurant.price + (restaurant.price*(change/100)))
  274. return restaurant
  275.  
  276. def Collection_change_prices(C:list, change:int)->list:
  277. result = []
  278. for r in C:
  279. result.append(Restaurant_change_price(r, change))
  280. return result
  281.  
  282. restaurants()
  283.  
  284. '''
  285. #Part E
  286. print ('------Part E-------')
  287.  
  288.  
  289.  
  290. Restaurant = namedtuple('Restaurant', 'name cuisine phone menu')
  291. r1 = Restaurant('Thai Dishes', 'Thai', '334-4433', [Dish('Mee Krob', 12.50, 500),
  292. Dish('Larb Gai', 11.00, 450)])
  293. r2 = Restaurant('Taillevent', 'French', '01-44-95-15-01',
  294. [Dish('Homard Bleu', 45.00, 750),
  295. Dish('Tournedos Rossini', 65.00, 950),
  296. Dish("Selle d'Agneau", 60.00, 850)])
  297. #Part E1
  298. r3 = Restaurant('Pascal', 'French', '940-752-0107', [Dish('Escargots', 12.95, 250),
  299. Dish('Poached Salmon', 18.50, 550),
  300. Dish('Rack of Lamb', 24.00, 850),
  301. Dish('Marjolaine Cake', 8.50, 950)])
  302. #Part E2
  303. def Restaurant_first_dish(r:Restaurant)->str:
  304. return r.menu[0].name
  305.  
  306. #Part E3
  307. def Restaurant_is_cheap(r:Restaurant, n:int):
  308. return Dishlist_average(r.menu) < n
  309.  
  310. #Part E4
  311. Collection = [r1, r2, r3]
  312.  
  313. def Collection_change_price(c:Collection, change:int):
  314. result = []
  315. for r in c:
  316. for d in r.menu:
  317. Dish_change_price(d, change)
  318. result.append(r)
  319. return result
  320.  
  321. print (Collection,"\n")
  322. print (Collection_change_price(Collection, 100))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement