Question: class Node: 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

class Node: 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 def __str__(self): return self.data 
class OrderedList: def __init__(self): self.head = None def is_empty(self): return self.head == None def size(self): curr = self.head count = 0 while curr != None: count = count + 1 curr = curr.get_next() return count def add(self, item): current = self.head exist = False previous = None stop = False while current != None and not stop: if current.get_data() == item: exist = True break if current.get_data() > item: stop = True else: previous = current current = current.get_next() if not exist: temp = Node(item) if previous == None: temp.set_next(self.head) self.head = temp else: temp.set_next(current) previous.set_next(temp) def get(self, index): if index = self.size(): print("Index out of range") else: current = self.head i = 0 while i != index: current = current.get_next() i = i + 1 return current.get_data() def remove(self, item): current = self.head previous = None found = False while not found: if current.get_data() == item: found = True else: previous = current current = current.get_next() if previous == None: self.head = current.get_next() else: previous.set_next(current.get_next()) def __iter__(self): return LinkedListIterator(self.head) def __str__(self): result = "[" node = self.head if node != None: result += str(node.data) node = node.next while node: result += ", " + str(node.data) node = node.next result += "]" return result def slice(self, start, stop): tmplis = list() 

class Node: def __init__(self, init_data): self.data = init_data self.next = None def

Extend the OrderedList class by adding the slice(self, start, stop) method that returns a slice of the ordered list. It should take two parameters, start and stop, and return new ordered list with nodes containing items starting at the start position and going up to but not including the stop position. Both start and stop are non-negative integer values. The function should raise an IndexError if it is passed start and/or stop values that are outside the valid range. The start value should be less than or equal to the stop value. The function should raise a ValueError if this is not the case The implementations of the Node class is provided to you as part of this exercise. You can simply use: Node), get_next), set_next(), as well as get data) and set data) as necessary in your function definition. Note: You should include the entire OrderedList class definition in your answer to this question For example Test Result try: [-5, 7, 17, 33, 59, 64, 91] [17, 33, 59, 64] sllist_of_nums OrderedList() for num in [91, -5, 59, 7, 64, 33, 17]: sllist_of_nums.add(num) print (sllist_of_nums) print (sllist_of_nums.slice(2, 6)) print("Index error:",err) print ("Value error:",err) except IndexError as err: except ValueError as err: try: 17, 24, 29, 38, 39, 44, 52, 63, 68, 70, 91] Value error: Start index should be less than or equal to Stop index sllist_of_numsOrderedList() for num in [44, 7, 68, 24, 29, 91, 52, 39, 63, 38, 70]: sllist_of_nums.add(num) print(sllist_of_nums) print(sllist_of_nums.slice (10, 6)) print("Index error:",err) print("Value error:",err) except IndexError as err: except ValueError as err: try: [-78, -43, 0, 11, 13, 23, 65, 456] Index error: Sl sllist_of_nums OrderedList() ice indices out of range for num in [456, -78, 43, 65, 13, 23, 0, 11]: sllist_of_nums.add(num) print (sllist_of_nums) print (sllist_of_nums.slice(-1, 6)) print("Index error:",err) print ("Value error:",err) except IndexError as err: except ValueError as err

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!