Question
Pyhton - How do i fix the error, i get the same error but with diffrent attributes when i try to call other methods. AttributeError:
Pyhton - How do i fix the error, i get the same error but with diffrent attributes when i try to call other methods.
AttributeError: 'LinkedList' object has no attribute 'get_next' when calling duplicates_unsorted
AttributeError: 'function' object has no attribute 'length' when calling checkBoolean
AttributeError: 'function' object has no attribute 'next' when calling mergeSort
I am able to print the linked list using the print_list method. The single_list method opens two text files with numbers stored inside and merges them into one and then make that list into a linked list. I can print that list without any trouble.
class Node(object): item = -1 next = None
def __init__(self, item, next): self.item = item self.next = next
def has_next(self): return self.next!=None def get_next(self): return self.next def set_next(self,node): self.next=node
class LinkedList(object): # required constructor def __init__(self): #initializing root and size self.item=None self.size=0
def length(self): curr = self total = 0 while curr.next is not None: total +=1 curr = curr.next return total def add(self, item): new_node = Node(item, self.item); self.item = new_node; self.size += 1;
def print_list(self): print("Print List = ", end= ""); if self.item is None: return; current = self.item; print(current.item, end=", "); while current.get_next(): current = current.get_next(); if not current.has_next(): print(current.item, end=""); else: print(current.item, end= ", ")
def duplicates_unsorted(list): #This method would find duplicates in a unsorted list k = list duplicate_elements = [] while k is not None: j = k.get_next while j is not None: if k.item == j.item: duplicate_elements.append(k.item) j = j.get_next k = k.get_next print("Duplicates: ", duplicate_elements)
def duplicates_sorted(list): k = list duplicate_elements = [] while k is not None: if k.next is None: break if k.item == k.next.item: duplicate_elements.append(k.item) k = k.next print("Duplicates: ", duplicate_elements) def bubble_sort(list): for i in range(list.length()): m = list n = m.next while n is not None: if n.item is None: break if m.item > n.item: swap(m, n) n = n.next m = m.next
def swap(node1, node2): temp = node1.item node1.item = node2.item node2.item = temp def mergeSort(list):
if list is None or list.next is None: return list else: middle = getMiddleNode(list) nextmiddle = middle.next middle.next = None
left = mergeSort(middle) right = mergeSort(nextmiddle)
sortedList = sortMerged(left, right)
return sortedList
def sortMerged(left, right):
if left is None: return left elif right is None: return right
if (int(right.item)
def getMiddleNode(list):
if list is None or list.next is None: return list else: fast = list.next slow = list
while fast != None: fast = fast.next slow = slow.next
return slow def checkBoolean(list): seen = [False]*(list.length()+1) m = list n = m.next while n is not None: if m.item == n.item: seen[m.item] = True m = m.next if m == n: n = n.next m = list return seen def single_list(): merge_list = LinkedList() #create empty linked list with open('vivendi.txt', 'r') as myfile: #calls on vivendi.txt file for line in myfile: merge_list.add(int(line.strip())) myfile.close()
with open('activision.txt', 'r') as myfile: #calls on activision.txt file for line in myfile: merge_list.add(int(line)) myfile.close return merge_list
def main(): #creating a linked list linkedList=single_list() linkedList.print_list() duplicates_unsorted(linkedList) #bubble_sort(single_list) #single_list.print_list() #duplicates_sorted(single_list) #bubble_sort(single_list) #mergeSort(single_list) #checkBoolean(single_list)
if __name__ =='__main__': main()
vivendi.txt-... activision.txt...- File Edit Format File Edit Format View Help 586 481 12 25 85 1000 32 65 123 784 4 78 39 698 10Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started