UniQuet0p1

Untitled

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