I need help part A1. how do you solve part A1? How is it different from part A2?
ID: 666859 • Letter: I
Question
I need help part A1. how do you solve part A1? How is it different from part A2? I have provided my code for part A2 and A3. thanks! Also, this is python 3.4
link:https://eee.uci.edu/15m/36530/labs/lab8.pdf
print('-----------A2---------')
#input: read the file
#output: a list of Dish
def read_menu(filename:str)->list:
'takes as an argument a string naming a file in this format, reads the file, and returns a list of Dish structures created from the data'
result = []
infile = open(filename,'r')
for line in infile.readlines():
i = line.split(' ')
menu = Dish(i[0],float(i[1].strip('$')), int(i[2].strip(' ')))
result.append(menu)
infile.close()
return result
print(read_menu('menu3.txt'))
print('---------------A3---------------')
#input: a list of Dish namedtuples and a string
#output: wirte the Dish data
def write_menu(lst: list, name:str)->None:
'takes as its argument a list of Dish namedtuples and a string that names a file, write the dish data to the named file'
outfile = open(name,'w')
outfile.write('number of dishes: '+str(len(lst))+' ')
for i in lst:
line = '{:20} ${:5} {:5} '.format(i.name,i.price,i.calories)
outfile.write(line)
outfile.close
Explanation / Answer
Answer:
#
# Part A
#
print()
print('-'*10, 'Part (A)', '-' * 10)
print()
print()
#c.1
from collections import named_tuple
Dish1 = named_tuple('Dish1', 'name price calories')//this is your requirement
d1 = Dish1('Burger', 4.00, 550)
d2 = Dish1('Cake', 10.00, 990)
d3 = Dish1('Pizza', 2.50, 340)
d4 = Dish1('Burger', 5.50, 550)
d5 = Dish1('Burrito', 7.50, 390)
#c.2
def Dish1_str(dish1: Dish1) -> str:
return (dish1.name+' ($ '+str(dish1.price)+' ) : '+str(dish1.calories)+' cal')
assert Dish1_str(d1) == 'Burger ($ 4.0 ) : 550 cal'
assert Dish1_str(d2) == 'Cake ($ 10.0 ) : 990 cal'
print(Dish1_str(d1))
print(Dish1_str(d2))
print(Dish1_str(d3))
print(Dish1_str(d4))
print(Dish1_str(d5))
#c.3
def Dish1_same(dish11: Dish1, dish12: Dish1) -> bool:
return dish11.name == dish12.name and dish11.calories == dish12.calories
assert Dish1_same(d1, d2) == False
assert Dish1_same(d3, d1) == False
assert Dish1_same(d1, d4) == True
#c.4
def Dish1_change_price(dish1: Dish1, num: float) -> Dish1:
return dish1._replace(price = dish1.price + (dish1.price * (num/100)))
assert Dish1_change_price(d1, 100) == Dish1(name='Burger',
price=8.0, calories=550)
assert Dish1_change_price(d2, 100) == Dish1(name='Cake',
price=20.0, calories=990)
#c.5
def Dish1_is_cheap(dish1: Dish1, num: float) -> bool:
return dish1.price < num
assert Dish1_is_cheap(d1, 5) == True
assert Dish1_is_cheap(d2, 0) == False
assert Dish1_is_cheap(d3, 100) == True
#c.6
DL = [ d1, d2, d3, d4, d5]
DL2 = [Dish1('Sandwich', 3.50, 175),
Dish1('Salad', 5.00, 150),
Dish1('Pasta', 7.00, 420),
Dish1('Popcorn', 6.25, 625)]
DL.extend(DL2)
def Dish1list_display(dish1es: [Dish1]) -> str:
result = ''
for i in dish1es:
result += (Dish1_str(i) + ' ')
return result
assert Dish1list_display(DL) ==
'''Burger ($ 4.0 ) : 550 cal
Cake ($ 10.0 ) : 990 cal
Pizza ($ 2.5 ) : 340 cal
Burger ($ 5.5 ) : 550 cal
Burrito ($ 7.5 ) : 390 cal
Sandwich ($ 3.5 ) : 175 cal
Salad ($ 5.0 ) : 150 cal
Pasta ($ 7.0 ) : 420 cal
Popcorn ($ 6.25 ) : 625 cal
'''
print(" ")
print(Dish1list_display(DL))
#c.7
def Dish1list_all_cheap(dish1es: [Dish1], num: float) -> bool:
check = 1
for i in dish1es:
if i.price >= num:
check = 0
break
return check == 1
assert Dish1list_all_cheap(DL, 5) == False
assert Dish1list_all_cheap(DL, 11) == True
print(" ")
num = float(input("Enter the price to check if cheaper than that : "))
print("All the dish1es on the list are cheaper than", num, " : ")
print(Dish1list_all_cheap(DL, num))
#c.8
def Dish1list_change_price(dish1es: [Dish1], num: float) -> [Dish1]:
for i in range(len(dish1es)):
dish1es[i] = dish1es[i]._replace(price = dish1es[i].price +
(dish1es[i].price * (num/100)))
return dish1es
print(" ")
num = float(input("Enter the percentage change in price : "))
Dish1list_change_price(DL, num)
print(Dish1list_display(DL))
#c.9
def Dish1list_prices(dish1es: [Dish1]) -> list:
result = []
for i in dish1es:
result.append(i.price)
return result
# Assertion is only for 100 % increase in prices:
assert Dish1list_prices(DL) == [8.0, 20.0, 5.0, 11.0, 15.0, 7.0, 10.0, 14.0, 12.5]
print(" ")
print("Dish1list prices :")
print(Dish1list_prices(DL))
#c.10
def Dish1list_average(dish1es: [Dish1]) -> float:
total = 0
for i in dish1es:
total += i.price
return total / len(dish1es)
assert Dish1list_average(DL) == 11.38888888888889
print(" ")
print("The average of prices of dish1es on the list : ")
print(Dish1list_average(DL))
#c.11
def Dish1list_keep_cheap(dish1es: [Dish1], num: float) -> [Dish1]:
result = []
for i in dish1es:
if i.price < num:
result.append(i)
return result
assert Dish1list_keep_cheap(DL, 10) ==
[Dish1(name='Burger', price=8.0, calories=550), Dish1(name='Pizza',
price=5.0, calories=340), Dish1(name='Sandwich', price=7.0, calories=175)]
assert Dish1list_keep_cheap(DL, 0) == []
assert Dish1list_keep_cheap([], 100) == []
print(" ")
num = float(input("Enter the number to check for cheaper dish1es : "))
print(" The list of cheaper dish1es is :")
print(Dish1list_display(Dish1list_keep_cheap(DL, num)))
#c.12
DL_new = []
DL_new.extend(DL)
DL_new2 = [Dish1('Cotton Candy', 1.75, 230),
Dish1('Tacos', 2.00, 400),
Dish1('Popcorn Chicken', 3.40, 560),
Dish1('Tater Tots', 6.70, 870),
Dish1('Rice', 1.50, 200),
Dish1('Cookies', 1.46, 135),
Dish1('Chicken Tikka', 8.90, 540),
Dish1('Gyro', 5.00, 470),
Dish1('Walnut Shrimp', 4.50, 560),
Dish1('Grilled Salmon', 11.90, 250),
Dish1('Beijing Beef', 3.50, 420),
Dish1('Brownie', 3.50, 780),
Dish1('Onion Rings', 2.90, 680),
Dish1('Bubble Tea', 2.00, 175),
Dish1('CheeseCake', 7.89, 890),
Dish1('Soup', 3.50, 100)]
DL_new.extend(DL_new2)
def before_and_after():
'''Prompts the user for interactive input of a number representing a
percentage change in prices'''
num = float(input("Enter the percent change in price : "))
print(" The list of dish1es before price change : ")
print(Dish1list_display(DL_new))
print(" The list of dish1es after price change : ")
print(Dish1list_display(Dish1list_change_price(DL_new, num)))
return
print(" ")
before_and_after()
#
#
# Part e
#
print()
print('-' * 10, 'Part (E)', '-' * 10)
print()
Restaurant = named_tuple('Restaurant', 'name cuisine phone menu')
r1 = Restaurant('Thai Dish1es', 'Thai', '334-4433', [Dish1('Mee Krob', 12.50, 500),
Dish1('Larb Gai', 11.00, 450)])
r2 = Restaurant('Taillevent', 'French', '01-44-95-15-01',
[Dish1('Homard Bleu', 45.00, 750),
Dish1('Tournedos Rossini', 65.00, 950),
Dish1("Selle d'Agneau", 60.00, 850)])
r3 = Restaurant('Pascal', 'French', '940-752-0107',
[Dish1('Escargots', 12.95, 250),
Dish1('Poached Salmon', 18.50, 550),
Dish1('Rack of Lamb', 24.00, 850)
])
def Restaurant_first_dish1(rest: Restaurant) -> str:
'''Takes a Restaurant as argument and returns the name of the first dish1
on its menu'''
if len(rest.menu) == 0:
return ''
else:
return rest.menu[0].name
assert Restaurant_first_dish1(r1) == 'Mee Krob'
assert Restaurant_first_dish1(r2) == 'Homard Bleu'
print(" ")
print("Options 1. r1 2. r2 3. r3")
rest = input(" Enter your choice to see first dish1 on menu : ")
if rest == 'r1':
print(Restaurant_first_dish1(r1))
elif rest == 'r2':
print(Restaurant_first_dish1(r2))
elif rest == 'r3':
print(Restaurant_first_dish1(r3))
else:
print("INVALID CHOICE")
print()
#e.3
def Restaurant_is_cheap(rest: Restaurant, num: float) -> bool:
'''Takes two arguments; a Restaurant and a number and returns True if
average price of restaurant's menu is less than or equal to the number
and False otherwise'''
return Dish1list_average(rest.menu) <= num
assert Restaurant_is_cheap(r3, 20) == True
assert Restaurant_is_cheap(r2, 0) == False
assert Restaurant_is_cheap(r1, 5) == False
print()
num = float(input("Enter the number to find cheaper (average price) : "))
print(" Option 1. r1 2. r2 3. r3")
rest = input(" Enter your choice to check if cheaper than number : ")
if rest == 'r1':
print(Restaurant_is_cheap(r1, num))
elif rest == 'r2':
print(Restaurant_is_cheap(r2, num))
elif rest == 'r3':
print(Restaurant_is_cheap(r3, num))
else:
print("INVALID COMMAND")
print()
#e.4.1
C = [r1, r2, r3]
def Dish1_raise_price(dish1: Dish1, num: float) -> Dish1:
return dish1._replace(price = dish1.price + num)
def Menu_raise_prices(dish1es: [Dish1], num: float) -> [Dish1]:
'''Takes a list of Dish1es and returns the list with each Dish1's price
raised by num'''
for i in range(len(dish1es)):
dish1es[i] = Dish1_raise_price(dish1es[i], num)
return dish1es
def Restaurant_raise_prices(rest: Restaurant, num: float) -> Restaurant:
'''Takes a restaurant and returns the restaurant with the price of each
dish1 in the menu raised by num'''
return Menu_raise_prices(rest.menu, num)
def Collection_raise_prices(C: [Restaurant], num: float) -> [Restaurant]:
for i in range(len(C)):
C[i] = Restaurant_raise_prices(C[i], num)
return C
print(" ")
print("The price of all the dish1es at every restaurant is increased by $2.50 :")
C_new = Collection_raise_prices(C, 2.5)
print()
for i in C_new:
print(i)
print()
#e.4.2
def Dish1_change_price(dish1: Dish1, num: float) -> Dish1:
'''Takes a dish1 and returns the dish1 with price increased by the specified
percentage i.e. num'''
return dish1._replace(price = dish1.price + (dish1.price * (num/100)))
def Menu_change_prices(dish1es: [Dish1], num: float) -> [Dish1]:
for i in range(len(dish1es)):
dish1es[i] = Dish1_change_price(dish1es[i], num)
return dish1es
def Restaurant_change_prices(rest: Restaurant, num: float) -> Restaurant:
return Menu_change_prices(rest.menu, num)
def Collection_change_prices(C: [Restaurant], num: float) -> [Restaurant]:
'''Takes a collection of Restaurants and increases the price of every dish1
by the specified percentage'''
for i in range(len(C)):
C[i] = Restaurant_change_prices(C[i], num)
return C
print(" ")
num = float(input("Enter the percent change in price : "))
C_new2 = Collection_change_prices(C_new, num)
for i in C_new2:
print(i)
print()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.