Question: Hello, I am stuck on a Python assignment, the code is provided below. I have to have the following functions: 1. index(item) returns the position
Hello, I am stuck on a Python assignment, the code is provided below. I have to have the following functions:
1. index(item) returns the position of item in the list. It needs the item and returns the index. Assume the item is in the list.
2. pop() removes and returns the last item in the list. It needs nothing and returns an item. Assume the list has at least one item.
3. pop(pos) removes and returns the item at position pos. It needs the position and returns the item. Assume the item is in the list.
4. a function that counts the number of times an item occurs in the linked list
5. a function that would delete the replicate items in the linked list (i.e. leave one occurrence only of each item in the linked list)
Your main function should do the following:
Generate 15 random integer numbers in the range from 1 to 5.
Insert each number (Item in a node) in the appropriate position in a linked list, so you will have a sorted linked list in ascending order.
Display the generated linked list items.
Call the index(item), pop() and pop(pos) functions to test them.
Display the linked list items
Display the number of occurrences of each item.
Delete the replicate items in the linked list (i.e. leave one occurrence only of each item in the linked list)
Display the final linked list items that should be unique and sorted.
Make sure your code is readable and well-documented. It must begin with a title block includes problem definition. Each function must also begin with a title block that describes the task of the function, input parameters and return value.
LinkedList code:
from Node import Node import random class LinkedList: def __init__(self): self.linkedlists = None def emptylist(self): return self.linkedlists == None def items(self, item): value= Node(item) value.setNext(self.linkedlists) self.linkedlists=value def length(self): beginning=self.linkedlists count=0 while beginning != None: count = count + 1 beginning = beginning.getNext() return count def programmethond(self): beginning=self.linkedlists while beginning != None: print(beginning.getData()) beginning=beginning.getNext() def search(self,item): counter = 0 beginning = self.linkedlists found = False stop = False while beginning != None and not found and not stop: if beginning.getData() == item: found= True else: if beginning.getData() > item: stop = True else: counter = counter + 1 beginning = beginning.getNext() return counter def search2(self,item): counter = 0 beginning = self.linkedlists found = False stop = False while beginning != None and not found and not stop: if beginning.getData() == item: found = True else: if beginning.getData() > item: stop = True else: counter= counter + 1 beginning = beginning.getNext() return found def remove(self, item): beginning = self.linkedlists previous = None found = False while beginning != None and not found: if beginning.getData() == item: found = True else: previous = beginning beginning = beginning.getNext() if found: if previous == None: self.linkedlists = beginning.getNext() else: previous.setNext(beginning.getNext()) def add(self, item): beginning=self.linkedlists previous =None stop= False while beginning != None and not stop: if beginning.getData() > item: stop= True else: previous=beginning beginning = beginning.getNext() value=Node(item) if previous == None: value.setNext(self.linkedlists) self.linkedlists = value else: value.setNext(beginning) previous.setNext(value) def pop(self): beginning=self.linkedlists previous=None while beginning.getNext() != None: previous=beginning beginning = beginning.getNext() previous.setNext(None) def popPos(self, pos): beginning= self.linkedlists pevious= None counter= 0 while counter != pos: counter += 1 previous= beginning beginning =beginning.getNext() previous=beginning.getNext() def multipleIntegers(self): beginning= self.linkedlists item= None counter= 0 while beginning != None: if beginning.getData() != item: value=self.linkedlists item = beginning.getData() while value != None: if value.getData() == item: counter += 1 value=value.getNext() print(repr(item) + " repeated " + repr(counter) + " times.") beginning=beginning.getNext() counter=0 def duplicateIntegers(self): count = 0 beginning=self.linkedlists for value in range(1,6): while beginning != None: if beginning.getData() == value: count += 1 beginning = beginning.getNext() while self.search2(value) == True: if count > 1: self.remove(value) count -= 1 else: break beginning =self.linkedlists count =0 def main(): a=[] selfs=LinkedList() #how many integers you need to generate for value in range(15): a.append(random.randrange(1, 6)) a= sorted(a, reverse=True) for value in a: selfs.add(value) print("Display fifteen random integers:") selfs.programmethond() print("List of random integers size:", selfs.length()) #print("Is 5 in the list? ", selfs.search2(5)) print("Pop from the list.") selfs.pop() print("Display the list.") selfs.programmethond() print("List size:", selfs.length()) print("Popping 3rd index position from the list.") selfs.popPos(3) print("Display the list.") selfs.programmethond() print("List size:", selfs.length()) selfs.multipleIntegers() selfs.duplicateIntegers() print("Display the numbers that appear in the list, but only once: ") selfs.programmethond() if __name__ == "__main__": main()
class Node: ''' Create a Node object and initialize its data. ''' def __init__(self, init_data): self.data = init_data self.next = None def get_data(self): return self.data def get_next(self): return self.next def set_data(self, new_data): self.data = new_data def set_next(self, new_next): self.next = new_next
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
