Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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: Node0, get next0, set next0, as well as get_data0 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 C-5, 7, 17, 33, 59, 64, 91] [17, 33, 59, 64] try: sllist of.nums-OrderedListo 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: [7, 24, 29, 38, 39, 44, 52, 63, 68, 70, 91] Value error: Start index should be less than or equal to Stop inde try: sllist-of-nums = OrderedList() for num in [44, 7, 68, 24, 29, 91, 52, 39, 63, 38, 7 0] 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: C-78, -43, 0, 11, 13, 23, 65, 456] Index error: Slice indices out of range try: sllist.of numsOrderedListO for num in [456,-78, -43, 65, 13, 23, 0, 11]: sllist_of_nums.addCnum) 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
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