Advertisement
UniQuet0p1

Untitled

Jan 13th, 2021
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.21 KB | None | 0 0
  1. """Eksam 3 (2020-01-12)."""
  2.  
  3. def odd_index_sum(nums: list) -> int:
  4. """
  5. Find sum of elements with odd indices.
  6.  
  7. #1
  8.  
  9. odd_index_sum([1, 2, 3]) => 2
  10. odd_index_sum([]) => 0
  11. odd_index_sum([1]) => 0
  12. odd_index_sum([2, 3]) => 3
  13. odd_index_sum([0, -1, -4, -3]) => -4
  14. """
  15. pass
  16.  
  17.  
  18. def encode_string_with_hex_key(input_str: str, key: str) -> str:
  19. """
  20. Encode string using key.
  21.  
  22. #2
  23.  
  24. :param input_str - string to encode. Non-alphabetic characters are left as is.
  25. Caps are encoded into caps.
  26. :param key - hex key in which n-th number tells how much should n-th char in input_str be shifted.
  27. Works as round buffer, eg. if z is reached start from a again.
  28. The input_str and key are always the same length.
  29.  
  30. encode_string("a", "1") -> "b"
  31. encode_string("z", "1") -> "a"
  32. encode_string("abc", "101") -> "bbd"
  33. encode_string("z.z.z", "fffff") -> "o.o.o"
  34.  
  35. :return Encoded string
  36. """
  37. pass
  38.  
  39.  
  40. def who_gets_gingerbread(students: dict, total_gingerbreads: int) -> dict:
  41. """
  42. How many gingerbread students get.
  43.  
  44. #3
  45.  
  46. Given a dictionary of students with their average score and amount of gingerbreads that shows
  47. how many gingerbreads elves are about to share between students. However, elves have some conditions how to
  48. they are sharing gingerbreads - students with average score with or below 2.0 don't get any and should not appear in
  49. result dictionary and student with the highest average starts getting gingerbreads first.
  50.  
  51. Return the dictionary of students with amount of gingerbreads they got from elves.
  52.  
  53. Examples:
  54. students = {
  55. 'Mart': 4.0,
  56. 'Kristi': 4.5,
  57. 'Kevin': 3.2,
  58. 'Markus': 2.0
  59. }
  60. total_gingerbreads = 11
  61.  
  62. The order of the students: Kristi, Mart, Kevin. Markus is left out due to the average grade 2.0.
  63.  
  64. result =>
  65. {
  66. 'Kristi': 4
  67. 'Mart': 4
  68. 'Kevin': 3
  69. }
  70.  
  71. :param students: dict of students with their aberage score
  72. :param total_gingerbreads: number of gingerbreads that elves have
  73. :return: dict of students with amount of gingerbreads they got
  74. """
  75. pass
  76.  
  77.  
  78. def convert_list(input_list: list) -> tuple:
  79. """
  80. Convert list of lists to tuple of tuples.
  81.  
  82. #4
  83.  
  84. Function has to use recursion. Loop is allowed to process elements in one level.
  85.  
  86. convert_list([[[[2]]]]) => ((((2,),),),)
  87. convert_list([1, [2, [3, [4, [5, [6, [7, [8, [9]]]]]]]]]) => (1, (2, (3, (4, (5, (6, (7, (8, (9,)))))))))
  88. convert_list([65, 223, [65, 36, [[7, 4], 5], 78], 46]) => (65, 223, (65, 36, ((7, 4), 5), 78), 46)
  89. """
  90. pass
  91.  
  92.  
  93. def make_table(n: int):
  94. r"""
  95. Given an odd integer n, return a n*n table like shown in the examples.
  96.  
  97. #5
  98.  
  99. The given n is more or equal to 7.
  100.  
  101. Example 1:
  102. n=15
  103. result:
  104.  
  105. \#####/|\#####/
  106. #\###/#|#\###/#
  107. ##\#/##|##\#/##
  108. ###X###|###X###
  109. ##/#\##|##/#\##
  110. #/###\#|#/###\#
  111. /#####\|/#####\
  112. -------+-------
  113. \#####/|\#####/
  114. #\###/#|#\###/#
  115. ##\#/##|##\#/##
  116. ###X###|###X###
  117. ##/#\##|##/#\##
  118. #/###\#|#/###\#
  119. /#####\|/#####\
  120.  
  121. Example 2:
  122. n=7
  123. result:
  124.  
  125. \#/|\#/
  126. #X#|#X#
  127. /#\|/#\
  128. ---+---
  129. \#/|\#/
  130. #X#|#X#
  131. /#\|/#\
  132.  
  133. Example 3:
  134. n=9
  135. result:
  136.  
  137. \##/|\##/
  138. #\/#|#\/#
  139. #/\#|#/\#
  140. /##\|/##\
  141. ----+----
  142. \##/|\##/
  143. #\/#|#\/#
  144. #/\#|#/\#
  145. /##\|/##\
  146.  
  147. :return:
  148. """
  149. pass
  150.  
  151.  
  152. class Student:
  153. """Represent student model."""
  154.  
  155. def __init__(self, name: str, gpa: float, age: int):
  156. """
  157. Class constructor.
  158.  
  159. Each student has name and gpa (Grade Point Average).
  160.  
  161. :param name: student's name
  162. :param gpa: student's gpa
  163. :param age: student's age
  164. """
  165. self.age = age
  166. self.gpa = gpa
  167. self.name = name
  168.  
  169.  
  170. class University:
  171. """Represent university model."""
  172.  
  173. def __init__(self, name: str, gpa_required: float):
  174. """
  175. Class constructor.
  176.  
  177. Each university has name and gpa_required.
  178.  
  179. University should also have a database to keep and track all students.
  180. :param name: university name
  181. :param gpa_required: university required gpa
  182. """
  183. pass
  184.  
  185. def can_enroll_student(self, student: Student) -> bool:
  186. """
  187. Check if student can be enrolled to university.
  188.  
  189. Student can be successfully enrolled if:
  190. * he/she has required gpa (>=)
  191. * he/she is not already enrolled to this university
  192. * he/she is at least 16 years old
  193. * additionally, if student's name characters length is
  194. exactly 13 -> student can be added to university despite gpa (though still should not be
  195. already present in university and be younger)
  196. If the student cannot be enrolled, returns False. Otherwise returns True.
  197.  
  198. :return: bool
  199. """
  200. pass
  201.  
  202. def enroll_student(self, student: Student):
  203. """
  204. Enroll new student to university if possible.
  205.  
  206. Before enrolling, you have to check whether student can be enrolled.
  207.  
  208. :param student: Student
  209. Function does not return anything
  210. """
  211. pass
  212.  
  213. def can_unenroll_student(self, student: Student) -> bool:
  214. """
  215. Check if student can leave from university.
  216.  
  217. Student can be successfully leave if he/she actually studies in this university.
  218.  
  219. Returns True, if the student can be unenrolled, False otherwise.
  220.  
  221. :return: bool
  222. """
  223. pass
  224.  
  225. def unenroll_student(self, student: Student):
  226. """
  227. Unenroll student from University if possible.
  228.  
  229. Before unenrolling, you have to make sure the student can be unenrolled.
  230. Function does not return anything
  231. """
  232. pass
  233.  
  234. def get_students(self) -> list:
  235. """
  236. Return a list of all students in current university.
  237.  
  238. :return: list of Student objects
  239. """
  240. pass
  241.  
  242. def get_student_highest_gpa(self) -> list:
  243. """
  244. Return a list of students (student) with the highest gpa.
  245.  
  246. :return: list of Student objects
  247. """
  248. pass
  249.  
  250.  
  251. class Accessory:
  252. """Accessory."""
  253.  
  254. def __init__(self, name: str, value: int):
  255. """Constructor."""
  256. pass
  257.  
  258. def __repr__(self):
  259. """
  260. String representation of accessory.
  261.  
  262. Returns string in form "{name}, value : {value}."
  263. """
  264. pass
  265.  
  266.  
  267. class Car:
  268. """Car."""
  269.  
  270. def __init__(self, name: str, color: str):
  271. """Constructor."""
  272. pass
  273.  
  274. def add_accessory(self, accessory: Accessory):
  275. """Add accessory to the car."""
  276. pass
  277.  
  278. def get_value(self) -> int:
  279. """
  280. Get the value of the car.
  281.  
  282. Regular car base price is 9500, for premium car its 42 500.
  283. All the values of accessories are summed up.
  284. """
  285. pass
  286.  
  287. def get_fuel_left(self):
  288. """Return how much fuel is left in percentage."""
  289. pass
  290.  
  291. def get_accessories_by_value(self):
  292. """Return accessories sorted by value (descending i.e. higher to lower)."""
  293. pass
  294.  
  295. def __repr__(self):
  296. """
  297. String representation of the car.
  298.  
  299. Should return "This {color} {name} contains {accessory_amount} accessories and has {fuel}% fuel left."
  300. """
  301. pass
  302.  
  303.  
  304. class Customer:
  305. """Customer."""
  306.  
  307. def __init__(self, name: str, wish: str):
  308. """
  309. Constructor.
  310.  
  311. The wish consists of two words.
  312. The first word is either "Cheap" or "Expensive".
  313. In case of "Cheap", the customer wants to get the car with the lowest value.
  314. In case of "Expensive", the customer wants to get the car with the highest value.
  315. The second word is the color. Customer does not want a car with another color.
  316. For premium customer a car with the given color is searched for from the premium cars.
  317. If there is no such car with the wished color, the cheapest car is taken from the premium cars.
  318.  
  319. For example: "Cheap Red", "Expensive Yellow".
  320. """
  321. pass
  322.  
  323. def get_garage(self):
  324. """
  325. Return all the cars of the customer sorted by the value (ascending i.e. from lower to higher).
  326.  
  327. Both regular and premium cars are kept in garage.
  328. """
  329. pass
  330.  
  331. def make_premium(self):
  332. """Make customer a premium customer, premium cars can be sold to the customer now."""
  333. pass
  334.  
  335. def drive_with_car(self, driving_style: str):
  336. """
  337. Go for a drive.
  338.  
  339. A car with the highest fuel percentage should be taken.
  340. If several cars have the same percentage, use the most expensive one.
  341.  
  342. If the driving_style is "Rally", the customer takes the cheapest car instead.
  343. Regular driving takes 15 percentage points of fuel, "Rally" takes 35 percentage points (85% - 35% => 50%).
  344. If the fuel gets to zero during the drive, the car is left behind (it is no longer part of garage).
  345. """
  346. pass
  347.  
  348.  
  349. class Dealership:
  350. """Dealership."""
  351.  
  352. def __init__(self, name: str):
  353. """Constructor."""
  354. pass
  355.  
  356. def add_car(self, car: Car):
  357. """Car is added to dealership."""
  358. pass
  359.  
  360. def get_all_regular_cars(self):
  361. """Return all the regular cars sorted by value (ascending, lower to higher)."""
  362. pass
  363.  
  364. def make_car_premium(self, car: Car):
  365. """Make a car premium, which can can be sold only to premium customers."""
  366. pass
  367.  
  368. def get_all_premium_cars(self):
  369. """Return all the premium cars sorted by value (ascending, lower to higher)."""
  370. pass
  371.  
  372. def sell_car_to_customer(self, customer: Customer):
  373. """
  374. Sell a car to customer depending on his/her wishes.
  375.  
  376. After selling, the car is removed from the dealership and moved into customer's garage.
  377. In the given exercise, there is always a matching car.
  378. """
  379. pass
  380.  
  381.  
  382. if __name__ == '__main__':
  383. assert odd_index_sum([]) == 0
  384. assert odd_index_sum([1]) == 0
  385. assert odd_index_sum([1, 2]) == 2
  386. assert odd_index_sum([1, 2, 4, 3]) == 5
  387.  
  388. assert encode_string_with_hex_key("hello", "01234") == "hfnos"
  389. assert encode_string_with_hex_key("a", "f") == "p"
  390.  
  391. assert who_gets_gingerbread(
  392. {
  393. 'Mart': 4.0,
  394. 'Kristi': 4.5,
  395. 'Kevin': 3.2,
  396. },
  397. 11
  398. ) == {
  399. "Kristi": 4,
  400. "Mart": 4,
  401. "Kevin": 3
  402. }
  403.  
  404. assert convert_list([1, [2, 3], 4]) == (1, (2, 3), 4)
  405. assert convert_list([1, [2], []]) == (1, (2, ), ())
  406.  
  407. print(make_table(9))
  408. r"""
  409. \##/|\##/
  410. #\/#|#\/#
  411. #/\#|#/\#
  412. /##\|/##\
  413. ----+----
  414. \##/|\##/
  415. #\/#|#\/#
  416. #/\#|#/\#
  417. /##\|/##\
  418. """
  419. table = make_table(9).split("\n")
  420. assert table[0] == r"\##/|\##/"
  421. assert table[4] == r"----+----"
  422. assert table[6] == r"#\/#|#\/#"
  423. assert table[8] == "/##\\|/##\\"
  424. assert table[-1] != "\n" # no new-line in the end
  425.  
  426. table = make_table(7)
  427. assert table[8:15] == "#X#|#X#"
  428.  
  429. # university
  430.  
  431. university = University("taltech", 60)
  432. student = Student("Bob", 61, 18)
  433. print(university.can_enroll_student(student)) # True
  434. print(university.can_unenroll_student(student)) # False; student is not yet in university
  435.  
  436. university.enroll_student(student)
  437. print(university.get_students()) # [student]
  438. print(university.get_student_highest_gpa()) # [student]; since this student is the only one
  439.  
  440. print(university.can_unenroll_student(student)) # True
  441. university.unenroll_student(student)
  442. print(university.get_students()) # []
  443.  
  444. # dealership
  445.  
  446. blue_car = Car("Audi R4", "blue")
  447. green_car = Car("Ford", "green")
  448. wheel = Accessory("Sport wheel", 100)
  449. blue_car.add_accessory(wheel)
  450. car_dealer = Dealership("Ago Carfriend")
  451. car_dealer.add_car(blue_car)
  452. car_dealer.add_car(green_car)
  453.  
  454. print(car_dealer.get_all_regular_cars())
  455. # [This green Ford contains 0 accessories and has 100% fuel left.,
  456. # This blue Audi R4 contains 1 accessories and has 100% fuel left.]
  457. print(car_dealer.get_all_premium_cars()) # []
  458.  
  459. customer = Customer("Ago", "Cheap green")
  460. car_dealer.sell_car_to_customer(customer)
  461. print(customer.get_garage()) # [This green Ford contains 0 accessories and has 100% fuel left.]
  462. customer.drive_with_car("Rally")
  463. print(customer.get_garage()) # [This green Ford contains 0 accessories and has 65% fuel left.]
  464. customer.drive_with_car("Rally")
  465. customer.drive_with_car("Rally")
  466. print(customer.get_garage()) # []]
  467.  
  468. car_dealer.make_car_premium(blue_car)
  469. print(car_dealer.get_all_premium_cars()) # [This blue Audi R4 contains 1 accessories and has 100% fuel left.]
  470.  
  471. customer_premium = Customer("Ago", "Expensive black")
  472. customer_premium.make_premium()
  473. car_dealer.sell_car_to_customer(customer_premium)
  474. print(customer_premium.get_garage()) # [This blue Audi R4 contains 1 accessories and has 100% fuel left.]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement