Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please complete the python functions by using the instructions in the skeleton code: -------------------------------------------------------------------------------------------------------------------------------------------------------------- from SLNode import * class SLLException(Exception): Custom exception class to

Please complete the python functions by using the instructions in the skeleton code:

--------------------------------------------------------------------------------------------------------------------------------------------------------------

from SLNode import * class SLLException(Exception): """ Custom exception class to be used by Singly Linked List DO NOT CHANGE THIS CLASS IN ANY WAY """ pass class LinkedList: def __init__(self, start_list=None) -> None: """ Initialize new linked list """ self._head = SLNode(None) # populate SLL with initial values (if provided) # before using this feature, implement insert_back() method if start_list is not None: for value in start_list: self.insert_back(value) def __str__(self) -> str: """ Return content of singly linked list in human-readable form """ out = 'SLL [' node = self._head.next while node: out += str(node.value) if node.next: out += ' -> ' node = node.next out += ']' return out def length(self) -> int: length = 0 node = self._head.next while node: length += 1 node = node.next return length def is_empty(self) -> bool: """ Return True is list is empty, False otherwise """ return not self._head.next # ------------------------------------------------------------------ # def insert_front(self, value: object) -> None: """ Method adds a new node at the beginning of the list Must be implemented with 0(1) runtime complexity """ pass def insert_back(self, value: object) -> None: """ Method adds a new node at the end of the list Must be implemented with 0(1) runtime complexity """ pass def insert_at_index(self, index: int, value: object) -> None: """ Method to insert a new value at the specified index position in the linked list Index 0 is beginning of the list If provided index is invalid method raises custom "SLException" Valid indices are [0, N] inclusive """ pass def remove_at_index(self, index: int) -> None: """ This method removes the node at the specified index position from the linked list Index 0 refers to the beginning of the list If the provided index is invalid, the method raises a custom SLLException valid indices for this method are [0, N - 1] inclusive """ pass 
if __name__ == "__main__": print(" # insert_front example 1") test_case = ["A", "B", "C"] lst = LinkedList() for case in test_case: lst.insert_front(case) print(lst) print(" # insert_back example 1") test_case = ["C", "B", "A"] lst = LinkedList() for case in test_case: lst.insert_back(case) print(lst) print(" # insert_at_index example 1") lst = LinkedList() test_cases = [(0, "A"), (0, "B"), (1, "C"), (3, "D"), (-1, "E"), (5, "F")] for index, value in test_cases: print("Inserted", value, "at index", index, ": ", end="") try: lst.insert_at_index(index, value) print(lst) except Exception as e: print(type(e)) print(" # remove_at_index example 1") lst = LinkedList([1, 2, 3, 4, 5, 6]) print(f"Initial LinkedList : {lst}") for index in [0, 2, 0, 2, 2, -2]: print("Removed at index", index, ": ", end="") try: lst.remove_at_index(index) print(lst) except Exception as e: print(type(e)) print(" # remove example 1") lst = LinkedList([1, 2, 3, 1, 2, 3, 1, 2, 3]) print(f"Initial LinkedList, Length: {lst.length()} {lst}") for value in [7, 3, 3, 3, 3]: print(f"remove({value}): {lst.remove(value)}, Length: {lst.length()}" f" {lst}") print(" # remove example 2") lst = LinkedList([1, 2, 3, 1, 2, 3, 1, 2, 3]) print(f"Initial LinkedList, Length: {lst.length()} {lst}") for value in [1, 2, 3, 1, 2, 3, 3, 2, 1]: print(f"remove({value}): {lst.remove(value)}, Length: {lst.length()}" f" {lst}") print(" # count example 1") lst = LinkedList([1, 2, 3, 1, 2, 2]) print(lst, lst.count(1), lst.count(2), lst.count(3), lst.count(4)) print(" # find example 1") lst = LinkedList(["Waldo", "Clark Kent", "Homer", "Santa Claus"]) print(lst) print(lst.find("Waldo")) print(lst.find("Superman")) print(lst.find("Santa Claus")) print(" # slice example 1") lst = LinkedList([1, 2, 3, 4, 5, 6, 7, 8, 9]) ll_slice = lst.slice(1, 3) print("Source:", lst) print("Start: 1 Size: 3 :", ll_slice) ll_slice.remove_at_index(0) print("Removed at index 0 :", ll_slice) print(" # slice example 2") lst = LinkedList([10, 11, 12, 13, 14, 15, 16]) print("Source:", lst) slices = [(0, 7), (-1, 7), (0, 8), (2, 3), (5, 0), (5, 3), (6, 1)] for index, size in slices: print("Start:", index, "Size:", size, end="") try: print(" :", lst.slice(index, size)) except: print(" : exception occurred.")

--------------------------------------------------------------------------------------------------------------------------------------------------------

#SLNode.py

class SLNode: """ Singly Linked List Node class DO NOT CHANGE THIS CLASS IN ANY WAY """ def __init__(self, value: object, next=None) -> None: self.value = value self.next = next

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

Recommended Textbook for

Objects And Databases International Symposium Sophia Antipolis France June 13 2000 Revised Papers Lncs 1944

Authors: Klaus R. Dittrich ,Giovanna Guerrini ,Isabella Merlo ,Marta Oliva ,M. Elena Rodriguez

2001st Edition

3540416641, 978-3540416647

More Books

Students also viewed these Databases questions