Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Tom Tan 50152878 and yina li 19829156. ICS 31 Lab sec 11. Lab asst. 5.
- print ()
- #C
- print('-----Part C-----')
- print()
- from collections import namedtuple
- Dish = namedtuple('Dish', 'name price calories')
- #c.1
- print("-----C.1-----")
- d1 = Dish('Sushi', 10.00, 500)
- d2 = Dish('Pasta', 8.00, 1000)
- d3 = Dish('Paht Woon Sen', 9.50, 330)
- print()
- #c.2
- print('-----C.2-----')
- def Dish_str(d: Dish):
- return '{0} (${1}): {2} cal'.format(d.name, d.price, d.calories)
- print(Dish_str(d3))
- print()
- #c.3
- print('-----C.3-----')
- def Dish_same(x: Dish, y: Dish):
- return x.name ==y.name and x.calories == y.calories
- d4= Dish('Sushi', 12.00, 600)
- d5=Dish('Mushroom', 14.50, 1000)
- d6=Dish('Paht Woon Sen', 20.00, 330)
- assert Dish_same(d4, d1) == False
- assert Dish_same(d1, d1) == True
- assert Dish_same(d1, d2) == False
- assert Dish_same(d3, d6) == True
- print()
- #c.4
- print('-----C.4-----')
- def Dish_change_price(D: Dish, x: int):
- D=Dish(D.name, D.price * (1+(x/100)), D.calories)
- return D
- print(Dish_change_price(d1, 100))
- print(Dish_change_price(d2, -50))
- print()
- #c.5
- print('-----C.5-----')
- def Dish_is_cheap(d: Dish, x:int):
- return d.price<x
- assert Dish_is_cheap(d1, 15) == True
- assert Dish_is_cheap(d2, 5) == False
- print()
- #c.6
- print('-----C.6-----')
- d7=Dish('Chips', 1.00, 200)
- d8=Dish('Hamburgers', 5.00, 800)
- DL = [d1, d2, d3, d4, d5]
- print(DL)
- DL2=[d6, d7, d8]
- DL.extend(DL2)
- print(DL)
- def Dishlist_display(d: [Dish]):
- string = ''
- for i in d:
- string += i.name + ' ' + str(i.price) + ' ' + str(i.calories)+'\n'
- return string
- print(Dishlist_display(DL))
- print()
- #c.7
- print('-----C.7-----')
- def Dishlist_all_cheap(d: [Dish], x: int):
- for i in d:
- if Dish_is_cheap(i, x) == False:
- return False
- return True
- assert Dishlist_all_cheap(DL, 25) == True
- assert Dishlist_all_cheap(DL, 15) ==False
- print()
- #c.8
- print('-----C.8-----')
- def Dishlist_change_price(DL:list, n:int):
- new_list =[]
- for i in range(len(DL)):
- new_list += Dish( DL[i].name, DL[i].price*(1+(n/100)), DL[i].calories)
- return new_list
- print(Dishlist_change_price(DL, 100))
- print()
- #c.9
- print('-----C.9-----')
- def Dishlist_prices (DL:list):
- second_list =[]
- for i in DL:
- second_list.append ( i.price)
- return second_list
- print(Dishlist_prices(DL))
- print()
- #c.10
- print('-----C.10-----')
- def Dishlist_average(d: list):
- return sum(Dishlist_prices(d))/len(d)
- print(Dishlist_average(DL))
- print()
- #c.11
- print('-----C.11-----')
- def Dishlist_keep_cheap(d: list, n: int):
- new_list = []
- for i in d:
- if i.price<n:
- new_list.append(i.name)
- return new_list
- print(Dishlist_keep_cheap(DL, 10))
- print()
- #c.12
- d9 = Dish('Tofu', 2.00, 100)
- d10 = Dish('Pie', 5.00, 600)
- d11 = Dish('Ramen', 12.50, 800)
- d12 = Dish('Caviar', 25.00, 750)
- d13 = Dish('Cake', 16.00, 1500)
- d14 = Dish('Waffle', 2.00, 500)
- d15 = Dish('Coffee', 3.50, 300)
- d16 = Dish('BBQ', 7.00, 400)
- d17 = Dish('Rice cake', 11.00, 800)
- d18 = Dish('Egg cake', 5.00, 800)
- d19 = Dish('Donuts', 6.00, 600)
- d20= Dish('Pizza', 9.00, 500)
- d21 = Dish('Burger', 3.00, 400)
- d22 = Dish('Taco', 2.00, 500)
- d23 = Dish('Salad', 8.50, 100)
- d24 = Dish('Bread', 3.50, 1000)
- d25 = Dish('Hot dog', 4.00, 700)
- DL3 =[d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25]
- def before_and_after():
- number = int(input('Percentage: '))
- print(Dishlist_display(DL3))
- for i in range(len(DL3)):
- DL3[i] = DL3[i]._replace(price = DL3[i].price * (1 + (number/100)))
- print(Dishlist_display(DL3))
- return
- print(before_and_after())
- print()
- 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)])
- print()
- print('-----E.1-----')
- 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)])
- print()
- print('-----E.2-----')
- def Restaurant_first_dish_name(r:Restaurant)->list:
- if r.menu == []:
- return ''
- return r.menu[0].name
- R = Restaurant('Pascal', 'French', '940-752-0107', [])
- assert Restaurant_first_dish_name(r3) == 'Escargots'
- assert Restaurant_first_dish_name(R) == ''
- print()
- print('-----E.3-----')
- def Restaurant_is_cheap(r: Restaurant, n: int):
- if Dishlist_average(r.menu) >= n:
- return False
- return True
- assert Restaurant_is_cheap(r3, 20) == True
- assert Restaurant_is_cheap(r3, 10) == False
- print()
- print('-----E.4-----')
- Collection = [r1, r2, r3]
- def Dish_raise_prices(d: Dish, n: float):
- d = d._replace(price = d.price + n)
- return d
- def Menu_raise_prices(m, n:float):
- for i in range(len(m)):
- m[i] = Dish_raise_prices(m[i], n)
- return m
- def Restaurant_raise_prices(r: Restaurant, n:float):
- r = r._replace(menu = Menu_raise_prices(r.menu, n))
- return r
- def Collection_raise_prices(C: list, n: int):
- for i in range(len(C)):
- C[i] = Restaurant_raise_prices(C[i], n)
- return C
- def Dish_raise_price(d: Dish, n: int):
- d = d._replace(price = d.price *(1 + n/100))
- return d
- def Menu_raise_price(m, n:float):
- for i in range(len(m)):
- m[i] = Dish_raise_price(m[i], n)
- return m
- def Restaurant_raise_price(r: Restaurant, n:float):
- r = r._replace(menu = Menu_raise_price(r.menu, n))
- return r
- print(Collection_raise_prices(Collection, 2.50))
- def Collection_raise_price(C: list, n: int):
- for i in range(len(C)):
- C[i] = Restaurant_raise_price(C[i], n)
- return C
- print(Collection_raise_price(Collection, 100))
- #
- ## Part(------E.5--------)
- def Collection_select_cheap(n: list,a:float):
- cheap=[]
- for i in n:
- if Dishlist_average(i.menu) <= a:
- cheap += i
- return cheap
- print(Collection_select_cheap(Collection, 50.0))
- print('-----Part G-----')
- print('4.13:')
- print()
- s='abcdefghijklmnopqrstuvwxyz'
- print(s[1:3] == 'bc')
- print(s[0:14] == 'abcdefghijklmn')
- print(s[14:] == 'opqrstuvwxyz')
- print(s[1:-1] == 'bcdefghijklmnopqrstuvw')
- print()
- print('4.14:')
- print()
- log = '128.0.0.1 - - [12/Feb/2011:10:31:08 -0600] "GET /docs/test.txt HTTP/1.0"'
- address = log[0:9].split('.')
- date = log[14:43]
- print(log)
- print(address)
- print(date)
- print()
- print('4.19:')
- print()
- first = 'Marlena'
- last = 'Sigel'
- middle = 'Mae'
- print('{}, {} {}'.format(last, first, middle))
- print('{}, {} {}.'.format(last, first, middle[0]))
- print('{} {}. {}'.format(first, middle[0], last))
- print('{}. {}. {}'.format(first[0], middle[0], last))
- print()
- print('4.23:')
- print()
- def average():
- sentence = input('Enter a sentence: ').split()
- mean = 0
- for i in sentence:
- mean += len(i)
- return mean/len(sentence)
- print(average())
- print()
- print('-----Part H-----')
- Count = namedtuple('Count', 'letter number')
- def letter_count(a: str, b:str):
- '''Takes String A and count how many times a letter appears in it'''
- Countlist = []
- letter= ''
- for x in b:
- letter = x
- number=0
- for y in a:
- if y.lower() in x:
- number+=1
- Countlist.append(Count(letter, number))
- return Countlist
- assert letter_count('Cake', 'ae') == [Count(letter='a', number=1), Count(letter='e', number=1)]
- assert letter_count('The on who did it is me.', ' \t\n') == [Count(letter=' ', number=6),
- Count(letter='\t', number=0),
- Count(letter='\n', number=0)]
- assert letter_count('Almond Acorn Ala', 'a') == [Count(letter='a', number=4)]
- print(letter_count('Two is larger than one', 'aeiou'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement