Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from copy import deepcopy
- # Create a Node class to create a node
- class Node:
- def __init__(self, n_arr=4):
- self.array = []
- self.next = None
- # Create a LinkedList class
- class LinkedList:
- def __init__(self, n_array=4):
- self.__head = None
- self.n_array = n_array
- self.__length = 0
- def __len__(self):
- return self.__length
- def search_index_by_value(self, value):
- tmp = self.__head
- count = 0
- while tmp:
- if value in tmp.array:
- return count + tmp.array.index(value)
- else:
- tmp = tmp.next
- count += self.n_array
- raise ValueError
- def insert_by_index(self, value, abs_index=-1):
- tmp = self.__head
- if abs_index < 0:
- abs_index = self.__length + abs_index + 1
- elif abs_index > self.__length:
- raise IndexError("Incorrect index")
- if tmp is None:
- tmp = Node()
- # tmp.array.append(value)
- self.__head = tmp
- tmp_len = 0
- while tmp:
- if tmp_len <= abs_index <= tmp_len + self.n_array:
- actual_index = abs_index - tmp_len
- tmp.array.insert(actual_index, value)
- self.__length += 1
- break
- else:
- tmp = tmp.next
- tmp_len += self.n_array
- while len(tmp.array) > self.n_array:
- if tmp.next:
- tmp.next.array.insert(0, tmp.array.pop())
- else:
- tmp.next = Node()
- tmp.next.array.append(tmp.array.pop())
- tmp = tmp.next
- def delete_by_index(self, abs_index=-1):
- tmp = self.__head
- if abs_index < 0:
- abs_index = self.__length + abs_index
- tmp_len = 0
- while tmp_len + len(tmp.array) - 1 < abs_index:
- tmp_len += self.n_array
- tmp = tmp.next
- tmp.array.pop(abs_index - tmp_len)
- self.__length -= 1
- while len(tmp.array) < (self.n_array // 2):
- if tmp.next:
- tmp.array.append(tmp.next.array.pop(0))
- tmp = tmp.next
- else:
- break
- if len(tmp.array) == 0:
- tmp = self.__head
- while tmp.next.next:
- tmp = tmp.next
- tmp.next = None
- def __str__(self):
- tmp = self.__head
- output = []
- while tmp:
- output += [tmp.array]
- tmp = tmp.next
- return ''.join(map(str, output))
- def array(self):
- tmp = self.__head
- res_lst = []
- while tmp:
- res_lst.extend(tmp.array)
- tmp = tmp.next
- return res_lst
- def set(self):
- tmp = self.__head
- res_lst = []
- while tmp:
- for i in tmp.array:
- if i not in res_lst:
- res_lst.append(i)
- tmp = tmp.next
- return res_lst
- def count(self):
- dictionary = dict()
- tmp = self.__head
- while tmp:
- for i in tmp.array:
- try:
- dictionary.get(i)
- dictionary[i] += 1
- except KeyError:
- dictionary[i] = 1
- tmp = tmp.next
- return dictionary
- def transform(self, n):
- tmp = self.__head
- ll = LinkedList(n_array=n)
- while tmp:
- for i in tmp.array:
- ll.insert_by_index(i)
- tmp = tmp.next
- return ll
- def dublicate(self):
- tmp = self.__head
- arr = []
- while tmp:
- for i in tmp.array:
- arr.extend([i] * i)
- tmp = tmp.next
- new_tmp = self.__head
- new_tmp.next = None
- new_tmp.array = arr
- while len(new_tmp.array) > self.n_array:
- if new_tmp.next:
- new_tmp.next.array.insert(0, new_tmp.array.pop())
- else:
- new_tmp.next = Node()
- new_tmp.next.array.append(new_tmp.array.pop())
- new_tmp = new_tmp.next
- def remove_repeats(self):
- check_dict = self.count()
- tmp = self.__head
- while tmp:
- arr = deepcopy(tmp.array)
- for i in arr:
- ind = self.search_index_by_value(i)
- if check_dict[i] != 1:
- self.delete_by_index(ind)
- check_dict[i] -= 1
- tmp = tmp.next
- def maximize(self, n):
- check_dict = self.count()
- delete_lst = [i for i in check_dict.keys() if check_dict[i] > n]
- tmp = self.__head
- while tmp:
- arr = deepcopy(tmp.array)
- for i in arr:
- if i in delete_lst:
- ind = self.search_index_by_value(i)
- self.delete_by_index(ind)
- tmp = tmp.next
- ll = LinkedList()
- ll.insert_by_index(1)
- ll.insert_by_index(1)
- ll.insert_by_index(1)
- ll.insert_by_index(2)
- ll.insert_by_index(3)
- ll.insert_by_index(4)
- ll.insert_by_index(5)
- ll.insert_by_index(6)
- ll.insert_by_index(6)
- print(str(ll))
- arr = ll.array()
- print(arr)
- s = ll.set()
- print(s)
- d = ll.count()
- print(d)
- new_ll = ll.transform(6)
- print(new_ll)
- ll2 = LinkedList()
- ll2.insert_by_index(1)
- ll2.insert_by_index(2)
- ll2.insert_by_index(3)
- ll2.insert_by_index(4)
- ll2.dublicate()
- print(str(ll2))
- ll2.remove_repeats()
- print(ll2)
- ll3 = LinkedList()
- ll3.insert_by_index(2)
- ll3.insert_by_index(1)
- ll3.insert_by_index(1)
- ll3.insert_by_index(1)
- print(str(ll3))
- ll3.maximize(2)
- print(str(ll3))
- def check(arr1, arr2, n_array=4):
- testing_list = LinkedList(n_array)
- for i in arr1:
- testing_list.insert_by_index(i)
- print(str(testing_list))
- for i in arr2:
- testing_list.delete_by_index(i)
- print(str(testing_list))
- return testing_list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement