Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. """
  2. This is a list of functions that should be completed.
  3. """
  4.  
  5. from typing import Any
  6. from typing import List
  7.  
  8.  
  9. class OurAwesomeException(Exception):
  10. pass
  11.  
  12.  
  13. def is_two_object_has_same_value(first: Any, second: Any) -> bool:
  14. """
  15. If @first and @second has same value should return True
  16. In another case should return False
  17. """
  18. return first == second
  19.  
  20.  
  21. def is_two_objects_has_same_type(first: Any, second: Any) -> bool:
  22. """
  23. If @first and @second has same type should return True
  24. In another case should return False
  25. """
  26. return type(first) == type(second)
  27.  
  28.  
  29. def is_two_objects_is_the_same_objects(first: Any, second: Any) -> bool:
  30. """
  31. If @first and @second has same type should return True
  32. In another case should return False
  33. """
  34. return id(first) == id(second)
  35.  
  36.  
  37. def multiple_ints(first_value: int, second_value: int) -> int:
  38. """
  39. Should calculate product of all args.
  40. if first_value or second_value is not int should raise ValueError
  41.  
  42. Raises:
  43. ValueError
  44.  
  45. Params:
  46. first_value: value for multiply
  47. second_value
  48. Returns:
  49. Product of elements
  50. """
  51. # if int(first_value) and int(second_value):
  52. # return first_value * second_value
  53. # else:
  54. # raise ValueError
  55. try:
  56. val1=int(first_value)
  57. val2=int(second_value)
  58. return first_value * second_value
  59. except TypeError:
  60. raise ValueError
  61.  
  62. def multiple_ints_with_conversion(first_value: Any, second_value: Any) -> int:
  63. """
  64. If possible to convert arguments to int value - convert and multiply them.
  65. If it is impossible raise OurAwesomeException
  66.  
  67. Args:
  68. first_value: number for multiply
  69. second_value: number for multiply
  70.  
  71. Raises:
  72. OurAwesomeException
  73.  
  74. Returns: multiple of two numbers.
  75.  
  76. Examples:
  77. multiple_ints_with_conversion(6, 6)
  78. >>> 36
  79. multiple_ints_with_conversion(2, 2.0)
  80. >>> 4
  81. multiple_ints_with_conversion("12", 1)
  82. >>> 12
  83. try:
  84. multiple_ints_with_conversion("Hello", 2)
  85. except ValueError:
  86. print("Not valid input data")
  87. >>> "Not valid input data"
  88. """
  89. first_value = int(first_value)
  90. second_value = int(second_value)
  91. try:
  92. return first_value * second_value
  93. except ValueError:
  94. raise OurAwesomeException
  95. print("Not valid inpup data")
  96.  
  97.  
  98. def is_word_in_text(word: str, text: str) -> bool:
  99. """
  100. If text contain word return True
  101. In another case return False.
  102.  
  103. Args:
  104. word: Searchable substring
  105. text: Text for searching
  106.  
  107. Examples:
  108. is_word_in_text("Hello", "Hello word")
  109. >>> True
  110. is_word_in_text("Glad", "Nice to meet you ")
  111. >>> False
  112.  
  113. """
  114. if word in text:
  115. return True
  116. else:
  117. return False
  118.  
  119.  
  120. def some_loop_exercise() -> list:
  121. """
  122. Use loop to create list that contain int values from 0 to 12 except 6 and 7
  123. """
  124. our_list = list(range(0,13))
  125. for i in our_list:
  126. if i == 6:
  127. our_list.remove(i)
  128. for j in our_list:
  129. if j == 7:
  130. our_list.remove(j)
  131.  
  132. return our_list
  133. #shitcode
  134.  
  135. def remove_from_list_all_negative_numbers(data: List[int]) -> list:
  136. """
  137. Use loops to solve this task.
  138. You could use data.remove(negative_number) to solve this issue.
  139. Also you could create new list with only positive numbers.
  140. Examples:
  141. remove_from_list_all_negative_numbers([1, 5, -7, 8, -1])
  142. >>> [1, 5, 8]
  143. """
  144. right = data[:]
  145. for i in data:
  146. if i < 0:
  147. right.remove(i)
  148. return right
  149.  
  150.  
  151. def alphabet() -> dict:
  152. """
  153. Create dict which keys is alphabetic characters. And values their number in alphabet
  154. Notes You could see an implementaion of this one in test, but create another one
  155. Examples:
  156. alphabet()
  157. >>> {"a": 1, "b": 2 ...}
  158. """
  159. letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
  160. 'v', 'w', 'x', 'y', 'z']
  161. numbers = list(range(1, len(letters) + 1))
  162. return dict(zip(numbers, letters))
  163.  
  164.  
  165. def simple_sort(data: List[int]) -> List[list]:
  166. """
  167. Sort list of ints without using built-in methods.
  168. Examples:
  169. simple_sort([2, 9, 6, 7, 3, 2, 1])
  170. >>> [1, 2, 2, 3, 6, 7, 9]
  171. Returns:
  172.  
  173. """
  174. new_list = []
  175.  
  176. while data:
  177. min = data[0]
  178. for x in data:
  179. if x < min:
  180. min = x
  181. new_list.append(min)
  182. data.remove(min)
  183.  
  184. return new_list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement