Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Help me with what it is asking me to do linon DoublyLinkodList import DoublyLinkadList as bLL import unittest # Basic tests are provided for you,
Help me with what it is asking me to do
linon DoublyLinkodList import DoublyLinkadList as bLL import unittest \# Basic tests are provided for you, but you nead to implement the last 3 unittests class testoLl (unittest. TestCase): def test_addfirst_renovefirst (self): "adde itans to front, then raxoves fram frant" d11 - DLL() n=1eg for j in range(5): \# repeat a fow times to make sure removing last item doesn't break anything for 1 in range(n): self assertEqual (len(d11), i) dl1.add_first(i) for i in range(n): self assertEqual (len(d11),ni) self assertEqual(dll. renove_first (),n1-i) with self assertRaises(RuntimeError): dll. romove_first () def test_addlast_renovelast(self): 'add's itans to and, then ravoucs from end" dll - BLL() n=1ag for j in range(5): \# repeat a fow times to make sure removing last itam doesn't break anything for i in range(n): self . assertEqual (len(d11),1) dll.add_last (i) for i in range(n): self assertEqual(len(dli), n-i) self assertEqual(dll. nenove_last (),n1-i) with self - asserthaises(RuntimeErrar): d11. remove_last() def test_add_renove_nix (self): "various add/ravove patterns" dIl - BLL() n=1eg \# addfirst/removelast for j in range(5): \# repeat a fow times to make sure romoving final node doesn't break anything for i in range(n): self assertEqual(len(dli), i) dll.add_first(i) for 1 in range(n): self assertEqual(len(dll), n-i) self assertEqual(dil. renove_last (),1) \# addlast/removefirst for j in range(5): \# repeat a fow tines to make sure removing final node doesn't break anything for i in range(n): self assertEqual(len(d11), i) dll.add_last (i) for 1 in range(n): self assertEqual(len(dl1), n-i) self assertequal(dll. remove_first (),i) \# mix of first/last. for j in range(5): \# repeat a fow times to make sure removing final node doesn't break anything for 1 in range(n): self assertEqual(len(dl1), i) if iq2: dll.add_last(i) \# odd numbers - add last else: dll.add_frst(i) \# even numbers - add first for 1 in range(n): self assertEqual (len(dll), ni) if iezz: self assertequal(dll.renove_last (), ni) \# odd numbers: remove last else: self assertequal(dll. renove_frst ( ), n2i) \# even numberes: renove first \# 1000: Add docstrings to and implement the unittests below def test_contains (self): pass def test_neighbors (self): pass def test_renove_iten (self) : pass unittest.main() 1) Add a dictionary DoublyLinkedList.py provides basic Node and DoublyLinkedList classes that supports typical DLL operations. Modify these methods to keep the _nodes dictionary up to date with item:node pairs as you add and remove from the DLL so you can access any node in O(1). 2) Implement __contains__(item) _nodes is a private attribute, and thus should not be called explicilty in your tests. Instead, use _nodes to add a O(1) __contains__() method to your DLL, and test __contains__(): >dll= DoublyLinkedList ( range (5) ) 3 in dll \# O(1), even though 3 is in the middle True 5 in dll \# Note the use of 'in' to call the dunder method __ contains_() False Write a unittest that verifies contains works as expected as you add and remove nodes. 3) Implement neighbors (item) Add a method neighbors (item) to your DoublyLinkedList class that returns the items immediately before and after the node with item: d11= DoublyLinkedList ( range (5)) > dll.neighbors ( 3 ) (2,4) (None, 1) > dll.neighbors(4) \# Edge case - tail (3, None) - Should be O(1) - When called on the item stored in the head/tail, return None as the previousext item, as shown above - Raise a RuntimeError if someone searches for an item not in the DLL Include unittests for the above behavior (except the running time). 4) Implement remove_node(item) Add a method that removes the node containing an item from your DLL. - O(1) - Make sure to "stitch together" the adjacent nodes - Edge cases you should be able to handle: - Raise a RuntimeError if someone tries to remove an item not in the DLL - Removing the head - Removing the tail Include unittests for the above behavior (except the running time)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