Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. # Cea mai lunga secventa de patrate perfecte si care au numarul de cifre in ordine descrescatoare
  2.  
  3. import math
  4.  
  5. '''
  6. Citire
  7. '''
  8.  
  9.  
  10. def read_array():
  11. """
  12. Reads an array of values and returns it.
  13. :return: array, the resulting array.
  14. """
  15. array = []
  16. item_count = str(input("How many values? "))
  17. if item_count.isnumeric() and int(item_count) > 0:
  18. for _ in range(int(item_count)):
  19. value = input("Value " + str(_) + ": ")
  20. if not value.isnumeric():
  21. array = []
  22. break
  23. else:
  24. array.append(int(value))
  25. print("")
  26. return array
  27.  
  28.  
  29. '''
  30. Cerinta 1
  31. '''
  32.  
  33.  
  34. def is_square_number(number):
  35. """
  36. Checks if the given number is a square number
  37. :param number: int, given number.
  38. :return: bool, False if given number is not a square number, True if otherwise.
  39. """
  40. if type(number) is not int:
  41. return False
  42. if number < 0:
  43. return False
  44. if math.sqrt(number) % 1.0 == 0:
  45. return True
  46. else:
  47. return False
  48.  
  49.  
  50. def test_is_square_number():
  51. assert is_square_number(0) is True
  52. assert is_square_number(-1) is False
  53. assert is_square_number(2) is False
  54. assert is_square_number(4) is True
  55. assert is_square_number(1) is True
  56.  
  57.  
  58. def first_assignment(array):
  59. """
  60. Returns the longest sequence of square numbers in given array.
  61. :param array: array, given array of numbers.
  62. :return: array, longest sequence of square numbers.
  63. """
  64. max_start_index = 0
  65. max_length = 0
  66. curr_start_index = 0
  67. curr_length = 0
  68.  
  69. for _ in range(len(array)):
  70. if is_square_number(array[_]):
  71. if curr_length == 0:
  72. curr_start_index = _
  73. curr_length += 1
  74. if curr_length > max_length:
  75. max_length = curr_length
  76. max_start_index = curr_start_index
  77. else:
  78. curr_length = 0
  79.  
  80. if max_length > 0:
  81. index_array = []
  82. for _ in range(max_start_index, max_start_index+max_length):
  83. index_array.append(_)
  84. return index_array
  85.  
  86.  
  87. def test_first_assignment():
  88. assert first_assignment([0, 1, "test", 4, 9, 16, 3.14, 64]) == [3, 4, 5]
  89. assert first_assignment(["test"]) is None
  90. assert first_assignment([-1, -524, "fine", 4, 4, 4, "test"]) == [3, 4, 5]
  91.  
  92.  
  93. '''
  94. Cerinta 2
  95. '''
  96.  
  97.  
  98. def number_of_digits(number):
  99. """
  100. Returns the number of digits of the given number.
  101. :param number: int, given number.
  102. :return: int, number of digits of given number.
  103. """
  104. if type(number) is float and number % 1.0 == 0.0:
  105. return len(str(abs(int(number))))
  106. if type(number) is not int:
  107. return 0
  108. return len(str(abs(number)))
  109.  
  110.  
  111. def test_number_of_digits():
  112. assert number_of_digits(123) == 3
  113. assert number_of_digits(1) == 1
  114. assert number_of_digits("uranus") == 0
  115. assert number_of_digits(-4) == 1
  116.  
  117.  
  118. def second_assignment(array):
  119. """
  120. Returns the longest sequence of numbers that have a decreasing number of digits.
  121. :param array: array, given array of numbers.
  122. :return: array, longest sequence of numbers that have a decreasing number of digits.
  123. """
  124. max_start_index = 0
  125. max_length = 0
  126. curr_start_index = 0
  127. curr_length = 0
  128. curr_digits = 0
  129.  
  130. for _ in range(len(array)):
  131. if number_of_digits(array[_]) > 0:
  132. if curr_digits == 0:
  133. curr_digits = number_of_digits(array[_])
  134. curr_length = 1
  135. curr_start_index = _
  136. else:
  137. if curr_digits > number_of_digits(array[_]):
  138. curr_digits = number_of_digits(array[_])
  139. curr_length += 1
  140. else:
  141. curr_digits = number_of_digits(array[_])
  142. curr_length = 1
  143. curr_start_index = _
  144. if curr_length > max_length:
  145. max_start_index = curr_start_index
  146. max_length = curr_length
  147. if max_length > 0:
  148. index_array = []
  149. for _ in range(max_start_index, max_start_index + max_length):
  150. index_array.append(_)
  151. return index_array
  152.  
  153.  
  154. def test_second_assignment():
  155. assert second_assignment([1, 12, 123, 12, 1]) == [2, 3, 4]
  156. assert second_assignment(["test", "test2", "test 3231"]) is None
  157. assert second_assignment([1, 12, 123, -12, -1]) == [2, 3, 4]
  158.  
  159.  
  160. '''
  161. Testing
  162. '''
  163.  
  164. test_is_square_number()
  165. test_first_assignment()
  166. test_number_of_digits()
  167. test_second_assignment()
  168.  
  169. '''
  170. Meniu
  171. '''
  172.  
  173.  
  174. def user_interface():
  175. trigger = False
  176. array = []
  177. while not trigger:
  178. print("")
  179. print("Choose an action: ")
  180. print("")
  181. print("1. Input values")
  182. print("2. Print longest sequence of square numbers in given list of values.")
  183. print("3. Print longest sequence of numbers that have a decreasing number of digits.")
  184. print("4. Exit")
  185. print("")
  186. choice = str(input("Choice: "))
  187. print("")
  188. if len(choice) == 1 and choice.isdigit():
  189. if choice == '1':
  190. array = read_array()
  191. else:
  192. if choice == '2':
  193. if len(array) == 0:
  194. print("You must input some values first...")
  195. array = read_array()
  196. if len(array) >= 0:
  197. print("Original array: ")
  198. print(array)
  199. print("Index array: ")
  200. print(first_assignment(array))
  201. result = []
  202. for _ in first_assignment(array):
  203. result.append(array[_])
  204. print("Result: ")
  205. print(result)
  206.  
  207. else:
  208. print("Original array: ")
  209. print(array)
  210. print("Index array: ")
  211. print(first_assignment(array))
  212. result = []
  213. for _ in first_assignment(array):
  214. result.append(array[_])
  215. print("Result: ")
  216. print(result)
  217. else:
  218. if choice == '3':
  219. if len(array) == 0:
  220. print("You must input some values first...")
  221. array = read_array()
  222. if len(array) >= 0:
  223. print("Original array: ")
  224. print(array)
  225. print("Index array: ")
  226. print(second_assignment(array))
  227. result = []
  228. for _ in second_assignment(array):
  229. result.append(array[_])
  230. print("Result: ")
  231. print(result)
  232. else:
  233. print("Original array: ")
  234. print(array)
  235. print("Index array: ")
  236. print(second_assignment(array))
  237. result = []
  238. for _ in second_assignment(array):
  239. result.append(array[_])
  240. print("Result: ")
  241. print(result)
  242. else:
  243. if choice == '4':
  244. trigger = True
  245. else:
  246. print("That's not a valid choice!")
  247. else:
  248. print("That's not a valid choice!")
  249.  
  250.  
  251. user_interface()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement