Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 . add ( self , item ) ( 2 points ) adds a new Node with value = item to the list making sure

1.add(self, item)(2 points) 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). Preconditions and Postconditions item: int/float -> numerical value Returns: None
2.split(self)(2 points) Divides the original list in half, returning two new instances of SortedLinkedList, one with the first half, and another one with the second half. If the number of nodes is odd, the extra node should go in the first half. It does not mutate the original list. Method returns None if the original list is empty. Postconditions Returns: None -> if the original list is empty SortedLinkedList, SortedLinkedList -> two linked list objects
3.removeDuplicates(self)(2 points) Removes any duplicate nodes from the list, so it modifies the original list. This method must traverse the list only once. Values such as 2 and 2.0 are considered duplicates, so you can remove either one. Postconditions Returns: None
4. intersection(self, other)(2 points) Given two instances of SortedLinkedList, returns a new instance of SortedLinkedList that contains the nodes that appear in both lists. The intersection should not keep duplicates. You are not allowed to alter the original lists. Preconditions and Postconditions other: SortedLinkedList Returns: SortedLinkedList
class Node: # You are not allowed to modify this class
def __init__(self, value):
self.value = value
self.next = None
def __str__(self):
return f"Node({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.removeDuplicates()
>>> x
Head:Node(-7.5)
Tail:Node(9.78)
List:-7.5->1->3->4->5->8.76->9.78
>>> sub1, sub2= x.split()
>>> sub1
Head:Node(-7.5)
Tail:Node(4)
List:-7.5->1->3->4
>>> sub2
Head:Node(5)
Tail:Node(9.78)
List:5->8.76->9.78
>>> x
Head:Node(-7.5)
Tail:Node(9.78)
List:-7.5->1->3->4->5->8.76->9.78
>>> x.add(1)
>>> x.intersection(sub1)
Head:Node(-7.5)
Tail:Node(4)
List:-7.5->1->3->4
'''
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}
Tail:{self.tail}
List:{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
def split(self):
# --- YOUR CODE STARTS HERE
def removeDuplicates(self):
# --- YOUR CODE STARTS HERE
def intersection(self, other):
# --- YOUR CODE STARTS HERE
def run_tests():
import doctest
doctest.testmod(verbose=True)
if __name__=="__main__":
run_tests()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

explain what is meant by experiential learning

Answered: 1 week ago

Question

identify the main ways in which you learn

Answered: 1 week ago