Question: Please help make the program in Python by following the given instructions commented in the code: -------------------------------------------------------------------------------------------------------------------------------------------------------------- from SLNode import * class SLLException(Exception): Custom

Please help make the program in Python by following the given instructions commented in the 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: """ Adding a new node to the beginning of the list """ new_node = SLNode(value) new_node.next = self._head.next self._head.next = new_node pass def insert_back(self, value: object) -> None: """ Adding a new node at the end of the list """ new_node = SLNode(value) node = self._head while node.next: node = node.next node.next = new_node pass def insert_at_index(self, index: int, value: object) -> None: """ TODO: Write this implementation """ if index < 0 or index > self.length(): raise SLLException("Index out of range") if index == 0: self.insert_front(value) else: node = self._head for i in range(index): node = node.next new_node = SLNode(value) new_node.next = node.next node.next = new_node pass def remove_at_index(self, index: int) -> None: """ Removing the node at the specified index position from the linked list """ if index < 0 or index >= self.length(): raise SLLException("Index out of range") #For node at head range node = self._head for i in range(index): node = node.next node.next = node.next.next pass
 def remove(self, value: object) -> bool: """ Method traverses the list beginning to end and removes the first node that matches the provided value object. The method returns True if a node was removed from the list. Otherwise, it returns False. """ pass def count(self, value: object) -> int: """ This method counts the number of elements in the list that match the provided value object. The method then returns this number. """ pass def find(self, value: object) -> bool: """ Method returns a Boolean value based on whether or not the provided value object exists in the list. """ pass def slice(self, start_index: int, size: int) -> "LinkedList": """ method returns a new LinkedList object that contains the requested number of nodes from the original list, """ 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

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!