Advertisement
Alekal

Untitled

Dec 10th, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. def segment_number(first_number, last_number):
  2. """
  3. Return list with elements dividable by 5 but not dividable by 3 between (inclusive) two arguments.
  4. Return list of numbers where only numbers between first_number
  5. and last_number (both inclusive) which divide by 5 but do not divide by 3
  6. are used.
  7. #1
  8.  
  9. :param first_number: the lowest possible candidate
  10. :param last_number: the highest possible candidate
  11. :return: list of numbers
  12. """
  13. list = []
  14. for i in range(first_number, last_number + 1):
  15. if i % 5 == 0 and i % 3 != 0:
  16. list.append(i)
  17. return list
  18.  
  19.  
  20. def add_or_subtract(numbers):
  21. """
  22. Return the sum of all numbers in a list.
  23.  
  24. The sum is calculated according to following rules:
  25. -always start by adding all the numbers together.
  26. -if you find a 0, start subtracting all following numbers until you find another 0, then start adding again.
  27. -there might be more than two 0 in a list - change +/- with every 0 you find.
  28.  
  29. For example:
  30. [1, 2, 0, 3, 0, 4] -> 1 + 2 - 3 + 4 = 4
  31. [0, 2, 1, 0, 1, 0, 2] -> -2 - 1 + 1 - 2 = -4
  32. [1, 2] -> 1 + 2 = 3
  33. [4, 0, 2, 3] = 4 - 2 - 3 = -1
  34.  
  35. #2
  36.  
  37. :param numbers: the list of number given.
  38. :return: the sum of all numbers.
  39. """
  40.  
  41. total = 0
  42. summary = True
  43. for i in range(len(numbers)):
  44. while summary is True:
  45. if len(numbers) == 0:
  46. break
  47. elif numbers[0] != 0:
  48. total += int(numbers[0])
  49. numbers.remove(numbers[0])
  50. elif numbers[0] == 0:
  51. numbers.remove(numbers[0])
  52. summary = False
  53. while summary is False:
  54. if len(numbers) == 0:
  55. break
  56. elif numbers[0] != 0:
  57. total -= int(numbers[0])
  58. numbers.remove(numbers[0])
  59. elif numbers[0] == 0:
  60. numbers.remove(numbers[0])
  61. summary = True
  62. return total
  63.  
  64.  
  65. def should_get_up_early(is_weekday, really_tired, first_class_is_programming):
  66. """
  67. Decide if you should get up early.
  68.  
  69. You should only even consider getting up early if it is a weekday, on weekends you should never get up early.
  70. If it is a weekday you should typically get up early, unless you are really tired.
  71. But if it is a weekday and you are really tired but the first class is a programming class you should still get up
  72. early ignoring you being tired.
  73.  
  74. #3
  75.  
  76. :param is_weekday: is it a weekday or not, boolean
  77. :param really_tired: are you really tired, boolean
  78. :param first_class_is_programming: is the first class a programming class, boolean
  79. :return: True if you should get up early, otherwise False
  80. """
  81. if is_weekday:
  82. if really_tired:
  83. if first_class_is_programming:
  84. return True
  85. if not first_class_is_programming:
  86. return False
  87. if not really_tired:
  88. return True
  89. if not is_weekday:
  90. return False
  91.  
  92.  
  93. def pear_fear(pears, people):
  94. """
  95. Return how many pears non-pear-fearers get.
  96.  
  97. Every 3rd person fears pears, so they won't get any.
  98. How many pears will each get?
  99. Everyone who is not afraid of pears gets equal number of pears.
  100. Only whole pears will be used, so some pears may remain.
  101.  
  102. #4
  103.  
  104. :param pears:
  105. :param people:
  106. :return:
  107. """
  108. people_who_fear = people // 3
  109. people_who_eat = people - people_who_fear
  110. how_many_pears = pears // people_who_eat
  111. return how_many_pears
  112.  
  113.  
  114. def string_between_string(word1, word2):
  115. """
  116. Insert reversed word2 to the center of word1.
  117.  
  118. word1 length is always even.
  119.  
  120. #5
  121.  
  122. :param word1: Initial word. String.
  123. :param word2: Word to reverse and insert. String.
  124. :return: New word as string.
  125. """
  126. half_word1 = (len(word1) // 2)
  127. reversed_word2 = word2[::-1]
  128. new_string = ''
  129. new_string = word1[:half_word1] + reversed_word2 + word1[half_word1:]
  130. return new_string
  131.  
  132.  
  133. def get_padded_string(string1, string2):
  134. """
  135. Pad the longer of two strings with the shorter one on both sides.
  136.  
  137. If both strings are the same length, consider string1 as the longer one.
  138. For example when string1 is "pizza" and string2 is "bbq", this should return "bbqpizzabbq".
  139.  
  140. #6
  141.  
  142. :param string1: String one
  143. :param string2: String two
  144. :return: Padded string
  145. """
  146. pass
  147.  
  148.  
  149. def remove_duplicate(number_list):
  150. """
  151. Return list where consecutive duplicates are removed.
  152.  
  153. Go though given list and remove all
  154. occurrences of two or more of the same
  155. numbers appearing after one another.
  156. Remove all but one of the duplicates.
  157.  
  158. #7
  159.  
  160. :param number_list: input list
  161. :return: new list
  162. """
  163. # list = []
  164. # i = 0
  165. # while i < len(number_list):
  166. # list.append(i)
  167. # if list[i] == number_list[i]:
  168. # i += 1
  169. # pass
  170. # else:
  171. # list.append(i)
  172. # i +=1
  173.  
  174.  
  175. def who_called(calls, name):
  176. """
  177. You are given a dictionary of calls and a name.
  178.  
  179. Determine who called that name.
  180. If nobody called the person, return -1.
  181.  
  182. #8
  183.  
  184. :param calls: dictionary of all the calls
  185. :param name: name of the receiver
  186. :return: name of the caller
  187. """
  188. if name in calls.values():
  189. for k, v in calls.items():
  190. if v == name:
  191. return k
  192. else:
  193. return -1
  194.  
  195.  
  196. def remove_lowest_digit(number):
  197. """
  198. Given a non-negative integer, remove the first occurrence of the lowest digit and return a new number.
  199.  
  200. 123 => 23
  201. 223 => 23
  202. 232 => 32
  203. 1 => 0
  204. :param number: non-negative integer
  205. :return: non-negative integer
  206. """
  207. new = ''
  208. list = []
  209. as_int = int
  210. if len(str(number)) == 1:
  211. as_int = 0
  212. elif number > 0:
  213. for i in str(number):
  214. list.append(str(i))
  215. minimal = min(list)
  216. list.remove(minimal)
  217. for i in list:
  218. new += i
  219. as_int = int(new)
  220. return as_int
  221.  
  222.  
  223. def show_highest_grade(grade1, grade2):
  224. """
  225. Print "Highest grade: GRADE" where GRADE is the higher of two inputs.
  226.  
  227. grade1, grade2 are both non-negative integers.
  228.  
  229. 3, 4 => "Highest grade: 4"
  230.  
  231. #10
  232.  
  233. :param grade1:
  234. :param grade2:
  235. :return:
  236. """
  237. if grade1 > grade2:
  238. print(f'Highest grade: {grade1}')
  239. else:
  240. print(f'Highest grade: {grade2}')
  241.  
  242.  
  243. if __name__ == '__main__':
  244.  
  245. assert segment_number(1, 11) == [5, 10]
  246. assert segment_number(1, 4) == []
  247. assert segment_number(-20, 20) == [-20, -10, -5, 5, 10, 20]
  248.  
  249. assert add_or_subtract([1, 2, 0, 3]) == 0
  250. assert add_or_subtract([0, 1, 2]) == -3
  251. assert add_or_subtract([1, 2, 0, 2, 0, 4]) == 5
  252.  
  253. assert should_get_up_early(True, True, True) is True
  254. assert should_get_up_early(False, True, False) is False
  255.  
  256. assert pear_fear(10, 3) == 5
  257. assert pear_fear(10, 5) == 2
  258. assert pear_fear(0, 3) == 0
  259. assert pear_fear(17, 2) == 8
  260. assert pear_fear(21, 10) == 3
  261.  
  262. assert string_between_string("ho", "lle") == "hello"
  263. assert string_between_string("", "yas") == "say"
  264. assert string_between_string("smrt", "a") == "smart"
  265. assert string_between_string("w d", " ro ") == "w or d"
  266. assert string_between_string(".,", ",.") == "..,,"
  267.  
  268. assert get_padded_string("pizza", "bbq") == "bbqpizzabbq"
  269. assert get_padded_string("dog", "cat") == "catdogcat"
  270. assert get_padded_string("geoff", "giraffe") == "geoffgiraffegeoff"
  271.  
  272. assert remove_duplicate([1, 1, 2, 2, 3, 3]) == [1, 2, 3]
  273. assert remove_duplicate([1, 2, 3]) == [1, 2, 3]
  274. assert remove_duplicate([1, 1, 1, 1, 1, 2, 1, 1, 3]) == [1, 2, 1, 3]
  275.  
  276. assert who_called({}, "Nathan") == -1
  277. assert who_called({"Alex": "James", "Jeff": "Bill", "James": "Alex", "Daniel": "Matt"}, "Alex") == "James"
  278. assert who_called({"Alex": "James", "Jeff": "Bill", "James": "Alex", "Daniel": "Matt"}, "Olaf") == -1
  279.  
  280. assert remove_lowest_digit(123) == 23
  281. assert remove_lowest_digit(100) == 10
  282. assert remove_lowest_digit(7) == 0
  283. assert remove_lowest_digit(171) == 71
  284.  
  285. assert show_highest_grade(10, 14) is None
  286. # prints:
  287. # Highest grade: 14
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement