Question: I have a class for I need to make a def pop that will: def pop(self, pos=None): # LL: You shouldn't delete the docstrings. they

I have a class for I need to make a def pop that will:

def pop(self, pos=None): # LL: You shouldn't delete the docstrings. they are there to give you information on what to do. # # LL: Here, you want to do if-statements, and raise the errors when pos is invalid. For example. # if pos < 0 or pos > self.size - 1: # raise IndexError # # LL: You need to check four things, and doing them in this order is best: # 1) Is pos None? If so, you need to re-assign it to be the last index of the list # 2) Is pos not an integer? If not, raise a TypeError # 3) Is the list empty? If so, raise an IndexError # 4) Is pos < 0 or > self.size -1? If so, raise an Index Error. # # Once you do all of those checks, you are guaranteed that pos is a valid index. # Then, you need to write a loop that steps curr and prev "pos" number of times down the list such that # when the loop stops, curr will point to the node to remove and prev will point to the previous node. # # After the loop, if correct, you can use the logic to remove curr from the remove() method. # # the final thing that you have to do is return the removed node's data.

Full Code:

class Node: def __init__(self, data=None, next=None): self.data = data self.next = next def __str__(self): return repr(self) def __repr__(self): return f"Node(data={str(self.data)})" class LinkedList: def __init__(self): self.head = None self.tail = None self.size = 0 def __iter__(self): current = self.head while current is not None: yield current current = current.next def __len__(self): return self.size def add(self, item): node = Node(item,self.head) self.head = node if self.size == 0: self.tail = node self.size += 1 #raise NotImplementedError def search(self, item): if self.head is None: return False curr = self.head while curr != None: if curr.data == item: return True else: curr = curr.next return False raise NotImplementedError def append(self, item): if self.head is None: self.tail= self.head = Node(item) else: self.tail.next = Node(item) self.tail = self.tail.next self.size += 1 #raise NotImplementedError def remove(self, item): if self.head is None: raise ValueError curr = self.head prev = None while curr != None: if curr.data == item: break else: prev = curr curr = curr.next if curr is None: raise ValueError elif prev is None: if self.size == 1: self.tail = None self.head = curr.next self.size -= 1 elif curr == self.tail: prev.next = None self.tail = prev self.size -= 1 else: prev.next = curr.next self.size -= 1 #raise NotImplementedError def pop(self, pos=None): if pos == None: self.tail = LinkedList[-1] elif pos != int: raise TypeError elif len(LinkedList) == 0: raise IndexError elif pos < 0 or pos > self.size -1: raise IndexError for pos in LinkedList: if pos == None: self.pop(-1) elif prev is None: if self.size == 1: self.tail = None self.head = curr.next self.size -= 1 elif curr == self.tail: prev.next = None self.tail = prev self.size -= 1 else: prev.next = curr.next self.size -= 1 raise NotImplementedError if __name__ == "__main__": from linked_list_test import TestSuite import sys suite = TestSuite(sys.modules[__name__], "LinkedList") suite.test_add() suite.test_search() suite.test_append() suite.test_remove() suite.test_pop_empty_list() suite.test_pop_bad_argument() suite.test_pop_list_length_1() suite.test_pop_multiple_items() # Extra credit tests # suite.test_extra_credit_getitem() # suite.test_extra_credit_setitem() # suite.test_extra_credit_delitem() # suite.test_extra_credit_reverse() print(f"There are 8 required tests and 4 extra credit tests.") print(f"You Passed: {suite.tests_run_count-len(suite.tests_failed)}, Failed: {len(suite.tests_failed)}") if len(suite.tests_failed) > 0: print("List of failed tests. See test output above for details.") for i in suite.tests_failed: print(f"\u2717 {i}()")

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!