Please help (in python) – Refer to questions attached in the images, and the starter code below (I cannot upload page 3 with example outputs for removeDuplicates(self)). Do not change the function...


Please help (in python) – Refer to questions attached in the images, and the starter code below (I cannot upload page 3 with example outputs for removeDuplicates(self)). Do not change the function names or given starter code in your script. You are not allowed to use any other data structures for the purposes of manipulating/sorting elements, nor may you use any modules from the Python to insert and remove elements from the list. You  are  not  allowed  to  swap  data  from  the  nodes  when  adding  the  node  to  be  sorted. Traversing  the  linked  list  and  updating  ‘next’  references  are  the  only  required  and acceptable operations. You are not allowed to use any kind of built-in sorting method or import any other libraries. If you are unable to complete a function, use the pass statement to avoid syntax errors.



Starter code -






class Node:
def __init__(self, value):
self.value = value

self.next = None



def __str__(self):
return "Node({})".format(self.value)

__repr__ = __str__




class SortedLinkedList:
'''
>>> x=SortedLinkedList()
>>> x.add(8.76)
>>> x.add(1)
>>> x.add(1)
>>> x.add(1)
>>> x.add(5)
>>> x.add(3)
>>> x.add(-7.5)
>>> x.add(4)
>>> x.add(9.78)
>>> x.add(4)
>>> x
Head:Node(-7.5)
Tail:Node(9.78)
List:-7.5 -> 1 -> 1 -> 1 -> 3 -> 4 -> 4 -> 5 -> 8.76 -> 9.78
>>> x.replicate()
Head:Node(-7.5)
Tail:Node(9.78)
List:-7.5 -> -7.5 -> 1 -> 1 -> 1 -> 3 -> 3 -> 3 -> 4 -> 4 -> 4 -> 4 -> 4 ->
4 -> 4 -> 4 -> 5 -> 5 -> 5 -> 5 -> 5 -> 8.76 -> 8.76 -> 9.78 -> 9.78
>>> x
Head:Node(-7.5)
Tail:Node(9.78)
List:-7.5 -> 1 -> 1 -> 1 -> 3 -> 4 -> 4 -> 5 -> 8.76 -> 9.78
>>> x.removeDuplicates()
>>> x
Head:Node(-7.5)
Tail:Node(9.78)
List:-7.5 -> 1 -> 3 -> 4 -> 5 -> 8.76 -> 9.78
'''
def __init__(self): # You are not allowed to modify the constructor
self.head=None
self.tail=None
def __str__(self): # You are not allowed to modify this method
temp=self.head
out=[]
while temp:






















out.append(str(temp.value))
temp=temp.next
out=' -> '.join(out)

return f'Head:{self.head}\nTail:{self.tail}\nList:{out}'
__repr__=__str__
def isEmpty(self):
return self.head == None
def __len__(self):
count=0
current=self.head
while current:
current=current.next
count+=1
return count


def add(self, value):
# --- YOUR CODE STARTS HERE
pass
def replicate(self):
# --- YOUR CODE STARTS HERE
pass
def removeDuplicates(self):
# --- YOUR CODE STARTS HERE
pass





Section 1: The Sorted Linked List class<br>add(self, item)<br>adds a new Node with value-item to the list making sure that the ascending order is preserved. It<br>needs the item and returns nothing but modifies the linked list. The items in the list might not be<br>unique, but you can assume every value will be numerical (int and float). You are not allowed to<br>traverse the linked list more than once (only one loop required).<br>Input (excluding self)<br>int or float<br>item A numerical value that represents the value of a Node object<br>Examples:<br>>>> x=SortedLinked List()<br>>>> x. add (8.76)<br>>>> x. add(7)<br>>>> x. add (3)<br>>>> x. add(-6)<br>>>> x. add (58)<br>>>> x. add (33)<br>>>> x. add (1)<br>>>> x. add(-88)<br>>>> print(x)<br>Head : Node( -88)<br>Tail:Node(58)<br>List:-88 -> -6 -> 1 -> 3 -> 7 -> 8.76 -> 33 -> 58<br>replicate(self)<br>Returns a new SortedLinkedList object where each elemet of the linked list appears its<br>node's value number of times (3 -> 1 results in 3 -> 3 -> 3 -> 1). Negative numbers and floats are<br>repeated only once immediately after that node. For 0, the node is added, but not repeated.<br>Method returns None if the list is empty. Hint: Using the add method from above could be very<br>useful here!<br>Output<br>SortedLinkedList A new linked list object that repeats values without disturbing the original<br>list<br>None<br>None keyword is returned if the original list is empty<br>

Extracted text: Section 1: The Sorted Linked List class add(self, item) adds a new Node with value-item to the list making sure that the ascending order is preserved. It needs the item and returns nothing but modifies the linked list. The items in the list might not be unique, but you can assume every value will be numerical (int and float). You are not allowed to traverse the linked list more than once (only one loop required). Input (excluding self) int or float item A numerical value that represents the value of a Node object Examples: >>> x=SortedLinked List() >>> x. add (8.76) >>> x. add(7) >>> x. add (3) >>> x. add(-6) >>> x. add (58) >>> x. add (33) >>> x. add (1) >>> x. add(-88) >>> print(x) Head : Node( -88) Tail:Node(58) List:-88 -> -6 -> 1 -> 3 -> 7 -> 8.76 -> 33 -> 58 replicate(self) Returns a new SortedLinkedList object where each elemet of the linked list appears its node's value number of times (3 -> 1 results in 3 -> 3 -> 3 -> 1). Negative numbers and floats are repeated only once immediately after that node. For 0, the node is added, but not repeated. Method returns None if the list is empty. Hint: Using the add method from above could be very useful here! Output SortedLinkedList A new linked list object that repeats values without disturbing the original list None None keyword is returned if the original list is empty
Section 1: The SortedLinked List class<br>Examples:<br>>>> x=SortedLinkedList()<br>>>> x. add(4)<br>>>> x. replicate()<br>He ad: Node (4)<br>Tail:Node (4)<br>List:4 -> 4 -> 4 - > 4<br>>>> x. add(-23)<br>>>> x. add(2)<br>>>> x. add(1)<br>>>> x. add(20.8)<br>>>> X<br>He ad: Node (-23)<br>Tail:Node (20.8)<br>List:-23 -> 1 -> 2 -> 4 -> 20.8<br>>>> x. replicate()<br>He ad : Node (-23)<br>Tail:Node (20.8)<br>List:-23 -> -23 -> 1 -> 2 -> 2 -> 4 -> 4 -> 4 -> 4 -> 20.8 -> 20.8<br>>>> x. add(-1)<br>>>> x. add(0)<br>>>> x. add(3)<br>>>> x. replicate()<br>Head: Node (-23)<br>Tail:Node ( 20.8)<br>List:-23 -> - 23 -> -1 -> -1 -> 0 -> 1 -> 2 -> 2 -> 3 -> 3 -> 3 -> 4 -> 4 -> 4 -> 4 -><br>20.8 -> 20.8<br>>>> X<br>Head: Node (-23)<br>Tail:Node (20.8)<br>List:-23 - > -1 -> 0 -> 1 -> 2 -> 3 -> 4 -> 20.8<br>>>> x.add(2)<br>>>> x.replicate()<br>He ad: Node (-23)<br>Tail:Node ( 20.8)<br>List:-23 -> - 23 -> -1 -> -1 -> 0 -> 1 - > 2 -> 2 - > 2 -> 2 -> 3 -> 3 -> 3 -> 4 -> 4 -> 4<br>-> 4 -> 20.8 -> 20.8<br>removeDuplicates(self)<br>Removes any duplicate nodes from the list, so it modifies the original list. This method must<br>traverse the list only once. It modifies the original list.<br>Output<br>None<br>Nothing is returned when the method completes the work<br>

Extracted text: Section 1: The SortedLinked List class Examples: >>> x=SortedLinkedList() >>> x. add(4) >>> x. replicate() He ad: Node (4) Tail:Node (4) List:4 -> 4 -> 4 - > 4 >>> x. add(-23) >>> x. add(2) >>> x. add(1) >>> x. add(20.8) >>> X He ad: Node (-23) Tail:Node (20.8) List:-23 -> 1 -> 2 -> 4 -> 20.8 >>> x. replicate() He ad : Node (-23) Tail:Node (20.8) List:-23 -> -23 -> 1 -> 2 -> 2 -> 4 -> 4 -> 4 -> 4 -> 20.8 -> 20.8 >>> x. add(-1) >>> x. add(0) >>> x. add(3) >>> x. replicate() Head: Node (-23) Tail:Node ( 20.8) List:-23 -> - 23 -> -1 -> -1 -> 0 -> 1 -> 2 -> 2 -> 3 -> 3 -> 3 -> 4 -> 4 -> 4 -> 4 -> 20.8 -> 20.8 >>> X Head: Node (-23) Tail:Node (20.8) List:-23 - > -1 -> 0 -> 1 -> 2 -> 3 -> 4 -> 20.8 >>> x.add(2) >>> x.replicate() He ad: Node (-23) Tail:Node ( 20.8) List:-23 -> - 23 -> -1 -> -1 -> 0 -> 1 - > 2 -> 2 - > 2 -> 2 -> 3 -> 3 -> 3 -> 4 -> 4 -> 4 -> 4 -> 20.8 -> 20.8 removeDuplicates(self) Removes any duplicate nodes from the list, so it modifies the original list. This method must traverse the list only once. It modifies the original list. Output None Nothing is returned when the method completes the work
Jun 04, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here