Advertisement
maxim_shlyahtin

lb1

Oct 6th, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. from copy import deepcopy
  2. # Create a Node class to create a node
  3. class Node:
  4. def __init__(self, n_arr=4):
  5. self.array = []
  6. self.next = None
  7.  
  8.  
  9. # Create a LinkedList class
  10.  
  11.  
  12. class LinkedList:
  13. def __init__(self, n_array=4):
  14. self.__head = None
  15. self.n_array = n_array
  16. self.__length = 0
  17.  
  18. def __len__(self):
  19. return self.__length
  20.  
  21. def search_index_by_value(self, value):
  22. tmp = self.__head
  23. count = 0
  24.  
  25. while tmp:
  26. if value in tmp.array:
  27. return count + tmp.array.index(value)
  28. else:
  29. tmp = tmp.next
  30. count += self.n_array
  31. raise ValueError
  32.  
  33. def insert_by_index(self, value, abs_index=-1):
  34. tmp = self.__head
  35.  
  36. if abs_index < 0:
  37. abs_index = self.__length + abs_index + 1
  38. elif abs_index > self.__length:
  39. raise IndexError("Incorrect index")
  40.  
  41. if tmp is None:
  42. tmp = Node()
  43. # tmp.array.append(value)
  44. self.__head = tmp
  45.  
  46. tmp_len = 0
  47. while tmp:
  48. if tmp_len <= abs_index <= tmp_len + self.n_array:
  49. actual_index = abs_index - tmp_len
  50. tmp.array.insert(actual_index, value)
  51. self.__length += 1
  52. break
  53. else:
  54. tmp = tmp.next
  55. tmp_len += self.n_array
  56. while len(tmp.array) > self.n_array:
  57. if tmp.next:
  58. tmp.next.array.insert(0, tmp.array.pop())
  59. else:
  60. tmp.next = Node()
  61. tmp.next.array.append(tmp.array.pop())
  62. tmp = tmp.next
  63.  
  64. def delete_by_index(self, abs_index=-1):
  65. tmp = self.__head
  66.  
  67. if abs_index < 0:
  68. abs_index = self.__length + abs_index
  69.  
  70. tmp_len = 0
  71. while tmp_len + len(tmp.array) - 1 < abs_index:
  72. tmp_len += self.n_array
  73. tmp = tmp.next
  74. tmp.array.pop(abs_index - tmp_len)
  75. self.__length -= 1
  76.  
  77. while len(tmp.array) < (self.n_array // 2):
  78. if tmp.next:
  79. tmp.array.append(tmp.next.array.pop(0))
  80. tmp = tmp.next
  81. else:
  82. break
  83.  
  84. if len(tmp.array) == 0:
  85. tmp = self.__head
  86. while tmp.next.next:
  87. tmp = tmp.next
  88. tmp.next = None
  89.  
  90. def __str__(self):
  91. tmp = self.__head
  92. output = []
  93. while tmp:
  94. output += [tmp.array]
  95. tmp = tmp.next
  96. return ''.join(map(str, output))
  97.  
  98. def array(self):
  99. tmp = self.__head
  100. res_lst = []
  101. while tmp:
  102. res_lst.extend(tmp.array)
  103. tmp = tmp.next
  104. return res_lst
  105.  
  106. def set(self):
  107. tmp = self.__head
  108. res_lst = []
  109. while tmp:
  110. for i in tmp.array:
  111. if i not in res_lst:
  112. res_lst.append(i)
  113. tmp = tmp.next
  114. return res_lst
  115.  
  116. def count(self):
  117. dictionary = dict()
  118. tmp = self.__head
  119. while tmp:
  120. for i in tmp.array:
  121. try:
  122. dictionary.get(i)
  123. dictionary[i] += 1
  124. except KeyError:
  125. dictionary[i] = 1
  126. tmp = tmp.next
  127. return dictionary
  128.  
  129. def transform(self, n):
  130. tmp = self.__head
  131. ll = LinkedList(n_array=n)
  132. while tmp:
  133. for i in tmp.array:
  134. ll.insert_by_index(i)
  135. tmp = tmp.next
  136. return ll
  137.  
  138. def dublicate(self):
  139. tmp = self.__head
  140. arr = []
  141. while tmp:
  142. for i in tmp.array:
  143. arr.extend([i] * i)
  144. tmp = tmp.next
  145. new_tmp = self.__head
  146. new_tmp.next = None
  147. new_tmp.array = arr
  148. while len(new_tmp.array) > self.n_array:
  149. if new_tmp.next:
  150. new_tmp.next.array.insert(0, new_tmp.array.pop())
  151. else:
  152. new_tmp.next = Node()
  153. new_tmp.next.array.append(new_tmp.array.pop())
  154. new_tmp = new_tmp.next
  155.  
  156. def remove_repeats(self):
  157. check_dict = self.count()
  158. tmp = self.__head
  159. while tmp:
  160. arr = deepcopy(tmp.array)
  161. for i in arr:
  162. ind = self.search_index_by_value(i)
  163. if check_dict[i] != 1:
  164. self.delete_by_index(ind)
  165. check_dict[i] -= 1
  166. tmp = tmp.next
  167.  
  168. def maximize(self, n):
  169. check_dict = self.count()
  170. delete_lst = [i for i in check_dict.keys() if check_dict[i] > n]
  171. tmp = self.__head
  172. while tmp:
  173. arr = deepcopy(tmp.array)
  174. for i in arr:
  175. if i in delete_lst:
  176. ind = self.search_index_by_value(i)
  177. self.delete_by_index(ind)
  178. tmp = tmp.next
  179.  
  180. ll = LinkedList()
  181. ll.insert_by_index(1)
  182. ll.insert_by_index(1)
  183. ll.insert_by_index(1)
  184. ll.insert_by_index(2)
  185. ll.insert_by_index(3)
  186. ll.insert_by_index(4)
  187. ll.insert_by_index(5)
  188. ll.insert_by_index(6)
  189. ll.insert_by_index(6)
  190. print(str(ll))
  191. arr = ll.array()
  192. print(arr)
  193. s = ll.set()
  194. print(s)
  195. d = ll.count()
  196. print(d)
  197. new_ll = ll.transform(6)
  198. print(new_ll)
  199. ll2 = LinkedList()
  200. ll2.insert_by_index(1)
  201. ll2.insert_by_index(2)
  202. ll2.insert_by_index(3)
  203. ll2.insert_by_index(4)
  204. ll2.dublicate()
  205. print(str(ll2))
  206. ll2.remove_repeats()
  207. print(ll2)
  208. ll3 = LinkedList()
  209. ll3.insert_by_index(2)
  210. ll3.insert_by_index(1)
  211. ll3.insert_by_index(1)
  212. ll3.insert_by_index(1)
  213. print(str(ll3))
  214. ll3.maximize(2)
  215. print(str(ll3))
  216.  
  217.  
  218. def check(arr1, arr2, n_array=4):
  219. testing_list = LinkedList(n_array)
  220.  
  221. for i in arr1:
  222. testing_list.insert_by_index(i)
  223. print(str(testing_list))
  224.  
  225. for i in arr2:
  226. testing_list.delete_by_index(i)
  227. print(str(testing_list))
  228.  
  229. return testing_list
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement