UniQuet0p1

Untitled

Jan 8th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.60 KB | None | 0 0
  1. """Exam 1 (2021-01-04)."""
  2.  
  3. def sum_of_multiples(limit: int, multiplier: int):
  4. """
  5. Given a limit, find the sum of all the multiples of multiplier up to but not including that number.
  6.  
  7. #1
  8.  
  9. Limit and multiplier are both natural numbers.
  10. If limit is smaller or equal than multiplier it should return 0.
  11.  
  12. sum_of_multiples(20, 5) -> 30
  13. sum_of_multiples(10, 1) -> 45
  14. sum_of_multiples(5, 5) -> 0
  15. """
  16. pass
  17.  
  18.  
  19. def mix_string(s1: str, s2: str) -> str:
  20. """
  21. Given two strings s1 and s2, create a mixed string by alternating between str1 and str2 chars.
  22.  
  23. #2
  24.  
  25. mix_string("AAA", "bbb") -> AbAbAb
  26. mix_string("AA", "") -> AA
  27. mix_string("mxdsrn", "ie tig") -> mixed string
  28. """
  29. pass
  30.  
  31.  
  32. def bingo(matrix: list, numbers: list) -> tuple:
  33. """
  34. Whether the matrix has winning combinations with the given numbers.
  35.  
  36. #3
  37.  
  38. Check if player got any winning combinations:
  39. 1. Corners - all 4 corners contain winning numbers
  40. 2. Diagonals - all diagonals contain winning numbers
  41. 3. Full game - all numbers in the matrix/ticket are in the winning numbers
  42. Example matrix:
  43. [
  44. [ 5, 7, 11, 15, 21],
  45. [22, 25, 26, 27, 9],
  46. [34, 2, 48, 54, 58],
  47. [59, 61, 33, 81, 24],
  48. [90, 37, 3, 6, 32],
  49. ]
  50.  
  51. :param matrix: 5 x 5 bingo ticket of numbers
  52. :param numbers: list of winning numbers (size always at least 4)
  53. :return: tuple of booleans (corners, diagonals, full_game)
  54. """
  55. pass
  56.  
  57.  
  58. def can_reach(arr: list, start: int) -> bool:
  59. """
  60. Given an array of non-negative integers arr, you are initially positioned at start index of the array.
  61.  
  62. #4
  63.  
  64. When you are at index i, you can jump to i + arr[i] or i - arr[i], check if you can reach to any index with value 0.
  65.  
  66. Notice that you can not jump outside of the array at any time.
  67.  
  68. Solution has to be recursive!
  69.  
  70. Example 1:
  71. Input: arr = [4,2,3,0,3,1,2], start = 5
  72. Output: True
  73. Explanation:
  74. All possible ways to reach at index 3 with value 0 are:
  75. index 5 -> index 4 -> index 1 -> index 3
  76. index 5 -> index 6 -> index 4 -> index 1 -> index 3
  77.  
  78. Example 2:
  79. Input: arr = [4,2,3,0,3,1,2], start = 0
  80. Output: True
  81. Explanation:
  82. One possible way to reach at index 3 with value 0 is:
  83. index 0 -> index 4 -> index 1 -> index 3
  84.  
  85. Example 3:
  86. Input: arr = [1, 2, 0], start = 0
  87. Output: False
  88. From index 0, we can move to index 1, but then nowhere else.
  89.  
  90. :return boolean whether a possible route exists or not
  91. """
  92. pass
  93.  
  94.  
  95. def prime_factorization(number: int) -> int:
  96. """
  97. Given a natural number greater than 1, return it's prime factorization.
  98.  
  99. #5
  100.  
  101. Return the prime factorization of number.
  102.  
  103. Return dict, where the key is a prime factor and the value is count of this factor.
  104.  
  105. 12 = 2 * 2 * 3 => {2: 2, 3: 1}
  106. 1960 = 2 * 2 * 2 * 5 * 7 * 7 => {2: 3, 5: 1, 7: 2}
  107. 79 = 79 * 1 => {79: 1}
  108.  
  109. Prime number is a number which is divisible only by 1 and the number itself.
  110. For example 2, 3, 5, 7, 11, 13, 17, 19, 23 are prime numbers.
  111.  
  112. Examples:
  113. 2 => { 2: 1 }
  114. 12 => { 2: 2, 3: 1 }
  115. 1960 => { 2: 3, 5: 1, 7: 2 }
  116. 1024 => { 2: 10 }
  117. 79 => { 79: 1 }
  118. 121 => { 11: 2 }
  119.  
  120. :param number: a number greater than 1
  121. :return: dict of prime factors and their counts.
  122. """
  123. pass
  124.  
  125.  
  126. class Candy:
  127. """Candy #6."""
  128.  
  129. def __init__(self, name: str, filling: str):
  130. """
  131. Candy class constructor.
  132.  
  133. :param name: candy name
  134. :param filling: candy filling
  135. """
  136. self.name = name
  137. self.filling = filling
  138.  
  139.  
  140. class CandyShop:
  141. """Candy shop #6."""
  142.  
  143. def __init__(self):
  144. """CandyShop class constructor."""
  145. pass
  146.  
  147. def add_candies(self, candies: list):
  148. """
  149. Add list of fresh candies to already existing ones.
  150.  
  151. :param candies: list of candies to add
  152. :return:
  153. """
  154. pass
  155.  
  156. def get_candies(self) -> list:
  157. """
  158. Return list of all candies existing in the shop.
  159.  
  160. :return: list of all candies
  161. """
  162. pass
  163.  
  164. def get_candies_by_filling(self, filling: str) -> list:
  165. """
  166. Get list of candies that have the same filling as given in parameter value.
  167.  
  168. :return: list
  169. """
  170. pass
  171.  
  172. def sort_candies_by_filling(self) -> list:
  173. """
  174. Method should return list of candies sorted by filling in alphabetical order.
  175.  
  176. If filling is the same, then sort
  177. by name in alphabetical order.
  178.  
  179. :return: sorted list of candies
  180. """
  181. pass
  182.  
  183. def get_most_popular_candy_name_and_filling(self) -> dict:
  184. """
  185. Find the most popular candy name and filling.
  186.  
  187. Method should return dict with name and filling of the most popular candy in the shop (type of candy which name
  188. and filling is present the most in the shop). NB! You should consider the most popular combination of name and filling.
  189. {name: most_pop_candy_name, filling: most_pop_candy_filling}
  190.  
  191. If there are several suitable candidates, return any of those (doesn't matter which one).
  192.  
  193. :return: dict with name and filling of most pop candy
  194. """
  195. pass
  196.  
  197. def get_least_popular_candy_name_and_filling(self) -> dict:
  198. """
  199. Find the least popular candy name and filling.
  200.  
  201. Method should return dict with name and filling of the least popular candy in the shop (type of candy which name
  202. and filling is present the least in the shop). NB! You should consider the least popular combination of name and filling.
  203. {name: least_pop_candy_name, filling: least_pop_candy_filling}
  204.  
  205. If there are several suitable candidates, return any of those (doesn't matter which one).
  206.  
  207. :return: dict with name and filling of least pop candy
  208. """
  209. pass
  210.  
  211. def collect_candies_by_filling(self) -> dict:
  212. """
  213. Group candies by filling.
  214.  
  215. Method should return dict with candies divided by filling, where dict key is filling and dict value is list
  216. of candies with this filling.
  217. {candy_filling: [candy1, candy2]}
  218.  
  219. :return: dict of candies divided by filling
  220. """
  221. pass
  222.  
  223.  
  224. class Grade:
  225. """Grade #7."""
  226.  
  227. def __init__(self, grade, weight: int, assignment: str, date: str):
  228. """Constructor."""
  229. self.assignment = assignment
  230. self.value = grade
  231. self.weight = weight
  232. self.date = date
  233. self.previous_grades = {}
  234.  
  235. def change_grade(self, new_grade: int, date: str):
  236. """
  237. Change a previous grade.
  238.  
  239. This function should save the previous grade in a dictionary previous_grades, where key is the date and value
  240. is the value of the grade. Value and date should be updated.
  241. """
  242. pass
  243.  
  244.  
  245. class Student:
  246. """Student #7."""
  247.  
  248. def __init__(self, name: str):
  249. """Constructor."""
  250. self.name = name
  251. self.grades = {}
  252.  
  253. def grade(self, grade: Grade):
  254. """
  255. Add a grade for an assignment that a students has done.
  256.  
  257. Grades are kept in a dictionary where assignment name is the key and Grade object is the value (All previous
  258. grades for the same assignment are kept in the Grade object previous grades dictionary).
  259. Note that this function is only used when a student does an assignment for the first time.
  260. """
  261. pass
  262.  
  263. def redo_assignment(self, new_grade: int, assignment: str, date: str):
  264. """
  265. Update the grade for given assignment.
  266.  
  267. This function is only used when an assignment has been attempted at least once before. Keep in mind that you
  268. need to also keep the history of grades, not create a new grade!
  269. """
  270. pass
  271.  
  272. def calculate_weighted_average(self):
  273. """
  274. Calculate the weighted average of grades.
  275.  
  276. You should take into account the weights. There are three weights: 1, 2 and 3, where 3 means that one grade of
  277. weight 3 is the same as three grades of weight 1.
  278.  
  279. For example:
  280. if there are grades 4 with weight 3 and 3 with weight 1, then the resulting value will be
  281. (4 * 3 + 3 * 1) / (3 + 1) = 15 / 4 = 3.75
  282. which will be rounded to 4.
  283.  
  284. Also make sure not to miss out when a grade is noted as "!". If there is no attempt to redo this, then "!"
  285. should be equivalent to grade "1".
  286. """
  287. pass
  288.  
  289.  
  290. class Class:
  291. """Class #7."""
  292.  
  293. def __init__(self, teacher: str, students: list):
  294. """Constructor."""
  295. self.teacher = teacher
  296. self.students = students
  297.  
  298. def add_student(self, student: Student):
  299. """Add student to the class."""
  300. pass
  301.  
  302. def add_students(self, students: list):
  303. """Add several students to the class."""
  304. pass
  305.  
  306. def remove_student(self, student: Student):
  307. """Remove student from the class."""
  308. pass
  309.  
  310. def get_grade_sheet(self):
  311. """
  312. Return grade sheet as a table.
  313.  
  314. Grade sheet includes information of all the students in the class and their final grades.
  315. All edges should be either "|" or "-".
  316. First column is student's name and the second column is the final grade (weighted average).
  317. First, second and third row should look something like this (notice the capital letters):
  318. ----------------------
  319. | Name | Final grade |
  320. ----------------------
  321.  
  322. Make sure that all the columns are correctly aligned after the longest element.
  323. For example, consider following rows:
  324. | Ago | 5 |
  325. | Some really long name | 3 |
  326.  
  327. Rules are following:
  328. Each row (except for "-----" rows) starts with "|" and a space " " and ends with a space " " and "|".
  329. Text in "Name" column needs to be aligned to left
  330. Grades in "Final grade" column need to be centered
  331.  
  332. Students in the table should follow the order which they were added to the class.
  333.  
  334. The whole table would look something like this:
  335. ---------------------------------------
  336. | Name | Final grade |
  337. ---------------------------------------
  338. | Ago | 5 |
  339. | Johannes | 4 |
  340. | Mari | 5 |
  341. | Some really long name | 3 |
  342. ---------------------------------------
  343.  
  344. """
  345. pass
  346.  
  347.  
  348. if __name__ == '__main__':
  349. assert sum_of_multiples(20, 5) == 30
  350.  
  351. assert mix_string("AAA", "bbb") == "AbAbAb"
  352.  
  353. assert bingo([
  354. [5, 7, 11, 15, 21],
  355. [22, 25, 26, 27, 9],
  356. [34, 2, 48, 54, 58],
  357. [59, 61, 33, 81, 24],
  358. [90, 37, 3, 6, 32],
  359. ], [5, 21, 90, 32]) == (True, False, False)
  360.  
  361. assert bingo([
  362. [5, 7, 11, 15, 21],
  363. [22, 25, 26, 27, 9],
  364. [34, 2, 48, 54, 58],
  365. [59, 61, 33, 81, 24],
  366. [90, 37, 3, 6, 32],
  367. ], [5, 21, 90, 32, 25, 48, 81, 27, 61, 91]) == (True, True, False)
  368.  
  369. assert can_reach([4, 2, 3, 0, 3, 1, 2], 5) is True
  370.  
  371. assert prime_factorization(1960) == {2: 3, 5: 1, 7: 2}
  372.  
  373. candy_shop = CandyShop()
  374. candy1 = Candy('candy1', 'chocolate')
  375. candy2 = Candy('candy2', 'caramel')
  376. candy3 = Candy('candy3', 'nut')
  377. candy4 = Candy('candy1', 'chocolate')
  378. candy5 = Candy('candy2', 'vanilla')
  379. candy6 = Candy('candy2', 'vanilla')
  380. candy7 = Candy('candy3', 'nut')
  381. candy8 = Candy('candy1', 'chocolate')
  382.  
  383. candies = [candy1, candy2, candy3, candy4, candy5, candy6, candy7, candy8]
  384.  
  385. candy_shop.add_candies(candies)
  386.  
  387. # NB! there are candy variable names in comments, not instance name parameter values!!!
  388.  
  389. print(candy_shop.get_candies_by_filling('chocolate')) # [candy1, candy4, candy8]
  390. print(candy_shop.get_least_popular_candy_name_and_filling()) # {name: candy2, filling: caramel}
  391. print(candy_shop.get_most_popular_candy_name_and_filling()) # {name: candy1, filling: chocolate}
  392. print(candy_shop.sort_candies_by_filling()) # [candy2, candy1, candy4, candy8, candy7, candy3, candy6, candy5]
  393. print(candy_shop.collect_candies_by_filling()) # {chocolate: [candy1, candy4, candy8],
  394. # caramel: [candy2],
  395. # nut: [candy3, candy7],
  396. # vanilla: [candy5, candy6]}
  397.  
  398. # Teacher, grade, student
  399. mari = Student("Mari Maa")
  400. jyri = Student("Jyri Jogi")
  401. teele = Student("Teele Tee")
  402. cl = Class("Anna", [mari, jyri, teele])
  403. mari.grade(Grade(5, 3, "KT", "01/09/2020"))
  404. gr = Grade("!", 3, "KT", "01/09/2020")
  405. jyri.grade(gr)
  406. teele.grade(Grade(4, 3, "KT", "01/09/2020"))
  407.  
  408. print(f"Jyri keskmine hinne on {jyri.calculate_weighted_average()}.") # 1
  409.  
  410. jyri.redo_assignment(3, "KT", "14/09/2020")
  411. print(len(gr.previous_grades)) # 1
  412.  
  413. print(f"Jyri keskmine hinne on nyyd {jyri.calculate_weighted_average()}.") # 3
  414. print()
  415.  
  416. mari.grade(Grade(5, 1, "TK", "30/11/2020"))
  417. jyri.grade(Grade(4, 1, "TK", "30/11/2020"))
  418. teele.grade(Grade(3, 1, "TK", "30/11/2020"))
  419.  
  420. print(f"Teele keskmine hinne on {teele.calculate_weighted_average()}.") # 4
  421. print(cl.get_grade_sheet())
  422. print()
  423.  
  424. tuuli = Student("Tuuli Karu")
  425. cl.add_student(tuuli)
  426. print(len(cl.students)) # 4
Add Comment
Please, Sign In to add comment