Advertisement
UniQuet0p1

Untitled

Jan 10th, 2021
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.80 KB | None | 0 0
  1. """Exam 2 (2021-01-09)."""
  2.  
  3. def range_with_count(start: int, stop: int, count: int) -> list:
  4. """
  5. Return list from start to stop with number total values in the list.
  6.  
  7. The step between the elements in the list should be the same for all the elements.
  8. In case there is only one element, use the start value.
  9. Check the examples.
  10.  
  11. #1
  12.  
  13. count >= 1
  14.  
  15. range_with_count(1, 5, 1) -> [1.0]
  16. range_with_count(1, 5, 2) -> [1.0, 5.0]
  17. range_with_count(1, 5, 3) -> [1.0, 3.0, 5.0]
  18. range_with_count(1, 5, 4) -> [1.0, 2.333333333333333, 3.6666666666666665, 5.0]
  19. range_with_count(1, 5, 5) -> [1.0, 2.0, 3.0, 4.0, 5.0]
  20. range_with_count(5, 1, 5) -> [5.0, 4.0, 3.0, 2.0, 1.0]
  21. """
  22. pass
  23.  
  24.  
  25. def add_symbols(string: str, symbols: str) -> str:
  26. """
  27. Return given string with added symbols where needed.
  28.  
  29. #2
  30.  
  31. If letter in string exists in symbols, double it in result.
  32. If number in string exists in symbols, triple it in result.
  33.  
  34. It does not change the result when any symbol appears more than once in symbols.
  35.  
  36. add_symbols("ab12", "b12a") -> "aabb111222"
  37. add_symbols("xyz", "xxxxxx") -> "xxyz"
  38. add_symbols("aaaa", "b") -> "aaaa"
  39. add_symbols("aab", "a") -> "aaaab"
  40. """
  41. pass
  42.  
  43.  
  44. def h_index(articles: list) -> int:
  45. """
  46. Given a list where each value represents the times of citations of one article.
  47.  
  48. Return an h index of the person.
  49.  
  50. H-index is the largest number such that a number of publications
  51. have at least the same number of citations.
  52.  
  53. Examples:
  54. [4, 2, 4] => 2
  55. [1, 2, 2] => 2
  56. [] => 0
  57. [1, 1, 1, 1] => 1
  58. [3, 5, 7] => 3
  59. [2, 5, 7] => 2
  60. [5, 4, 7, 3, 6] => 4
  61.  
  62. :param articles:
  63. :return:
  64. """
  65. pass
  66.  
  67.  
  68. def fuel_calculator(fuel: int) -> int:
  69. """
  70. Find needed amount of fuel for a given mass.
  71.  
  72. #4
  73.  
  74. Amount of fuel needed = mass divided by three, rounded down, subtract two
  75. + fuel needed for the fuel itself
  76. + fuel needed for the fuel's fuel + etc.
  77.  
  78. Negative fuel rounds to zero.
  79.  
  80. Examples:
  81. fuel_calculator(10) -> 1 + 0 = 1
  82. fuel_calculator(151) -> 48 + 14 + 2 + 0 = 64
  83. """
  84. pass
  85.  
  86.  
  87. def valid_parentheses(sequence: str) -> bool:
  88. """
  89. Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
  90.  
  91. #5
  92.  
  93. An input string is valid if:
  94.  
  95. Open brackets must be closed by the same type of brackets.
  96. Open brackets must be closed in the correct order.
  97.  
  98. Example 1:
  99. Input: sequence = "()"
  100. Output: true
  101.  
  102.  
  103. Example 2:
  104. Input: sequence = "()[]{}"
  105. Output: true
  106.  
  107. Example 3:
  108. Input: sequence = "(]"
  109. Output: false
  110.  
  111. Example 4:
  112. Input: sequence = "([)]"
  113. Output: false
  114.  
  115. Example 5:
  116. Input: sequence = "{[]}"
  117. Output: true
  118.  
  119. :return boolean whether sequence is valid or not
  120. """
  121. pass
  122.  
  123.  
  124. class Donut:
  125. """Donut class."""
  126.  
  127. def __init__(self, filling: str, icing: str):
  128. """
  129. Donut class constructor.
  130.  
  131. :param icing: donut icing
  132. :param filling: donut filling
  133. """
  134. self.filling = filling
  135. self.icing = icing
  136.  
  137.  
  138. class DonutFactory:
  139. """DonutFactory class."""
  140.  
  141. def __init__(self):
  142. """DonutFactory class constructor."""
  143. pass
  144.  
  145. def add_donuts(self, donuts: list):
  146. """
  147. Add list of fresh donuts to already existing ones.
  148.  
  149. :param donuts: list of donuts to add
  150. :return:
  151. """
  152. pass
  153.  
  154. def get_donuts(self) -> list:
  155. """
  156. Return list of all donuts present on the line at the moment.
  157.  
  158. :return: list of all donuts
  159. """
  160. pass
  161.  
  162. def pack_donuts_by_filling_and_icing(self) -> dict:
  163. """
  164. Method should return dict with donuts divided by filling and icing.
  165.  
  166. Dict key must be represented as tuple of filling and icing and value as list of donuts with
  167. given filling and icing.
  168. {(filling, icing): [donut1, donut2]}
  169.  
  170. After packing, the production line for donuts should be empty (everything is packed).
  171.  
  172. :return: dict
  173. """
  174. pass
  175.  
  176. def sort_donuts_by_icing_and_filling(self) -> list:
  177. """
  178. Method should return list of donuts sorted by icing in alphabetical order and then by filling in alphabetical order.
  179.  
  180. :return: sorted list of donuts
  181. """
  182. pass
  183.  
  184. def get_most_popular_donut(self) -> dict:
  185. """
  186. Method should return dict with icing and filling of the most popular donut.
  187.  
  188. {icing: most_pop_donut_icing, filling: most_pop_donut_filling}
  189. If there are several icing-filling combinations with the same amount of donuts,
  190. use the one which icing is alphabetically lower (a comes before b).
  191.  
  192. :return: dict with icing and filling of most pop donut
  193. """
  194. pass
  195.  
  196. def get_least_popular_donut(self) -> dict:
  197. """
  198. Method should return dict with icing and filling of the least popular donut.
  199.  
  200. {icing: least_pop_donut_icing, filling: least_pop_donut_filling}
  201. If there are several icing-filling combinations with the same amount of donuts,
  202. use the one which filling is alphabetically higher (b comes before a).
  203.  
  204. :return: dict with icing and filling of least pop donut
  205. """
  206. pass
  207.  
  208. def get_donuts_by_flavour(self, flavour: str) -> list:
  209. """
  210. Get list of donuts that have the same icing or filling as given in method parameter.
  211.  
  212. :return: list of donuts with the given flavour.
  213. """
  214. pass
  215.  
  216.  
  217. TICKET_MARKUP = 1.2
  218.  
  219.  
  220. class Aircraft:
  221. """Aircraft that will be flying your passengers around."""
  222.  
  223. def __init__(self, operating_cost_per_distance: float, seats: int):
  224. """Constructor."""
  225. self.operating_cost_per_distance = operating_cost_per_distance
  226. self.seats = seats
  227.  
  228.  
  229. class Passenger:
  230. """Passenger interested in flying with your airline."""
  231.  
  232. def __init__(self, money):
  233. """Constructor."""
  234. self.money = money
  235.  
  236.  
  237. class Airport:
  238. """Airport where flights are passing by."""
  239.  
  240. def __init__(self, lat: float, lon: float, airport_fee: float):
  241. """Initialize airport with its coordinates in latitude and longitude, and airport fee for airlines."""
  242. self.lat = lat
  243. self.lon = lon
  244. self.airport_fee = airport_fee
  245.  
  246. def distance_to_airport(self, other_airport) -> float:
  247. """
  248. Calculate the distance between this airport and the other airport in the input, using the Pythagorean theorem.
  249.  
  250. Formula: distance = sqrt((Airport_A.lat - Airport_B.lat) ** 2 + (Airport_A.lon - Airport_B.lon) ** 2)
  251.  
  252. :param other_airport: airport we are flying to.
  253. :return: distance between this airport and other_airport
  254. """
  255. pass
  256.  
  257.  
  258. class Flight:
  259. """Flight through two or more airports."""
  260.  
  261. def __init__(self, aircraft: Aircraft, airports: list, passengers: list):
  262. """
  263. Constructor.
  264.  
  265. NB! If you change the aircraft later on and the new aircraft has less seats than you have passengers,
  266. you must remove and refund any extra passengers by the amount they originally paid for the flight.
  267.  
  268. :param aircraft: aircraft
  269. :param airports: list of Airports
  270. :param passengers: list of Passengers
  271. """
  272. pass
  273.  
  274. def change_aircraft(self, aircraft):
  275. """
  276. Change aircraft of the flight.
  277.  
  278. If the aircraft has less seats then currently there are passengers,
  279. you must remove any extra passengers and refund the amount they originally paid for the flight.
  280. """
  281. pass
  282.  
  283. def add_passenger(self, passenger: Passenger):
  284. """
  285. Add a passenger to this flight, but only if they have enough money for the ticket and there are available seats.
  286.  
  287. Don't forget to charge them for the flight!
  288.  
  289. :param passenger: potential passenger who is supposed to be added and money taken from
  290. """
  291. pass
  292.  
  293. def remove_passenger(self, passenger: Passenger):
  294. """
  295. Remove a passenger from the flight and give them a full refund for what they originally paid for the ticket.
  296.  
  297. :param passenger: the passenger to be removed
  298. """
  299. pass
  300.  
  301. def get_total_distance(self) -> float:
  302. """
  303. Calculate the entire distance of the flight through each airport.
  304.  
  305. :return: total distance of the flight
  306. """
  307. pass
  308.  
  309. def get_operating_cost(self) -> float:
  310. """
  311. Calculate the total operating cost.
  312.  
  313. Calculate the total operating cost for a flight for this flight, by multiplying the distance
  314. and the airplane's operating_cost_per_distance and finally adding the airport fee of each airport
  315. visited for this flight.
  316.  
  317. The resulting float is rounded to two decimal places.
  318.  
  319. :return: fuel cost for flying the provided distance
  320. """
  321. pass
  322.  
  323. def get_ticket_cost(self) -> float:
  324. """
  325. Calculate the cost of a single ticket.
  326.  
  327. Calculate the cost of a single ticket, for which the formula is:
  328. ticket_cost = (operating costs / (available seats / 2)) * TICKET_MARKUP
  329.  
  330. Ticket price is rounded to two decimal places.
  331.  
  332. :return: price per ticket
  333. """
  334. pass
  335.  
  336. def get_flight_profit(self) -> float:
  337. """
  338. Calculate and return the total profit of the flight, which is all ticket sales - operating costs.
  339.  
  340. Profit is rounded to two decimal places.
  341.  
  342. :return: flight profit
  343. """
  344. pass
  345.  
  346.  
  347. if __name__ == '__main__':
  348. assert range_with_count(1, 5, 2) == [1.0, 5.0]
  349. assert range_with_count(5, 1, 5) == [5.0, 4.0, 3.0, 2.0, 1.0]
  350.  
  351. assert add_symbols("aab", "a") == "aaaab"
  352. assert add_symbols("aab1", "a12") == "aaaab111"
  353.  
  354. assert h_index([4, 2, 4]) == 2
  355. assert h_index([5, 4, 7, 3, 6]) == 4
  356.  
  357. assert fuel_calculator(151) == 64
  358.  
  359. assert valid_parentheses("()") is True
  360. assert valid_parentheses("[[") is False
  361. assert valid_parentheses("[(])") is False
  362.  
  363. # donut examples
  364.  
  365. donut_factory = DonutFactory()
  366. donut1 = Donut('chocolate', 'sugar')
  367. donut2 = Donut('caramel', 'chocolate')
  368. donut3 = Donut('cherry', 'marshmallow')
  369. donut4 = Donut('chocolate', 'sugar')
  370. donut5 = Donut('vanilla', 'cream')
  371. donut6 = Donut('vanilla', 'cream')
  372. donut7 = Donut('cherry', 'marshmallow')
  373. donut8 = Donut('chocolate', 'sugar')
  374.  
  375. donuts = [donut1, donut2, donut3, donut4, donut5, donut6, donut7, donut8]
  376.  
  377. donut_factory.add_donuts(donuts)
  378.  
  379. print(donut_factory.get_donuts_by_flavour('marshmallow')) # [donut3, donut7]
  380. print(donut_factory.get_least_popular_donut()) # {icing: chocolate, filling: caramel}
  381. print(donut_factory.get_most_popular_donut()) # {icing: sugar, filling: chocolate}
  382. print(donut_factory.sort_donuts_by_icing_and_filling()) # [donut2, donut5, donut6, donut3, donut7, donut1,
  383. # donut4, donut8]
  384. print(donut_factory.pack_donuts_by_filling_and_icing()) # {(chocolate, sugar): [donut1, donut4, donut8],
  385. # (caramel, chocolate): [donut2],
  386. # (cherry, marshmallow): [donut3, donut7],
  387. # (vanilla, cream): [donut5, donut6]}
  388.  
  389. # airport examples
  390.  
  391. passenger_1 = Passenger(2000.0)
  392. passenger_2 = Passenger(350.0)
  393. airport_1 = Airport(3, 2, 25.0)
  394. airport_2 = Airport(9, 7, 20.0)
  395.  
  396. print(f'distance from airport_1 to airport_2: {airport_1.distance_to_airport(airport_2)}') # 7.81...
  397.  
  398. flight = Flight(Aircraft(200.0, 10), [airport_1, airport_2, airport_1], [passenger_1])
  399.  
  400. print(f'total distance: {flight.get_total_distance()}') # 15.62...
  401. print(f'operating cost: {flight.get_operating_cost()}') # 3194.1
  402. print(f'ticket cost: {flight.get_ticket_cost()}') # 766.58
  403. print(f'flight profit: {flight.get_flight_profit()}') # -2427.52
  404.  
  405. flight.add_passenger(passenger_2)
  406. print(f'passenger count: {len(flight.passengers)}') # 1
  407.  
  408. passenger_3 = Passenger(800.0)
  409. flight.add_passenger(passenger_3)
  410. print(f'passenger count: {len(flight.passengers)}') # 2
  411. print(f'flight profit: {flight.get_flight_profit()}') # -1660.94
  412.  
  413. flight.remove_passenger(passenger_3)
  414. print(f'passenger count: {len(flight.passengers)}') # 1
  415. print(f'flight profit: {flight.get_flight_profit()}') # -2427.52
  416.  
  417. print(f'passenger money: {passenger_3.money}') # 800.0
  418.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement