Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.17 KB | None | 0 0
  1. #Raam Nachiappan 45618246 and Marcos Antonio Avila 58548681
  2. import tkinter
  3. from collections import namedtuple
  4.  
  5. #
  6. #
  7. # Part (c)
  8. #
  9. #
  10.  
  11. print()
  12. print('*********** Part (c) ***********')
  13. print()
  14.  
  15. print('---------- Part (c1) ----------')
  16. print("Create a function that returns the first 3 letters of the input string.")
  17. print()
  18.  
  19. def abbreviate (inputString: str) -> str:
  20. '''Returns first 3 letters of a string'''
  21. return inputString[0:3]
  22. assert abbreviate('January') == 'Jan'
  23. assert abbreviate('abril') == 'abr'
  24.  
  25. print(abbreviate('January'))
  26. print(abbreviate('abril'))
  27.  
  28. print()
  29. print('---------- Part (c2) ----------')
  30. print("Create a function that calculates the area of a square.")
  31. print()
  32.  
  33. def find_area_square(length: float) -> float:
  34. '''Returns area of a square'''
  35. return length**2
  36. assert find_area_square(1) == 1
  37. assert find_area_square(5) == 25
  38.  
  39. print(find_area_square(1), "units squared")
  40. print(find_area_square(5), "units squared")
  41.  
  42. print()
  43. print('---------- Part (c3) ----------')
  44. print("Create a function that calculates the area of a circle.")
  45. print()
  46.  
  47. def find_area_circle(radius: float) -> float:
  48. '''Returns area of a circle'''
  49. return 3.14159*radius**2
  50. assert find_area_circle(1) == 3.14159
  51. assert find_area_circle(5) == 78.53975
  52.  
  53. print(find_area_circle(1), "units squared")
  54. print(find_area_circle(5), "units squared")
  55.  
  56. print()
  57. print('---------- Part (c4) ----------')
  58. print("Create a function that prints out all even numbers from a list.")
  59. print()
  60.  
  61. def print_even_numbers(even_list: list) -> None:
  62. '''Prints the even numbers of a list'''
  63. for number in even_list:
  64. if number % 2 == 0:
  65. print(number)
  66.  
  67. print_even_numbers([2, 47, 31, 99, 20, 19, 23, 105, 710, 1004])
  68.  
  69. print()
  70. print('---------- Part (c5) ----------')
  71. print("Create a function able to return the shipping rate based on weight.")
  72. print()
  73.  
  74. def calculate_shipping(weight: float) -> float:
  75. '''Calculates shipping rate based on weight'''
  76. if weight < 2:
  77. return 2.00
  78. elif weight >= 2 and weight < 10:
  79. return 5.00
  80. else:
  81. return 5 + ((weight - 10) * 1.50)
  82. return rate
  83. assert calculate_shipping(1.5) == 2
  84. assert calculate_shipping(7) == 5
  85. assert calculate_shipping(15) == 12.50
  86.  
  87. print("${:2.2f}".format(calculate_shipping(1.5)))
  88. print("${:2.2f}".format(calculate_shipping(7)))
  89. print("${:2.2f}".format(calculate_shipping(15)))
  90.  
  91. print()
  92. print('---------- Part (c6) ----------')
  93. print("Create a function that can draw squares.")
  94. print()
  95.  
  96. window1 = tkinter.Tk()
  97. canvas1 = tkinter.Canvas(window1, width=500, height=500)
  98. canvas1.pack()
  99.  
  100. print("Check tkinter window.")
  101.  
  102. def create_square(x: int, y: int, length: int):
  103. '''Draws a square'''
  104. canvas1.create_rectangle(x,y,x+length,y+length)
  105.  
  106. create_square(10,10,80)
  107.  
  108. print()
  109. print('---------- Part (c7) ----------')
  110. print("Create a function that can draw circles.")
  111. print()
  112.  
  113. print("Check tkinter window.")
  114.  
  115. def create_circle(x: int, y: int, diameter: int):
  116. '''Draws a circle'''
  117. canvas1.create_oval(x,y,x+diameter,y+diameter)
  118.  
  119. create_circle(400,400,80)
  120.  
  121. #
  122. #
  123. # Part (d)
  124. #
  125. #
  126.  
  127. print()
  128. print('*********** Part (d) ***********')
  129. print()
  130. print('---------- Part (d1) ----------')
  131. print("Create a function that returns the price of a Restaurant")
  132. print()
  133.  
  134. Restaurant = namedtuple('Restaurant', 'name cuisine phone dish price')
  135.  
  136. RC = [
  137. Restaurant("Thai Dishes", "Thai", "334-4433", "Mee Krob", 12.50),
  138. Restaurant("Nobu", "Japanese", "335-4433", "Natto Temaki", 5.50),
  139. Restaurant("Nonna", "Italian", "355-4433", "Stracotto", 25.50),
  140. Restaurant("Jitlada", "Thai", "324-4433", "Paht Woon Sen", 15.50),
  141. Restaurant("Nola", "New Orleans", "336-4433", "Jambalaya", 5.50),
  142. Restaurant("Noma", "Modern Danish", "337-4433", "Birch Sap", 35.50),
  143. Restaurant("Addis Ababa", "Ethiopian", "337-4453", "Yesiga Tibs", 10.50) ]
  144.  
  145.  
  146.  
  147. def restaurant_price(restaur: Restaurant) -> float:
  148. '''Returns the price of the best dish from a Restaurant type'''
  149. return restaur.price
  150. assert restaurant_price(RC[0]) == 12.50
  151. assert restaurant_price(RC[-1]) == 10.50
  152.  
  153. print("${:2.2f}".format(restaurant_price(RC[0])))
  154. print("${:2.2f}".format(restaurant_price(RC[-1])))
  155.  
  156. print()
  157. print('---------- Part (d2) ----------')
  158. print("Create a function that prints out a list of restaurants sorted by price.")
  159. print()
  160.  
  161. RC.sort(key=restaurant_price)
  162. print(RC)
  163.  
  164. RC.sort() #restores list to alpha-order, costliest function will sort from this list by price
  165.  
  166. print()
  167. print('---------- Part (d3) ----------')
  168. print("Create a function that returns the name of the restaurant with the most expensive dish.")
  169. print()
  170.  
  171. def costliest(restaurantList: list) -> str:
  172. '''Returns the name of the restaurant with the most expensive best dish'''
  173. restaurantList.sort(key=restaurant_price)
  174. return restaurantList[-1].name
  175. #assert costliest(RC) == 'Noma' This assertion modifies RC list
  176.  
  177. print(RC)
  178. print()
  179. print(costliest(RC))
  180. print()
  181. print(RC)
  182. #print(RC)#list sorted by price
  183.  
  184. RC.sort() #restores list to alpha-order, costliest2 function will sort from this list by price
  185.  
  186. print()
  187. print('---------- Part (d4) ----------')
  188. print("Create a function that returns the name of the restaurant with the most expensive dish AND does not modify the list of restaurants.")
  189. print()
  190.  
  191. def costliest2(restaurantList: list) -> str:
  192. '''Returns the name of the restaurant with the most expensive best dish WITHOUT modifying list'''
  193. return sorted(restaurantList, key=restaurant_price, reverse=True)[0].name
  194. assert costliest2(RC) == 'Noma'
  195.  
  196. print(RC)
  197. print()
  198. print(costliest2(RC))
  199. print()
  200. print(RC)
  201.  
  202. #
  203. #
  204. # Part (e)
  205. #
  206. #
  207.  
  208. print()
  209. print('*********** Part (e) ***********')
  210. print()
  211.  
  212. Book = namedtuple('Book', 'author title genre year price instock')
  213. BSI = [
  214. Book('Edgar Allen Poe', 'The Raven', 'Mystery', 1845, 20.00, 100),
  215. Book('John Flanagan', 'Ranger\'s Apprentice: Book 1', 'Adventure', 2004, 16.00, 75),
  216. Book('Agatha Christie', 'And Then There Were None', 'Mystery', 1939, 10.50, 34),
  217. Book('John Green','The Fault in Our Stars', 'Young-Adult Novel', 2012, 23.00, 234),
  218. Book('John Green', 'Looking For Alaska', 'Young-Adult Novel', 2005, 20.00, 253),
  219. Book('Bryan Lee O\'Malley', 'Scott Pilgrim\'s Precious Little Life', 'Comedy', 2004, 26.00, 1002),
  220. Book('Alisa Smanpan','Book of Modern Technology','Technology', 1996, 4.00, 2) ] #last book to test e.4
  221.  
  222. print()
  223. print('---------- Part (e1) ----------')
  224. print("Write a few statements that will print out every book title.")
  225. print()
  226.  
  227. for current_book in BSI:
  228. print(current_book.title)
  229.  
  230. print()
  231. print('---------- Part (e2) ----------')
  232. print("Write a few statements that will print out the book titles in alphabetical order.")
  233. print()
  234.  
  235. def get_book_title(bk: Book) -> str:
  236. '''Returns the title of a Book type'''
  237. return bk.title
  238. assert get_book_title(BSI[0]) == 'The Raven'
  239.  
  240. for current_book in sorted(BSI, key=get_book_title):
  241. print(current_book.title)
  242.  
  243. print()
  244. print('---------- Part (e3) ----------')
  245. print("Write a few statements that will increase the prices of all books by 10%.")
  246. print()
  247.  
  248. for i in range(len(BSI)):
  249. BSI[i] = BSI[i]._replace(price = BSI[i].price * 1.10)
  250. print("${:2.2f}".format(BSI[i].price))
  251.  
  252. print()
  253. print('---------- Part (e4) ----------')
  254. print("Write a few statements that will print out books with the genre 'Technology'.")
  255. print()
  256.  
  257. for current_book in BSI:
  258. if(current_book.genre == 'Technology'):
  259. print(current_book.title)
  260.  
  261. print()
  262. print('---------- Part (e5) ----------')
  263. print("Write a few statements that will determine if there are more books printed before or after year 2000.")
  264. print()
  265.  
  266. beforeList = []
  267. afterList = []
  268.  
  269. for current_book in BSI:
  270. if(current_book.year < 2000):
  271. beforeList.append(current_book.title)
  272. elif(current_book.year >= 2000):
  273. afterList.append(current_book.title)
  274.  
  275. if len(beforeList) > len(afterList):
  276. print("More titiles before 2000", len(beforeList), "vs.", len(afterList))
  277. else:
  278. print("More titles 2000 or later", len(afterList), "vs.", len(beforeList))
  279.  
  280. print()
  281. print('---------- Part (e6) ----------')
  282. print("Write a function that calculates inventory value and a function that returns the Book with the highest inventory value.")
  283. print()
  284.  
  285. def inventory_value(bk: Book) -> float:
  286. '''Returns the inventory value of a book'''
  287. return bk.price * bk.instock
  288. assert inventory_value(BSI[0]) == 2200
  289.  
  290. def top_value(bookList: list) -> Book:
  291. '''Returns the Book type with the highest inventory value'''
  292. return sorted(bookList, key=inventory_value)[-1]
  293. #assert top_value(BSI) == BSI[-2] Bad assertion, if list grows, then what?
  294.  
  295. print("The highest-value book is", top_value(BSI).title, "by", top_value(BSI).author, "at a value of", "${:2.2f}".format((top_value(BSI).instock * top_value(BSI).price)))
  296.  
  297. #
  298. #
  299. # Part (f)
  300. #
  301. #
  302.  
  303. print()
  304. print('*********** Part (f) ***********')
  305. print("Make functions that will draw a face.")
  306. print()
  307. print("Check tkinter window.")
  308.  
  309. window2 = tkinter.Tk()
  310. canvas2 = tkinter.Canvas(window2, width=1000, height=1000)
  311. canvas2.pack()
  312.  
  313. def draw_eye(x: int, y: int, eye_color: str) -> None:
  314. '''Draws an eye'''
  315. canvas2.create_oval(x+0,y+0,x+80,y+60, fill = 'white')
  316. canvas2.create_oval(x+10,y+0,x+70,y+60, fill = eye_color)
  317. canvas2.create_oval(x+35,y+25,x+45,y+35, fill = 'black' )
  318.  
  319. def draw_nose(x: int, y:int) -> None:
  320. '''Draws a nose'''
  321. canvas2.create_oval(x, y, x + 80, y + 105, fill = 'light yellow')
  322.  
  323. def draw_mouth(x: int, y: int) -> None:
  324. '''Draws a mouth'''
  325. canvas2.create_arc(x, y, x + 220, y + 195, start=0, extent=-180, fill='red')
  326.  
  327. def draw_face(x: int, y: int, eye_color: str, skin_color: str) -> None:
  328. '''Draws an entire face'''
  329. canvas2.create_oval(x,y,x+300, y+ 350, fill = skin_color)
  330. draw_eye(x + 40,y + 80, eye_color)
  331. draw_eye(x+ 180,y+ 80, eye_color)
  332. draw_nose(x + 110,y + 115)
  333. draw_mouth(x + 40,y + 135)
  334.  
  335. draw_face(0,0,'light blue', 'light green')
  336. draw_face(300,0,'pink', 'light blue')
  337. draw_face(300,350,'yellow', 'orange')
  338. draw_face(0,350, 'blue', 'black')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement