Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.19 KB | None | 0 0
  1. #Tom Tan 50152878 and yina li 19829156. ICS 31 Lab sec 11. Lab asst. 5.
  2.  
  3. print ()
  4. #C
  5. print('-----Part C-----')
  6. print()
  7. from collections import namedtuple
  8. Dish = namedtuple('Dish', 'name price calories')
  9. #c.1
  10. print("-----C.1-----")
  11. d1 = Dish('Sushi', 10.00, 500)
  12. d2 = Dish('Pasta', 8.00, 1000)
  13. d3 = Dish('Paht Woon Sen',  9.50, 330)
  14.  
  15. print()
  16. #c.2
  17. print('-----C.2-----')
  18. def Dish_str(d: Dish):
  19.     return '{0} (${1}): {2} cal'.format(d.name, d.price, d.calories)
  20. print(Dish_str(d3))
  21.  
  22. print()
  23. #c.3
  24. print('-----C.3-----')
  25. def Dish_same(x: Dish, y: Dish):
  26.     return x.name ==y.name and x.calories == y.calories
  27.  
  28. d4= Dish('Sushi', 12.00, 600)
  29. d5=Dish('Mushroom', 14.50, 1000)
  30. d6=Dish('Paht Woon Sen', 20.00, 330)
  31.  
  32. assert Dish_same(d4, d1) == False
  33. assert Dish_same(d1, d1) == True
  34. assert Dish_same(d1, d2) == False
  35. assert Dish_same(d3, d6) == True
  36.  
  37. print()
  38. #c.4
  39. print('-----C.4-----')
  40. def Dish_change_price(D: Dish, x: int):
  41.     D=Dish(D.name, D.price * (1+(x/100)), D.calories)
  42.     return D
  43.  
  44. print(Dish_change_price(d1, 100))
  45. print(Dish_change_price(d2, -50))
  46.  
  47. print()
  48. #c.5
  49. print('-----C.5-----')
  50. def Dish_is_cheap(d: Dish, x:int):
  51.     return d.price<x
  52.  
  53. assert Dish_is_cheap(d1, 15) == True
  54. assert Dish_is_cheap(d2, 5) == False
  55.  
  56. print()
  57. #c.6
  58. print('-----C.6-----')
  59. d7=Dish('Chips', 1.00, 200)
  60. d8=Dish('Hamburgers', 5.00, 800)
  61. DL = [d1, d2, d3, d4, d5]
  62. print(DL)
  63. DL2=[d6, d7, d8]
  64. DL.extend(DL2)
  65. print(DL)
  66.  
  67. def Dishlist_display(d: [Dish]):
  68.     string = ''
  69.     for i in d:
  70.         string += i.name + ' ' + str(i.price) + ' ' + str(i.calories)+'\n'
  71.     return string
  72.  
  73. print(Dishlist_display(DL))
  74.  
  75. print()
  76. #c.7
  77. print('-----C.7-----')
  78. def Dishlist_all_cheap(d: [Dish], x: int):
  79.     for i in d:
  80.         if Dish_is_cheap(i, x) == False:
  81.             return False
  82.     return True
  83.  
  84. assert Dishlist_all_cheap(DL, 25) == True
  85. assert Dishlist_all_cheap(DL, 15) ==False
  86.  
  87. print()
  88. #c.8
  89. print('-----C.8-----')
  90. def Dishlist_change_price(DL:list, n:int):
  91.     new_list =[]
  92.     for i in range(len(DL)):
  93.         new_list += Dish( DL[i].name, DL[i].price*(1+(n/100)), DL[i].calories)
  94.     return new_list
  95. print(Dishlist_change_price(DL, 100))
  96.  
  97.  
  98. print()
  99. #c.9
  100. print('-----C.9-----')
  101. def Dishlist_prices (DL:list):
  102.     second_list =[]
  103.     for i in DL:
  104.         second_list.append ( i.price)
  105.     return second_list
  106. print(Dishlist_prices(DL))
  107.  
  108. print()
  109. #c.10
  110. print('-----C.10-----')
  111. def Dishlist_average(d: list):
  112.     return sum(Dishlist_prices(d))/len(d)
  113.  
  114. print(Dishlist_average(DL))
  115.  
  116. print()
  117. #c.11
  118. print('-----C.11-----')
  119. def Dishlist_keep_cheap(d: list, n: int):
  120.     new_list = []
  121.     for i in d:
  122.         if i.price<n:
  123.             new_list.append(i.name)
  124.     return new_list
  125. print(Dishlist_keep_cheap(DL, 10))
  126.  
  127. print()
  128. #c.12
  129. d9 = Dish('Tofu', 2.00, 100)
  130. d10 = Dish('Pie', 5.00, 600)
  131. d11 = Dish('Ramen', 12.50, 800)
  132. d12 = Dish('Caviar', 25.00, 750)
  133. d13 = Dish('Cake', 16.00, 1500)
  134. d14 = Dish('Waffle', 2.00, 500)
  135. d15 = Dish('Coffee', 3.50, 300)
  136. d16 = Dish('BBQ', 7.00, 400)
  137. d17 = Dish('Rice cake', 11.00, 800)
  138. d18 = Dish('Egg cake', 5.00, 800)
  139. d19 = Dish('Donuts', 6.00, 600)
  140. d20= Dish('Pizza', 9.00, 500)
  141. d21 = Dish('Burger', 3.00, 400)
  142. d22 = Dish('Taco', 2.00, 500)
  143. d23 = Dish('Salad', 8.50, 100)
  144. d24 = Dish('Bread', 3.50, 1000)
  145. d25 = Dish('Hot dog', 4.00, 700)
  146. 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]
  147.  
  148. def before_and_after():
  149.     number = int(input('Percentage: '))
  150.     print(Dishlist_display(DL3))
  151.     for i in range(len(DL3)):
  152.         DL3[i] = DL3[i]._replace(price = DL3[i].price * (1 + (number/100)))
  153.     print(Dishlist_display(DL3))
  154.     return
  155.  
  156. print(before_and_after())
  157.  
  158.  
  159. print()
  160. print('-----Part E-----')
  161.  
  162. Restaurant = namedtuple('Restaurant', 'name cuisine phone menu')
  163. r1 = Restaurant ('Thai Dishes', 'Thai', '334-4433', [Dish('Mee Krob', 12.50, 500), Dish('Larb Gai', 11.00, 450)])
  164. r2 = Restaurant('Taillevent', 'French', '01-44-95-15-01',
  165.                 [Dish('Homard Bleu', 45.00, 750),
  166.                  Dish('Tournedos Rossini', 65.00, 950),
  167.                  Dish("Selle d'Agneau", 60.00, 850)])
  168. print()
  169. print('-----E.1-----')
  170. r3 = Restaurant('Pascal', 'French', '940-752-0107',
  171.                 [Dish('Escargots', 12.95, 250),
  172.                  Dish('Poached Salmon', 18.50, 550),
  173.                  Dish('Rack of Lamb', 24.00, 850),
  174.                  Dish('Marjolaine Cake', 8.50, 950)])
  175.  
  176.                
  177. print()
  178. print('-----E.2-----')
  179. def Restaurant_first_dish_name(r:Restaurant)->list:
  180.     if r.menu == []:
  181.         return ''
  182.     return r.menu[0].name
  183.  
  184. R = Restaurant('Pascal', 'French', '940-752-0107', [])
  185.  
  186. assert Restaurant_first_dish_name(r3) == 'Escargots'
  187. assert Restaurant_first_dish_name(R) == ''
  188.  
  189. print()
  190. print('-----E.3-----')
  191. def Restaurant_is_cheap(r: Restaurant, n: int):
  192.     if Dishlist_average(r.menu) >= n:
  193.         return False
  194.     return True
  195.  
  196. assert Restaurant_is_cheap(r3, 20) == True
  197. assert Restaurant_is_cheap(r3, 10) == False
  198.  
  199. print()
  200. print('-----E.4-----')
  201. Collection = [r1, r2, r3]
  202. def Dish_raise_prices(d: Dish, n: float):
  203.     d = d._replace(price = d.price + n)
  204.     return d
  205. def Menu_raise_prices(m, n:float):
  206.     for i in range(len(m)):
  207.         m[i] = Dish_raise_prices(m[i], n)
  208.     return m
  209. def Restaurant_raise_prices(r: Restaurant, n:float):
  210.     r = r._replace(menu = Menu_raise_prices(r.menu, n))
  211.     return r
  212.  
  213. def Collection_raise_prices(C: list, n: int):
  214.     for i in range(len(C)):
  215.         C[i] = Restaurant_raise_prices(C[i], n)
  216.     return C
  217.  
  218.  
  219. def Dish_raise_price(d: Dish, n: int):
  220.     d = d._replace(price = d.price *(1 + n/100))
  221.     return d
  222. def Menu_raise_price(m, n:float):
  223.     for i in range(len(m)):
  224.         m[i] = Dish_raise_price(m[i], n)
  225.     return m
  226. def Restaurant_raise_price(r: Restaurant, n:float):
  227.     r = r._replace(menu = Menu_raise_price(r.menu, n))
  228.     return r
  229.  
  230. print(Collection_raise_prices(Collection, 2.50))
  231. def Collection_raise_price(C: list, n: int):
  232.     for i in range(len(C)):
  233.         C[i] = Restaurant_raise_price(C[i], n)
  234.     return C
  235.  
  236. print(Collection_raise_price(Collection, 100))
  237.  
  238. #
  239. ## Part(------E.5--------)
  240.  
  241. def Collection_select_cheap(n: list,a:float):
  242.     cheap=[]
  243.     for i in n:
  244.         if Dishlist_average(i.menu) <= a:
  245.             cheap += i
  246.     return cheap
  247.  
  248. print(Collection_select_cheap(Collection, 50.0))
  249. print('-----Part G-----')
  250. print('4.13:')
  251. print()
  252. s='abcdefghijklmnopqrstuvwxyz'
  253. print(s[1:3] == 'bc')
  254. print(s[0:14] == 'abcdefghijklmn')
  255. print(s[14:] == 'opqrstuvwxyz')
  256. print(s[1:-1] == 'bcdefghijklmnopqrstuvw')
  257.  
  258. print()
  259. print('4.14:')
  260. print()
  261. log = '128.0.0.1 - - [12/Feb/2011:10:31:08 -0600] "GET /docs/test.txt HTTP/1.0"'
  262. address = log[0:9].split('.')
  263. date = log[14:43]
  264. print(log)
  265. print(address)
  266. print(date)
  267.  
  268.  
  269. print()
  270. print('4.19:')
  271. print()
  272. first = 'Marlena'
  273. last = 'Sigel'
  274. middle = 'Mae'
  275. print('{}, {} {}'.format(last, first, middle))
  276. print('{}, {} {}.'.format(last, first, middle[0]))
  277. print('{} {}. {}'.format(first, middle[0], last))
  278. print('{}. {}. {}'.format(first[0], middle[0], last))
  279.  
  280. print()
  281. print('4.23:')
  282. print()
  283. def average():
  284.     sentence = input('Enter a sentence: ').split()
  285.     mean = 0
  286.     for i in sentence:
  287.         mean += len(i)
  288.     return mean/len(sentence)
  289.  
  290. print(average())
  291.  
  292. print()
  293. print('-----Part H-----')
  294. Count = namedtuple('Count', 'letter number')
  295.  
  296. def letter_count(a: str, b:str):
  297.     '''Takes String A and count how many times a letter appears in it'''
  298.     Countlist = []
  299.     letter= ''
  300.     for x in b:
  301.         letter = x
  302.         number=0
  303.         for y in a:
  304.             if y.lower() in x:
  305.                 number+=1
  306.         Countlist.append(Count(letter, number))
  307.     return Countlist
  308.  
  309. assert letter_count('Cake', 'ae') == [Count(letter='a', number=1), Count(letter='e', number=1)]
  310. assert letter_count('The on who did it is me.', ' \t\n') == [Count(letter=' ', number=6),
  311.                                                              Count(letter='\t', number=0),
  312.                                                              Count(letter='\n', number=0)]
  313. assert letter_count('Almond Acorn Ala', 'a') == [Count(letter='a', number=4)]
  314. print(letter_count('Two is larger than one', 'aeiou'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement